├── AGGREGATE FUNCTIONS ├── 1. Find the total purchase amount for all orders.txt ├── 10. Find the highest purchase amount each customer ordered.txt ├── 11. Highest purchase amount on a date for each salesman.txt ├── 12. Highest purchase amount of customers on a given date.txt ├── 13. Higher purchase amount of customers within a range.txt ├── 14. Higher purchase amount by the customer within a list.txt ├── 15. Highest purchase amount from customers.txt ├── 16. Customer details and purchases over a certain amount.txt ├── 17. Customer with the highest purchase amount.txt ├── 18. Count all orders for a specific date.txt ├── 19. Number of non NULL city values for salesmen.txt ├── 2. Find the average purchase amount of all orders.txt ├── 20. Count how many salesmen each day register orders.txt ├── 21. Calculate the average price of all the products.txt ├── 22. Find the number of products over or equal to Rs 350.txt ├── 23. Compute the average price for unique companies.txt ├── 24. Calculate the total allotment for all departments.txt ├── 25. Find out the number of employees in each department.txt ├── 3. Find the number of salesmen for each customer.txt ├── 4. Find number of customers have listed their names.txt ├── 5. How many customers get a grade for their performance.txt ├── 6. Find the maximum purchase amount of all the orders.txt ├── 7. Find the minimum purchase amount of all the orders.txt ├── 8. Highest grade for each of the cities of the customers.txt └── 9. Highest purchase amount ordered by the each customer.txt ├── Formatting Query Output ├── 1. Salesmen commission with the percent sign (%).txt ├── 10. Purchase amount and commission for the last order.txt ├── 2. Number of orders booked for each day.txt ├── 3. Display orders after sorting orders by order number.txt ├── 4. Arrange the orders according to the order date.txt ├── 5. Oldest orders first on the highest purchase amount.txt ├── 6. Display customer name, city, grade.txt ├── 7. Salesman details by smallest ID along with order date.txt ├── 8. Display customer name, city and grade by highest grade.txt └── 9. Find largest number of orders booked by the customer.txt ├── HACKERRANK ├── 1. Types of Triangle.txt ├── 10. African Cities.txt ├── 11. Population Census.txt ├── 12. Average Population of Each Continent.txt ├── 13. The Report.txt ├── 14. Top Competitors.txt ├── 15. Ollivander's Inventory.txt ├── 16. Challenges.txt ├── 17. Contest Leaderboard.txt ├── 18. Draw The Triangle 1.txt ├── 19. SQL Project Planning.txt ├── 2. Weather Observation Station 19.txt ├── 20. Placements.txt ├── 21. Symmetric Pairs.txt ├── 22. Interviews.txt ├── 23. 15 Days of Learning SQL.txt ├── 24. Draw The Triangle 2.txt ├── 25. Print Prime Numbers.txt ├── 3. Weather Observation Station 13.txt ├── 4. Weather Observation Station 14.txt ├── 5. Weather Observation Station 15.txt ├── 6. Weather Observation Station 16.txt ├── 7. Weather Observation Station 17.txt ├── 8. Weather Observation Station 18.txt └── 9. Weather Observation Station 20.txt ├── HR Database - SORT FILTER ├── 1. Find those employees who earn below 6000.txt ├── 10. Employees whose first name is ending with the letter m.txt ├── 11. Employees whose salary is out of a given range.txt ├── 12. Employees who was hired during given dates.txt ├── 13. Find employees who works either in department 70 or 90.txt ├── 14. Find those employees who is working under a manager.txt ├── 15. Find those employees hired before June 21st, 2002.txt ├── 16. Find employees whose managers are hold given IDs.txt ├── 17. Employees have the given letters in their first name.txt ├── 18. Employees earn over 11000 or 7th digit in their phone.txt ├── 19. Employees with s as their 3rd character in first name.txt ├── 2. Find those employees who earn more than 8000.txt ├── 20. Find employees who is working except given departments.txt ├── 21. Find employees who is working given departments.txt ├── 22. Find employees who did two or more jobs in the past.txt ├── 23. Difference between highest and lowest salary for a job.txt ├── 24. Jobs done by two or more for more than 300 days.txt ├── 25. Country ID and number of cities in country has.txt ├── 26. Find employees managed by the manager.txt ├── 27. Details of jobs in descending order by title.txt ├── 28. Employees who are Sales Representatives or Sales Men.txt ├── 29. Average salary of each commission-based department.txt ├── 3. Display Employees whose last name is McEwen.txt ├── 30. Departments with managers manage more than 4 employees.txt ├── 31. Departments more than 10 employees who get commissions.txt ├── 32. The date when he left his last job and his employee ID.txt ├── 33. A department 50 employee without a commission %.txt ├── 34. Jobs which average salary is above 8000.txt ├── 35. Salary ranges for jobs with a minimum and maximum.txt ├── 36. Employees whose first or last name begins with D.txt ├── 37. Display jobs with minimum salaries over 9000.txt ├── 38. Employees who joined after 7th September, 1987.txt ├── 4. Details of employees without any department number.txt ├── 5. Information about the department Marketing.txt ├── 6. Employees first name does not contain the letter M.txt ├── 7. Employees with salaries of given range and commission.txt ├── 8. Employees who does not earn any commission.txt └── 9. Find employees whose salary is within 9000 to 17000.txt ├── Query on Multiple Tables ├── 1. Customer and salesmen who lives in the same city.txt ├── 2. Customers along with the salesmen who works for them.txt ├── 3. Customer lives in a city other than the salesman's.txt ├── 4. Find out customers who made the order.txt ├── 5. Sort out the customer who made at least an order.txt ├── 6. Customers served by a salesman and commission.txt ├── 7. Find salesman commission details of given customer.txt └── 8. Find all customers with orders on October 5, 2012.txt ├── SQL JOINS on HR Database ├── 1. Display the name, department number, for each employee.txt ├── 10. Display employees who have or have not any department.txt ├── 11. Display employees and their manager.txt ├── 12. Employees who work in the same department as Taylor.txt ├── 13. Jobs which started between two given dates.txt ├── 14. Salary difference between jobs maximum and employees.txt ├── 15. Employees working in the department who got commission.txt ├── 16. Salary differences of employees in the department 80.txt ├── 17. Display country, city, and the departments.txt ├── 18. Department name and the full name of the manager.txt ├── 19. Display the job title and average salary of employees.txt ├── 2. Display name, department, city and state each employee.txt ├── 20. Employees who earn a salary over 12000.txt ├── 21. Departments where at least 2 employees are working.txt ├── 22. Display the department, manager name, and their city.txt ├── 23. Number of days worked for all jobs in department 80.txt ├── 24. Employees worked without a commission percentage.txt ├── 25. Employees who work in any department located in London.txt ├── 26. Number of employees in each of the department.txt ├── 27. Display the employee with ID and present country.txt ├── 3. List all employees names, salaries, and job grades.txt ├── 4. Display all employees for departments 80 or 40.txt ├── 5. Employees who contain a letter z to their first name.txt ├── 6. Departments which does not have any employee.txt ├── 7. Employees who earn less than employee 182.txt ├── 8. Employees including the first name of their manager.txt └── 9. Display the department name, city for each department.txt ├── SQL JOINS ├── 1. Salesmen and customer, who belongs to same city.txt ├── 10. Customer have placed no order or one or more orders.txt ├── 11. Find customers who have placed no order or one or more.txt ├── 12. Salesmen works either for one or more customer or not.txt ├── 13. Salesmen who works either for one or more customer.txt ├── 14. Salesmen work for one or more customers or yet to join.txt ├── 15. One or more customers ordered from the existing list.txt ├── 16. Customer who is neither in the list nor have a grade.txt ├── 17. Salesman will appear for all customer and vice versa.txt ├── 18. All customers will be served by a salesman from a city.txt ├── 19. Salesmen belongs to a city, customers have a grade.txt ├── 2. Customers and their cities for given range of orders.txt ├── 20. Customers from a different city than the salesmen.txt ├── 21. Display each item producer company.txt ├── 22. Display the item name, price, and company name.txt ├── 23. Display the average price of items of each company.txt ├── 24. Company whose products have an average price.txt ├── 25. Display ID and price of most expensive product.txt ├── 26. Display employees including their department.txt ├── 27. Employee and sanction amount for their department.txt ├── 28. Find employees and departments with a given budget.txt ├── 29. Departments where more than two employees are working.txt ├── 3. From the following tables write a SQL query to find the salesperson(s) and the customer(s) he represents. Return Customer Name, city, Salesman, commission..txt ├── 4. From the following tables write a SQL query to find salespeople who received commissions of more than 12 percent from the company. Return Customer Name, customer city, Salesman, commission..txt ├── 5. Customers and salespeople live in different cities.txt ├── 6. Display commission of the salesman for an order.txt ├── 7. Join within the tables salesman, customer and orders.txt ├── 8. Customer who works either through a salesman or by own.txt └── 9. Customers who holds a grade less than 300.txt ├── SQL UNION ├── 1. Display all salesmen and customer located in London.txt ├── 2. Display distinct salesman and their cities.txt ├── 3. Salesmen, customer involved in inventory management.txt ├── 4. Largest and smallest orders are produced on each date.txt ├── 5. Largest and smallest orders on each date.txt ├── 6. Salesmen who do not have customers in their cities.txt ├── 7. Any salesman was matched to the city of any customer.txt ├── 8. Ratings of all customers with a comment string.txt └── 9. Customer with more than one current order.txt ├── SQL VIEW ├── 1. View for the salesmen who belong to the city New York.txt ├── 2. View to show salesmen with columns id, name and city.txt ├── 3. View to find the salesmen of the city New York.txt ├── 4. View to count many customers at each lavel of grade.txt ├── 5. View to keep track the number of customers ordering.txt ├── 6. View to show for each order the salesman and customer.txt ├── 7. View to find salesman with the highest order of a day.txt └── 8. View to find the salesman with the highest order.txt ├── SUBQUERIES ├── 1. Display all orders issued by Paul Adam..txt ├── 10. Customers who ordered on August 17, 2012.txt ├── 11. List all salesmen with more than one customer.txt ├── 12. Find all orders with above-average amounts.txt ├── 13. Customer order amounts that are on or above average.txt ├── 14. Sums of all order amounts, grouped by date.txt ├── 15. Find out if any of the customers are located in London..txt ├── 16. Find the salesmen who have multiple customers.txt ├── 17. Find all the salesmen worked for only one customer.txt ├── 18. Show all salesmen with more than one order.txt ├── 19. Find salespeople in the city where the customer lives.txt ├── 2. Show all the orders for the London-based salesman.txt ├── 20. Display the salesmen with customers following them.txt ├── 21. Alphabetically list the salesmen below their customers.txt ├── 22. Customers with the highest grade in alphabetical order.txt ├── 23. All orders with amounts greater than one on given date.txt ├── 24. Orders smaller than any amount for a London customer.txt ├── 25. Orders higher than any amount for a London customer.txt ├── 26. Customers with higher grades than everyone in New York.txt ├── 27. Salesperson live in the same city as the customers.txt ├── 28. Customers whose grade differs from a London customer.txt ├── 29. Customers who differ from the grade in the city Paris.txt ├── 3. Orders of the salesman working for the customer 3007.txt ├── 30. Different grades than other Dallas customers.txt ├── 31. Find the average price of each manufacturer products.txt ├── 32. List of products with an average price of 350 or more.txt ├── 33. Prices for the most expensive products by each company.txt ├── 34. Find all employees with last names Gabriel or Dosio.txt ├── 35. List all employees in departments 89 and 63.txt ├── 36. Departments with allotment amounts exceeding Rs. 50000.txt ├── 37. Departments with a sanction amount higher than average.txt ├── 38. Find departments with more than two employees.txt ├── 39. Employees working with the lowest sanction amount.txt ├── 4. Orders with values greater than the given date average.txt ├── 5. Find all orders attributed to salesman in New York.txt ├── 6. Display the commissions of all salespeople in Paris.txt ├── 7. The customer whose ID is 2001 is below Mc Lyons.txt ├── 8. Counts the customers with grades over New York average.txt └── 9. Find salesmen who earned the maximum commission..txt └── Sub Queries On HR Database ├── 1. Employees getting more salary than employee 163.txt ├── 10. Employees, salary is between smallest salary and 2500.txt ├── 11. Display employees not working in certain departments.txt ├── 12. Display employees who get the second highest salary.txt ├── 13. Employee names and hire dates for Claras department.txt ├── 14. Employees in a department beginning with T.txt ├── 15. J-named employees earn more than average.txt ├── 16. Employees whose department location is Toronto.txt ├── 17. Employees salary is smaller than job title MK_MAN.txt ├── 18. Employees whose job title is MK_MAN, exclude MK_MAN.txt ├── 19. Employees whose salary is higher than title is PU_MAN.txt ├── 2. Employees who work in the same designation as ID 169.txt ├── 20. Salary exceeds the average salary of any department.txt ├── 21. Employees whose salary is more than 3700.txt ├── 22. Departments which contains at least one employee.txt ├── 23. Employees whose job title is ST_MAN, IT_PROG.txt ├── 24. Employees salary is more and less than the average.txt ├── 25. SalaryStatus column with a title HIGH and LOW.txt ├── 26. Departments that have one or more employees.txt ├── 27. Employees in departments in the United Kingdom.txt ├── 28. IT employees who earn more than the average.txt ├── 29. Determine who earns more than Mr. Ozer.txt ├── 3. Smallest salary of any of the departments.txt ├── 30. Manager who works for a department based in the US.txt ├── 31. Salary exceeds 50% of their departments total salary.txt ├── 32. Find out the details of employees who are managers.txt ├── 33. A department 50 employee without a commission %.txt ├── 34. Jobs which average salary is above 8000.txt ├── 35. Departments which located in the city London.txt ├── 36. Employees earn more than average salary in ASC order.txt ├── 37. Employees earn more than the maximum salary.txt ├── 38. Location where department number 30 is located.txt ├── 39. Department where the employee works with ID 201.txt ├── 4. Employees who earn more than the average salary.txt ├── 40. Employee who works in that department which ID is 40.txt ├── 41. Employees who work in the department Marketing.txt ├── 42. Employees earning more than departments minimum wage.txt ├── 43. Employees who was hired after the employee with ID 165.txt ├── 44. Minimum salary of a department which ID is 70.txt ├── 45. Employees earn less than the average salary like Laura.txt ├── 46. Employees whose department is in London.txt ├── 47. Display the city where the employee with ID 134 works.txt ├── 48. Employees who already done one or more jobs.txt ├── 49. Departments which starting salary is at least 8000.txt ├── 5. Salary of all employees who report to Payam.txt ├── 50. Manager who is supervising 4 or more employees.txt ├── 51. Employees worked as a Sales Representative in the past.txt ├── 52. Employees who earn second lowest salary of all.txt ├── 53. Display the details of departments managed by Susan.txt ├── 54. Highest salary drawar in a department.txt ├── 55. Employees who did not have any job in the past.txt ├── 6. All employees in the Finance department.txt ├── 7. Salary and reporting person id is 3000 and 121.txt ├── 8. Employee whose id is any of the number 134, 159, 183.txt └── 9. Employees whose salary is within the given range.txt /AGGREGATE FUNCTIONS/1. Find the total purchase amount for all orders.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to calculate total purchase 2 | amount of all orders. Return total purchase amount. 3 | 4 | Ans--> 5 | 6 | SELECT SUM (purch_amt) 7 | FROM orders; 8 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/10. Find the highest purchase amount each customer ordered.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the highest purchase 2 | amount ordered by each customer on a particular date. Return, order date and 3 | highest purchase amount. 4 | 5 | Ans--> 6 | 7 | SELECT customer_id,ord_date,MAX(purch_amt) 8 | FROM orders 9 | GROUP BY customer_id,ord_date; 10 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/11. Highest purchase amount on a date for each salesman.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to determine the highest 2 | purchase amount made by each salesperson on '2012-08-17'. Return salesperson 3 | ID, purchase amount. 4 | 5 | Ans--> 6 | 7 | SELECT salesman_id,MAX(purch_amt) 8 | FROM orders 9 | WHERE ord_date = '2012-08-17' 10 | GROUP BY salesman_id; 11 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/12. Highest purchase amount of customers on a given date.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the highest order 2 | (purchase) amount by each customer on a particular order date. Filter the 3 | result by highest order (purchase) amount above 2000.00. Return customer id, 4 | order date and maximum purchase amount. 5 | 6 | Ans--> 7 | 8 | SELECT customer_id,ord_date,MAX(purch_amt) 9 | FROM orders 10 | GROUP BY customer_id,ord_date 11 | HAVING MAX(purch_amt)>2000.00; 12 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/13. Higher purchase amount of customers within a range.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the maximum order 2 | (purchase) amount in the range 2000 - 6000 (Begin and end values are included. 3 | ) by combination of each customer and order date. Return customer id, order 4 | date and maximum purchase amount. 5 | 6 | Ans--> 7 | 8 | SELECT customer_id,ord_date,MAX(purch_amt) 9 | FROM orders 10 | GROUP BY customer_id,ord_date 11 | HAVING MAX(purch_amt) BETWEEN 2000 AND 6000; 12 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/14. Higher purchase amount by the customer within a list.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the maximum order 2 | (purchase) amount based on the combination of each customer and order date. 3 | Filter the rows for maximum order (purchase) amount is either 2000, 3000, 4 | 5760, 6000. Return customer id, order date and maximum purchase amount. 5 | 6 | Ans--> 7 | 8 | SELECT customer_id,ord_date,MAX(purch_amt) 9 | FROM orders 10 | GROUP BY customer_id,ord_date 11 | HAVING MAX(purch_amt) IN(2000 ,3000,5760, 6000); 12 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/15. Highest purchase amount from customers.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to determine the maximum 2 | order amount for each customer. The customer ID should be in the range 3 | 3002 and 3007(Begin and end values are included.). Return customer id and 4 | maximum purchase amount. 5 | 6 | Ans--> 7 | 8 | SELECT customer_id,MAX(purch_amt) 9 | FROM orders 10 | WHERE customer_id BETWEEN 3002 and 3007 11 | GROUP BY customer_id; 12 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/16. Customer details and purchases over a certain amount.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the maximum order 2 | (purchase) amount for each customer. The customer ID should be in the range 3 | 3002 and 3007(Begin and end values are included.).Filter the rows for maximum 4 | order (purchase) amount is higher than 1000. Return customer id and maximum 5 | purchase amount. 6 | 7 | Ans--> 8 | 9 | SELECT customer_id,MAX(purch_amt) 10 | FROM orders 11 | WHERE customer_id BETWEEN 3002 and 3007 12 | GROUP BY customer_id 13 | HAVING MAX(purch_amt)>1000; 14 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/17. Customer with the highest purchase amount.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to determine the maximum 2 | order (purchase) amount generated by each salesperson. Filter the rows 3 | for the salesperson ID is in the range 5003 and 5008 (Begin and end values 4 | are included.). Return salesperson id and maximum purchase amount. 5 | 6 | Ans--> 7 | 8 | SELECT salesman_id,MAX(purch_amt) 9 | FROM orders 10 | GROUP BY salesman_id 11 | HAVING salesman_id BETWEEN 5003 AND 5008; 12 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/18. Count all orders for a specific date.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to count all the orders 2 | generated on '2012-08-17'. Return number of orders. 3 | 4 | Ans--> 5 | 6 | SELECT COUNT(*) 7 | FROM orders 8 | WHERE ord_date='2012-08-17'; 9 | 10 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/19. Number of non NULL city values for salesmen.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to count the number of 2 | salespeople in a city. Return number of salespeople. 3 | 4 | 5 | Ans--> 6 | 7 | SELECT COUNT(*) 8 | FROM salesman 9 | WHERE city IS NOT NULL; 10 | 11 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/2. Find the average purchase amount of all orders.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to calculate the average 2 | purchase amount of all orders. Return average purchase amount. 3 | 4 | Ans--> 5 | 6 | SELECT AVG (purch_amt) 7 | FROM orders; -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/20. Count how many salesmen each day register orders.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to count the number of orders 2 | based on the combination of each order date and salesperson. Return order 3 | date, salesperson id. 4 | 5 | Ans--> 6 | 7 | 8 | SELECT ord_date,salesman_id,COUNT(*) 9 | FROM orders 10 | GROUP BY ord_date,salesman_id; 11 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/21. Calculate the average price of all the products.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to calculate the average 2 | product price. Return average product price. 3 | 4 | Ans--> 5 | 6 | SELECT AVG(pro_price) AS "Average Price" 7 | FROM item_mast; 8 | 9 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/22. Find the number of products over or equal to Rs 350.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to count the number of 2 | products whose price are higher than or equal to 350. Return number of 3 | products 4 | 5 | Ans--> 6 | 7 | SELECT COUNT(*) AS "Number of Products" 8 | FROM item_mast 9 | WHERE pro_price >= 350; 10 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/23. Compute the average price for unique companies.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to compute the average price 2 | for unique companies. Return average price and company id. 3 | 4 | Ans--> 5 | 6 | SELECT AVG(pro_price) AS "Average Price", 7 | pro_com AS "Company ID" 8 | FROM item_mast 9 | GROUP BY pro_com; 10 | 11 | 12 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/24. Calculate the total allotment for all departments.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to compute the sum of the 2 | allotment amount of all departments. Return sum of the allotment amount. 3 | 4 | Ans--> 5 | 6 | SELECT SUM(dpt_allotment) 7 | FROM emp_department; 8 | 9 | 10 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/25. Find out the number of employees in each department.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to count the number of 2 | employees in each department. Return department code and number of employees. 3 | 4 | Ans--> 5 | 6 | SELECT emp_dept, COUNT(*) 7 | FROM emp_details 8 | GROUP BY emp_dept; 9 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/3. Find the number of salesmen for each customer.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query that counts the number of 2 | unique salespeople. Return number of salespeople. 3 | 4 | Ans--> 5 | 6 | SELECT COUNT (DISTINCT salesman_id) 7 | FROM orders; 8 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/4. Find number of customers have listed their names.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to count the number of 2 | customers. Return number of customers. 3 | 4 | 5 | Ans--> 6 | 7 | SELECT COUNT(*) 8 | FROM customer; 9 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/5. How many customers get a grade for their performance.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to determine the number of 2 | customers who received at least one grade for their activity. 3 | 4 | 5 | Ans--> 6 | 7 | SELECT COUNT (ALL grade) 8 | FROM customer; 9 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/6. Find the maximum purchase amount of all the orders.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the maximum purchase 2 | amount. 3 | 4 | Ans--> 5 | 6 | SELECT MAX (purch_amt) 7 | FROM orders; 8 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/7. Find the minimum purchase amount of all the orders.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the minimum purchase 2 | amount. 3 | 4 | Ans--> 5 | 6 | SELECT MIN(purch_amt) 7 | FROM orders; 8 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/8. Highest grade for each of the cities of the customers.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the highest grade of 2 | the customers in each city. Return city, maximum grade. 3 | 4 | Ans--> 5 | 6 | SELECT city,MAX(grade) 7 | FROM customer 8 | GROUP BY city; 9 | -------------------------------------------------------------------------------- /AGGREGATE FUNCTIONS/9. Highest purchase amount ordered by the each customer.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the highest purchase 2 | amount ordered by each customer. Return customer ID, maximum purchase amount. 3 | 4 | Ans--> 5 | 6 | SELECT customer_id,MAX(purch_amt) 7 | FROM orders 8 | GROUP BY customer_id; 9 | -------------------------------------------------------------------------------- /Formatting Query Output/1. Salesmen commission with the percent sign (%).txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to select all the salespeople. 2 | Return salesman_id, name, city, commission with the percent sign (%). 3 | 4 | Ans--> 5 | 6 | SELECT salesman_id,name,city,'%',commission*100 7 | FROM salesman; -------------------------------------------------------------------------------- /Formatting Query Output/10. Purchase amount and commission for the last order.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to calculate the summation of 2 | purchase amount, total commission (15% for all salespeople) by each order 3 | date. Sort the result-set on order date. Return order date, summation of 4 | purchase amount and commission. 5 | 6 | Ans--> 7 | 8 | SELECT ord_date, SUM(purch_amt), 9 | SUM(purch_amt)*.15 10 | FROM orders 11 | GROUP BY ord_date 12 | ORDER BY ord_date; 13 | -------------------------------------------------------------------------------- /Formatting Query Output/2. Number of orders booked for each day.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the number of orders 2 | booked for each day. Return the result in a format like "For 2001-10-10 there 3 | are 15 orders". 4 | 5 | Ans--> 6 | 7 | SELECT 'For',ord_date,',there are', 8 | COUNT (ord_no),'orders.' 9 | FROM orders 10 | GROUP BY ord_date; -------------------------------------------------------------------------------- /Formatting Query Output/3. Display orders after sorting orders by order number.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find all the orders. 2 | Sort the result-set in ascending order by ord_no. Return all fields. 3 | 4 | Ans--> 5 | 6 | SELECT * 7 | FROM orders 8 | ORDER BY ord_no; 9 | 10 | -------------------------------------------------------------------------------- /Formatting Query Output/4. Arrange the orders according to the order date.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find all the orders. 2 | Sort the result-set in descending order by ord_date. Return all fields. 3 | 4 | Ans--> 5 | 6 | SELECT * 7 | FROM orders 8 | ORDER BY ord_date DESC; 9 | 10 | -------------------------------------------------------------------------------- /Formatting Query Output/5. Oldest orders first on the highest purchase amount.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find all the orders. 2 | Sort the result-set in descending order by ord_date and purch_amt. Return 3 | all fields. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM orders 9 | ORDER BY ord_date, purch_amt DESC; 10 | -------------------------------------------------------------------------------- /Formatting Query Output/6. Display customer name, city, grade.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find all the customers. 2 | Sort the result-set by customer_id. Return cust_name, city, grade. 3 | 4 | Ans--> 5 | 6 | SELECT cust_name,city,grade 7 | FROM customer 8 | ORDER BY customer_id; 9 | 10 | 11 | -------------------------------------------------------------------------------- /Formatting Query Output/7. Salesman details by smallest ID along with order date.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query that calculates the maximum 2 | purchase amount generated by each salesperson for each order date. Sort the 3 | result-set by salesperson id and order date in ascending order. Return 4 | salesperson id, order date and maximum purchase amount. 5 | 6 | Ans--> 7 | 8 | 9 | SELECT salesman_id,ord_date,MAX(purch_amt) 10 | FROM orders 11 | GROUP BY salesman_id,ord_date 12 | ORDER BY salesman_id,ord_date; -------------------------------------------------------------------------------- /Formatting Query Output/8. Display customer name, city and grade by highest grade.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find all the customers. 2 | Sort the result-set in descending order on 3rd field. Return customer name, 3 | city and grade. 4 | 5 | Ans--> 6 | 7 | SELECT cust_name,city,grade 8 | FROM customer 9 | ORDER BY 3 DESC; 10 | -------------------------------------------------------------------------------- /Formatting Query Output/9. Find largest number of orders booked by the customer.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query that counts the unique orders 2 | and the highest purchase amount for each customer. Sort the result-set in 3 | descending order on 2nd field. Return customer ID, number of distinct orders 4 | and highest purchase amount by each customer. 5 | 6 | Ans--> 7 | 8 | SELECT customer_id, COUNT(DISTINCT ord_no), 9 | MAX(purch_amt) 10 | FROM orders 11 | GROUP BY customer_id 12 | ORDER BY 2 DESC; -------------------------------------------------------------------------------- /HACKERRANK/1. Types of Triangle.txt: -------------------------------------------------------------------------------- 1 | SELECT CASE 2 | WHEN A + B > C AND B + C > A AND A + C > B THEN 3 | CASE 4 | WHEN A = B AND B = C THEN 'Equilateral' 5 | WHEN A = B OR B = C OR A = C THEN 'Isosceles' 6 | ELSE 'Scalene' 7 | END 8 | ELSE 'Not A Triangle' 9 | END 10 | FROM TRIANGLES; 11 | -------------------------------------------------------------------------------- /HACKERRANK/10. African Cities.txt: -------------------------------------------------------------------------------- 1 | select city.Name from city Inner join country on countrycode=code where continent='Africa'; -------------------------------------------------------------------------------- /HACKERRANK/11. Population Census.txt: -------------------------------------------------------------------------------- 1 | select sum(city.population) from city Inner join country on countrycode = code where continent = 'Asia'; -------------------------------------------------------------------------------- /HACKERRANK/12. Average Population of Each Continent.txt: -------------------------------------------------------------------------------- 1 | select country.continent, floor(avg(city.population)) from city Inner join country on city.countrycode = country.code group by country.continent; -------------------------------------------------------------------------------- /HACKERRANK/13. The Report.txt: -------------------------------------------------------------------------------- 1 | SELECT IF(GRADE < 8, NULL, NAME), GRADE, MARKS FROM STUDENTS JOIN GRADES WHERE MARKS BETWEEN MIN_MARK AND MAX_MARK ORDER BY GRADE DESC, NAME; -------------------------------------------------------------------------------- /HACKERRANK/14. Top Competitors.txt: -------------------------------------------------------------------------------- 1 | select h.hacker_id, h.name 2 | from submissions s 3 | inner join challenges c 4 | on s.challenge_id = c.challenge_id 5 | inner join difficulty d 6 | on c.difficulty_level = d.difficulty_level 7 | inner join hackers h 8 | on s.hacker_id = h.hacker_id 9 | where s.score = d.score and c.difficulty_level = d.difficulty_level 10 | group by h.hacker_id, h.name 11 | having count(s.hacker_id) > 1 12 | order by count(s.hacker_id) desc, s.hacker_id asc -------------------------------------------------------------------------------- /HACKERRANK/15. Ollivander's Inventory.txt: -------------------------------------------------------------------------------- 1 | SELECT a.id, b.age, a.coins_needed, a.power 2 | FROM wands a JOIN wands_property b ON a.code = b.code 3 | WHERE b.is_evil = 0 AND a.coins_needed = (SELECT Min(a1.coins_needed) 4 | FROM wands a1 JOIN wands_property b1 ON a1.code = b1.code 5 | WHERE b.age = b1.age AND a.power = a1.power) 6 | ORDER BY a.power DESC, b.age DESC; -------------------------------------------------------------------------------- /HACKERRANK/16. Challenges.txt: -------------------------------------------------------------------------------- 1 | SELECT h.hacker_id, h.name, COUNT(c.challenge_id) AS c_count 2 | FROM Hackers h JOIN Challenges c ON c.hacker_id = h.hacker_id 3 | GROUP BY h.hacker_id, h.name 4 | HAVING c_count = (SELECT COUNT(c2.challenge_id) AS c_max 5 | FROM challenges as c2 GROUP BY c2.hacker_id 6 | ORDER BY c_max DESC limit 1) OR c_count IN 7 | (SELECT DISTINCT c_compare AS c_unique 8 | FROM (SELECT h2.hacker_id, h2.name, COUNT(challenge_id) AS c_compare FROM Hackers h2 JOIN Challenges c ON c.hacker_id = h2.hacker_id 9 | GROUP BY h2.hacker_id, h2.name) counts 10 | GROUP BY c_compare HAVING COUNT(c_compare) = 1) 11 | ORDER BY c_count DESC, h.hacker_id; -------------------------------------------------------------------------------- /HACKERRANK/17. Contest Leaderboard.txt: -------------------------------------------------------------------------------- 1 | select h.hacker_id, name, sum(score) as total_score 2 | from hackers as h inner join (select hacker_id, max(score) as score from submissions group by challenge_id, hacker_id) max_score on h.hacker_id=max_score.hacker_id 3 | group by h.hacker_id, name 4 | having total_score>0 5 | order by total_score desc, h.hacker_id; -------------------------------------------------------------------------------- /HACKERRANK/18. Draw The Triangle 1.txt: -------------------------------------------------------------------------------- 1 | set @number = 21; 2 | select repeat('* ', @number := @number - 1) from information_schema.tables; -------------------------------------------------------------------------------- /HACKERRANK/19. SQL Project Planning.txt: -------------------------------------------------------------------------------- 1 | set sql_mode = ''; 2 | select Start_Date, End_Date 3 | from 4 | (select Start_Date from Projects where Start_Date Not In (Select End_Date from Projects)) a, 5 | (select End_Date from Projects where End_Date Not in (Select Start_Date from Projects)) b 6 | where Start_Date < End_Date 7 | Group by Start_Date 8 | order by DATEDIFF(End_Date, Start_Date), Start_Date -------------------------------------------------------------------------------- /HACKERRANK/2. Weather Observation Station 19.txt: -------------------------------------------------------------------------------- 1 | SELECT 2 | ROUND(SQRT( 3 | POWER(MAX(LAT_N) - MIN(LAT_N), 2) 4 | + POWER(MAX(LONG_W) - MIN(LONG_W), 2) 5 | ), 4) 6 | FROM STATION; -------------------------------------------------------------------------------- /HACKERRANK/20. Placements.txt: -------------------------------------------------------------------------------- 1 | Select S.name 2 | From (Students S join Friends F Using(ID) 3 | join Packages P1 on S.ID=P1.ID 4 | join Packages P2 on F.Friend_ID=P2.ID) 5 | Where P2.Salary > P1.Salary 6 | Order By P2.Salary; -------------------------------------------------------------------------------- /HACKERRANK/21. Symmetric Pairs.txt: -------------------------------------------------------------------------------- 1 | SELECT A.x, A.y 2 | FROM FUNCTIONS A JOIN FUNCTIONS B ON 3 | A.x = B.y AND A.y = B.x 4 | GROUP BY A.x, A.y 5 | HAVING COUNT(A.x) > 1 OR A.x < A.y 6 | ORDER BY A.x -------------------------------------------------------------------------------- /HACKERRANK/22. Interviews.txt: -------------------------------------------------------------------------------- 1 | SELECT A.contest_id, A.hacker_id, A.Name, 2 | SUM(total_submissions) As total_submissions, 3 | SUM(total_accepted_submissions) AS total_accepted_submissions, 4 | SUM(total_views) AS total_views, 5 | SUM(total_unique_views) AS total_unique_views 6 | FROM Contests AS A 7 | LEFT JOIN Colleges AS B 8 | ON A.contest_id = B.contest_id 9 | LEFT JOIN Challenges AS C 10 | ON B.college_id = C.college_id 11 | LEFT JOIN (SELECT challenge_id, SUM(total_views) AS total_views, 12 | SUM(total_unique_views) AS total_unique_views 13 | FROM View_Stats 14 | GROUP BY challenge_id) AS D 15 | ON C.challenge_id = D.challenge_id 16 | LEFT JOIN (SELECT challenge_id, SUM(total_submissions) AS total_submissions, 17 | SUM(total_accepted_submissions) AS total_accepted_submissions 18 | FROM Submission_Stats 19 | GROUP BY challenge_id) AS E ON C.challenge_id = E.challenge_id 20 | GROUP BY A.contest_id, A.hacker_id, A.Name 21 | HAVING (total_submissions + total_accepted_submissions + total_views + total_unique_views) > 0 22 | ORDER BY A.contest_id; -------------------------------------------------------------------------------- /HACKERRANK/23. 15 Days of Learning SQL.txt: -------------------------------------------------------------------------------- 1 | select 2 | submission_date , 3 | 4 | ( SELECT COUNT(distinct hacker_id) 5 | FROM Submissions s2 6 | WHERE s2.submission_date = s1.submission_date AND (SELECT COUNT(distinct s3.submission_date) FROM Submissions s3 WHERE s3.hacker_id = s2.hacker_id AND s3.submission_date < s1.submission_date) = dateDIFF(s1.submission_date , '2016-03-01')) , 7 | 8 | (select hacker_id from submissions s2 where s2.submission_date = s1.submission_date 9 | group by hacker_id order by count(submission_id) desc , hacker_id limit 1) as shit, 10 | (select name from hackers where hacker_id = shit) 11 | from 12 | (select distinct submission_date from submissions) s1 13 | group by submission_date -------------------------------------------------------------------------------- /HACKERRANK/24. Draw The Triangle 2.txt: -------------------------------------------------------------------------------- 1 | set @row := 0; 2 | select repeat('* ', @row := @row + 1) 3 | from information_schema.tables where @row < 20 -------------------------------------------------------------------------------- /HACKERRANK/25. Print Prime Numbers.txt: -------------------------------------------------------------------------------- 1 | SELECT GROUP_CONCAT(NUMB SEPARATOR '&') 2 | FROM ( 3 | SELECT @num:=@num+1 as NUMB FROM 4 | information_schema.tables t1, 5 | information_schema.tables t2, 6 | (SELECT @num:=1) tmp 7 | ) tempNum 8 | WHERE NUMB<=1000 AND NOT EXISTS( 9 | SELECT * FROM ( 10 | SELECT @nu:=@nu+1 as NUMA FROM 11 | information_schema.tables t1, 12 | information_schema.tables t2, 13 | (SELECT @nu:=1) tmp1 14 | LIMIT 1000 15 | ) tatata 16 | WHERE FLOOR(NUMB/NUMA)=(NUMB/NUMA) AND NUMA1 17 | ) -------------------------------------------------------------------------------- /HACKERRANK/3. Weather Observation Station 13.txt: -------------------------------------------------------------------------------- 1 | Select cast(sum(LAT_N) as decimal(9, 4)) 2 | from station where LAT_N>38.7880 and LAT_N<137.2357; -------------------------------------------------------------------------------- /HACKERRANK/4. Weather Observation Station 14.txt: -------------------------------------------------------------------------------- 1 | select cast(Max(LAT_N)as decimal(9,4))from station where LAT_N<137.2345; 2 | 3 | 4 | -------------------------------------------------------------------------------- /HACKERRANK/5. Weather Observation Station 15.txt: -------------------------------------------------------------------------------- 1 | Select CAST(LONG_W AS DECIMAL(10,4)) from STATION WHERE LAT_N = (SELECT MAX(LAT_N) FROM STATION WHERE LAT_N<137.2345); -------------------------------------------------------------------------------- /HACKERRANK/6. Weather Observation Station 16.txt: -------------------------------------------------------------------------------- 1 | SELECT CAST(MIN(LAT_N) AS DECIMAL(10,4)) FROM STATION WHERE LAT_N>38.7780; -------------------------------------------------------------------------------- /HACKERRANK/7. Weather Observation Station 17.txt: -------------------------------------------------------------------------------- 1 | SELECT ROUND(LONG_W,4) FROM STATION WHERE LAT_N IN (SELECT MIN(LAT_N) FROM STATION WHERE LAT_N>38.7780); -------------------------------------------------------------------------------- /HACKERRANK/8. Weather Observation Station 18.txt: -------------------------------------------------------------------------------- 1 | SELECT ROUND( MAX(lat_n)-MIN(lat_n) + MAX(long_w)-MIN(long_w), 4) FROM Station; -------------------------------------------------------------------------------- /HACKERRANK/9. Weather Observation Station 20.txt: -------------------------------------------------------------------------------- 1 | select round(s.lat_n,4) from station s where (select round(count(s.id)/2)-1 2 | from station) =(select count(s1.id)from station s1 where s1.lat_n >s.lat_n); -------------------------------------------------------------------------------- /HR Database - SORT FILTER/1. Find those employees who earn below 6000.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees whose 2 | salaries are less than 6000. Return full name (first and last name), and salary. 3 | 4 | Ans--> 5 | 6 | SELECT first_name ||' '||last_name AS Full_Name, salary 7 | FROM employees 8 | WHERE salary < 6000; 9 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/10. Employees whose first name is ending with the letter m.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the employees whose first name ends with the letter ‘m’. Return the first and last name, and salary. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name, salary 6 | FROM employees 7 | WHERE first_name LIKE '%m'; 8 | 9 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/11. Employees whose salary is out of a given range.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees whose salaries are not between 7000 and 15000 (Begin and end values are included). Sort the result-set in ascending order by the full name (first and last). Return full name and salary.SELECT first_name || ' ' || last_name as Name, salary 2 | FROM employees 3 | WHERE salary NOT BETWEEN 7000 AND 15000 4 | ORDER BY first_name || ' ' || last_name; 5 | 6 | 7 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/12. Employees who was hired during given dates.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who were hired between November 5th, 2007 and July 5th, 2009. Return full name (first and last), job id and hire date. 2 | 3 | Ans--> 4 | 5 | SELECT first_name ||' '||last_name AS Full_Name, 6 | job_id, hire_date 7 | FROM employees 8 | WHERE hire_date 9 | BETWEEN '2007-11-05' AND '2009-07-05'; 10 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/13. Find employees who works either in department 70 or 90.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who 2 | work either in department 70 or 90. Return full name (first and last name), 3 | department id. 4 | 5 | Ans--> 6 | 7 | SELECT first_name ||' '|| last_name AS Full_Name, department_id 8 | FROM employees 9 | WHERE department_id = 70 10 | OR department_id = 90; 11 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/14. Find those employees who is working under a manager.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who work under a manager. Return full name (first and last name), salary, and manager ID. 2 | 3 | Ans--> 4 | 5 | SELECT first_name ||' '||last_name AS Full_Name, salary, manager_id 6 | FROM employees 7 | WHERE manager_id IS NOT NULL; 8 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/15. Find those employees hired before June 21st, 2002.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the employees who were 2 | hired before June 21st, 2002. Return all fields. 3 | 4 | Ans--> 5 | 6 | SELECT * 7 | FROM employees 8 | WHERE hire_date < '2002-06-21'; 9 | 10 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/16. Find employees whose managers are hold given IDs.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the employees whose managers hold the ID 120, 103, or 145. Return first name, last name, email, salary and manager ID. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name, email, 6 | salary, manager_id 7 | FROM employees 8 | WHERE manager_id IN (120 , 103 , 145); 9 | 10 | 11 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/17. Employees have the given letters in their first name.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find employees whose first names contain the letters D, S, or N. Sort the result-set in descending order by salary. Return all fields. 2 | 3 | Ans--> 4 | 5 | SELECT * 6 | FROM employees 7 | WHERE first_name LIKE '%D%' 8 | OR first_name LIKE '%S%' 9 | OR first_name LIKE '%N%' 10 | ORDER BY salary DESC; 11 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/18. Employees earn over 11000 or 7th digit in their phone.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who earn above 11000 or the seventh character in their phone number is 3. Sort the result-set in descending order by first name. Return full name (first name and last name), hire date, commission percentage, email, and telephone separated by '-', and salary. 2 | 3 | Ans--> 4 | 5 | SELECT first_name ||' '||last_name AS Full_Name, hire_date , 6 | commission_pct, email ||' - '||phone_number AS Contact_Details, salary 7 | FROM employees 8 | WHERE salary > 11000 9 | OR phone_number LIKE '______3%' 10 | ORDER BY first_name DESC; 11 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/19. Employees with s as their 3rd character in first name.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees whose first name contains a character 's' in the third position. Return first name, last name and department id. 2 | 3 | Ans--> 4 | 5 | SELECT first_name,last_name, department_id 6 | FROM employees 7 | WHERE first_name LIKE '__s%'; 8 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/2. Find those employees who earn more than 8000.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees whose salary is higher than 8000. Return first name, last name and department number and salary. 2 | 3 | Ans--> 4 | 5 | SELECT first_name,last_name, department_id, salary 6 | FROM employees 7 | WHERE salary > 8000; 8 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/20. Find employees who is working except given departments.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees work in the departments that are not part of the department 50 or 30 or 80. Return employee_id, first_name,job_id, department_id. 2 | 3 | Ans--> 4 | 5 | SELECT employee_id, first_name, job_id, department_id 6 | FROM employees 7 | WHERE department_id NOT IN (50, 30, 80); 8 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/21. Find employees who is working given departments.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the employees whose department numbers are included in 30, 40, or 90. Return employee id, first name, job id, department id. 2 | 3 | 4 | Ans--> 5 | 6 | SELECT employee_id, first_name, job_id, department_id 7 | FROM employees 8 | WHERE department_id IN (30 , 40 , 90); 9 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/22. Find employees who did two or more jobs in the past.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who worked more than two jobs in the past. Return employee id. 2 | 3 | Ans--> 4 | 5 | SELECT employee_id 6 | FROM job_history 7 | GROUP BY employee_id 8 | HAVING COUNT(*) >=2; 9 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/23. Difference between highest and lowest salary for a job.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to count the number of employees, the sum of all salary, and difference between the highest salary and lowest salaries by each job id. Return job_id, count, sum, salary_difference. 2 | 3 | Ans--> 4 | 5 | SELECT job_id, COUNT(*), SUM(salary), 6 | MAX(salary)-MIN(salary) AS salary_difference 7 | FROM employees 8 | GROUP BY job_id; 9 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/24. Jobs done by two or more for more than 300 days.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find each job ids where two or more employees worked for more than 300 days. Return job id. 2 | 3 | Ans--> 4 | 5 | SELECT job_id 6 | FROM job_history 7 | WHERE end_date-start_date >300 8 | GROUP BY job_id 9 | HAVING COUNT(*)>=2; 10 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/25. Country ID and number of cities in country has.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to count the number of cities in each country. Return country ID and number of cities. 2 | 3 | Ans--> 4 | 5 | SELECT country_id, COUNT(*) 6 | FROM locations 7 | GROUP BY country_id; 8 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/26. Find employees managed by the manager.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to count the number of employees worked under each manager. Return manager ID and number of employees. 2 | 3 | 4 | Ans--> 5 | 6 | SELECT manager_id, COUNT(*) 7 | FROM employees 8 | GROUP BY manager_id; 9 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/27. Details of jobs in descending order by title.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find all jobs. Sort the result-set in descending order by job title. Return all fields. 2 | Ans--> 3 | 4 | SELECT * 5 | FROM jobs 6 | ORDER BY job_title DESC; 7 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/28. Employees who are Sales Representatives or Sales Men.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find all those employees who are either Sales Representatives or Salesmen. Return first name, last name and hire date. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name, hire_date 6 | FROM employees 7 | WHERE job_id IN ('SA_REP', 'SA_MAN'); 8 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/29. Average salary of each commission-based department.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to calculate the average salary of employees who receive a commission percentage for each department. Return department id, average salary. 2 | 3 | Ans--> 4 | 5 | SELECT department_id, AVG(salary) 6 | FROM employees 7 | WHERE commission_pct IS NOT NULL 8 | GROUP BY department_id; 9 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/3. Display Employees whose last name is McEwen.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees whose 2 | last name is "McEwen". Return first name, last name and department ID. 3 | 4 | Ans--> 5 | 6 | SELECT first_name, last_name, department_id 7 | FROM employees 8 | WHERE last_name = 'McEwen'; 9 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/30. Departments with managers manage more than 4 employees.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the departments where any manager manages four or more employees. Return department_id. 2 | 3 | Ans--> 4 | 5 | SELECT DISTINCT department_id 6 | FROM employees 7 | GROUP BY department_id, manager_id 8 | HAVING COUNT(employee_id) >=4; 9 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/31. Departments more than 10 employees who get commissions.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the departments where more than ten employees receive commissions. Return department id. 2 | 3 | Ans--> 4 | 5 | SELECT department_id 6 | FROM employees 7 | WHERE commission_pct IS NOT NULL 8 | GROUP BY department_id 9 | HAVING COUNT(commission_pct)>10; 10 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/32. The date when he left his last job and his employee ID.txt: -------------------------------------------------------------------------------- 1 | Q.From the following table, write a SQL query to find those employees who have completed their previous jobs. Return employee ID, end_date. 2 | 3 | Ans--> 4 | 5 | SELECT employee_id , MAX(end_date) 6 | FROM job_history 7 | WHERE employee_id IN (SELECT employee_id 8 | FROM job_history 9 | GROUP BY 1 10 | HAVING COUNT(employee_id) > 1) 11 | GROUP BY 1 12 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/33. A department 50 employee without a commission %.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who do not have commission percentage and have salaries between 7000, 12000 (Begin and end values are included.) and who are employed in the department number 50. Return all the fields of employees. 2 | 3 | Ans--> 4 | 5 | SELECT * 6 | FROM employees 7 | WHERE commission_pct IS NULL 8 | AND salary BETWEEN 7000 AND 12000 9 | AND department_id=50; 10 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/34. Jobs which average salary is above 8000.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to compute the average salary of each job ID. Exclude those records where average salary is higher than 8000. Return job ID, average salary. 2 | 3 | Ans--> 4 | 5 | SELECT job_id, AVG(salary) 6 | FROM employees 7 | GROUP BY job_id 8 | HAVING AVG(salary)>8000; 9 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/35. Salary ranges for jobs with a minimum and maximum.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those job titles where maximum salary falls between 12000 and 18000 (Begin and end values are included.). Return job_title, max_salary-min_salary. 2 | 3 | Ans--> 4 | 5 | SELECT job_title, max_salary-min_salary AS salary_differences 6 | FROM jobs 7 | WHERE max_salary BETWEEN 12000 AND 18000; 8 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/36. Employees whose first or last name begins with D.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the employees whose first or last name begins with 'D'. Return first name, last name. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name 6 | FROM employees 7 | WHERE first_name LIKE 'D%' 8 | OR last_name LIKE 'D%'; 9 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/37. Display jobs with minimum salaries over 9000.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find details of those jobs where the minimum salary exceeds 9000. Return all the fields of jobs. 2 | 3 | Ans--> 4 | 5 | SELECT * 6 | FROM jobs 7 | WHERE min_salary > 9000; 8 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/38. Employees who joined after 7th September, 1987.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who joined after 7th September 1987. Return all the fields. 2 | 3 | Ans--> 4 | 5 | SELECT * 6 | FROM employees 7 | WHERE hire_date > '1987-09-07'; 8 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/4. Details of employees without any department number.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to identify employees who do 2 | not have a department number. Return employee_id, first_name, last_name, 3 | email, phone_number, hire_date, job_id, salary,commission_pct, manager_id 4 | and department_id. 5 | 6 | Ans--> 7 | 8 | SELECT * 9 | FROM employees 10 | WHERE department_id IS NULL; 11 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/5. Information about the department Marketing.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the details of 'Marketing' department. 2 | Return all fields. 3 | 4 | Ans--> 5 | 6 | SELECT * 7 | FROM departments 8 | WHERE department_name = 'Marketing'; 9 | 10 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/6. Employees first name does not contain the letter M.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees whose first name does not contain the letter ‘M’. Sort the result-set in ascending order by department ID. Return full name (first and last name together), hire_date, salary and department_id. 2 | 3 | Ans--> 4 | 5 | SELECT first_name || ' ' || last_name as Full_Name, hire_date, 6 | salary, department_id 7 | FROM employees 8 | WHERE first_name NOT LIKE '%M%' 9 | ORDER BY department_id; 10 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/7. Employees with salaries of given range and commission.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who 2 | earn between 8000 and 12000 (Begin and end values are included.) and get some 3 | commission. These employees joined before ‘1987-06-05’ and were not included 4 | in the department numbers 40, 120 and 70. Return all fields. 5 | 6 | Ans--> 7 | 8 | SELECT * 9 | FROM employees 10 | WHERE salary BETWEEN 8000 AND 12000 11 | AND commission_pct IS NOT NULL 12 | OR department_id NOT IN (40 , 120 , 70) 13 | AND hire_date < '2003-06-05' 14 | 15 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/8. Employees who does not earn any commission.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who do 2 | not earn any commission. Return full name (first and last name), and salary. 3 | 4 | Ans--> 5 | 6 | SELECT first_name ||' '||last_name AS Full_Name, salary 7 | FROM employees 8 | WHERE commission_pct IS NULL; 9 | 10 | -------------------------------------------------------------------------------- /HR Database - SORT FILTER/9. Find employees whose salary is within 9000 to 17000.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the employees whose salary is in the range 9000,17000 (Begin and end values are included). Return full name, contact details and salary. 2 | 3 | Ans--> 4 | 5 | SELECT first_name ||' '||last_name AS Full_Name, 6 | phone_number ||' - '|| email AS Contact_Details, 7 | salary AS Remuneration 8 | FROM employees 9 | WHERE salary BETWEEN 9000 AND 17000; 10 | -------------------------------------------------------------------------------- /Query on Multiple Tables/1. Customer and salesmen who lives in the same city.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find the salespeople and 2 | customers who live in the same city. Return customer name, salesperson name 3 | and salesperson city. 4 | 5 | Ans--> 6 | 7 | SELECT customer.cust_name, 8 | salesman.name, salesman.city 9 | FROM salesman, customer 10 | WHERE salesman.city = customer.city; 11 | -------------------------------------------------------------------------------- /Query on Multiple Tables/2. Customers along with the salesmen who works for them.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to locate all the customers 2 | and the salesperson who works for them. Return customer name, and salesperson 3 | name. 4 | 5 | Ans--> 6 | 7 | SELECT customer.cust_name, salesman.name 8 | FROM customer,salesman 9 | WHERE salesman.salesman_id = customer.salesman_id; 10 | -------------------------------------------------------------------------------- /Query on Multiple Tables/3. Customer lives in a city other than the salesman's.txt: -------------------------------------------------------------------------------- 1 | Q.From the following tables, write a SQL query to find those salespeople who generated 2 | orders for their customers but are not located in the same city. 3 | Return ord_no, cust_name, customer_id (orders table), salesman_id (orders table). 4 | 5 | Ans--> 6 | 7 | SELECT ord_no, cust_name, orders.customer_id, orders.salesman_id 8 | FROM salesman, customer, orders 9 | WHERE customer.city <> salesman.city 10 | AND orders.customer_id = customer.customer_id 11 | AND orders.salesman_id = salesman.salesman_id; 12 | 13 | -------------------------------------------------------------------------------- /Query on Multiple Tables/4. Find out customers who made the order.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to locate the orders made by customers. Return order number, customer name. 2 | 3 | Ans--> 4 | 5 | SELECT orders.ord_no, customer.cust_name 6 | FROM orders, customer 7 | WHERE orders.customer_id = customer.customer_id; -------------------------------------------------------------------------------- /Query on Multiple Tables/5. Sort out the customer who made at least an order.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find those customers 2 | where each customer has a grade and is served by a salesperson who belongs 3 | to a city. Return cust_name as "Customer", grade as "Grade". 4 | 5 | Ans--> 6 | 7 | select customer.cust_name as "Customer", 8 | customer.grade as "Grade",orders.ord_no AS "Order No." 9 | from orders,salesman,customer 10 | where orders.customer.id=customer.customer.id 11 | AND orders.salesman_id = salesman.salesman_id 12 | AND salesman.city IS NOT NULL 13 | AND customer.grade IS NOT NULL; -------------------------------------------------------------------------------- /Query on Multiple Tables/6. Customers served by a salesman and commission.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those customers who 2 | are served by a salesperson and the salesperson earns commission in the 3 | range of 12% to 14% (Begin and end values are included.). Return cust_name 4 | AS "Customer", city AS "City". 5 | 6 | Ans--> 7 | 8 | SELECT customer.cust_name AS "Customer", 9 | customer.city AS "City", 10 | salesman.name AS "Salesman", 11 | salesman.commission 12 | FROM customer,salesman 13 | WHERE customer.salesman_id = salesman.salesman_id 14 | AND salesman.commission 15 | BETWEEN .12 AND .14; 16 | -------------------------------------------------------------------------------- /Query on Multiple Tables/7. Find salesman commission details of given customer.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find all orders executed 2 | by the salesperson and ordered by the customer whose grade is greater than or 3 | equal to 200. Compute purch_amt*commission as “Commission”. Return customer 4 | name, commission as “Commission%” and Commission. 5 | 6 | Ans--> 7 | 8 | SELECT ord_no, cust_name, commission AS "Commission%", 9 | purch_amt*commission AS "Commission" 10 | FROM salesman,orders,customer 11 | WHERE orders.customer_id = customer.customer_id 12 | AND orders.salesman_id = salesman.salesman_id 13 | AND customer.grade>=200; -------------------------------------------------------------------------------- /Query on Multiple Tables/8. Find all customers with orders on October 5, 2012.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those customers who 2 | placed orders on October 5, 2012. Return customer_id, cust_name, city, grade, 3 | salesman_id, ord_no, purch_amt, ord_date, customer_id and salesman_id. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM customer a,orders b 9 | WHERE a.customer_id=b.customer_id 10 | AND b.ord_date='2012-10-05'; 11 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/1. Display the name, department number, for each employee.txt: -------------------------------------------------------------------------------- 1 | 1. From the following tables, write a SQL query to find the first name, last name, department number, and department name for each employee. 2 | 3 | Ans--> 4 | 5 | SELECT E.first_name , E.last_name , 6 | E.department_id , D.department_name 7 | FROM employees E 8 | JOIN departments D 9 | ON E.department_id = D.department_id; 10 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/10. Display employees who have or have not any department.txt: -------------------------------------------------------------------------------- 1 | 10. From the following tables, write a SQL query to find out which employees have or do not have a department. Return first name, last name, department ID, department name. 2 | 3 | Ans--> 4 | 5 | SELECT E.first_name, E.last_name, E.department_id, D.department_name 6 | FROM employees E 7 | LEFT OUTER JOIN departments D 8 | ON E.department_id = D.department_id; 9 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/11. Display employees and their manager.txt: -------------------------------------------------------------------------------- 1 | 11. From the following table, write a SQL query to find the employees and their managers. Those managers do not work under any manager also appear in the list. Return the first name of the employee and manager. 2 | 3 | Ans--> 4 | 5 | SELECT E.first_name AS "Employee Name", 6 | M.first_name AS "Manager" 7 | FROM employees E 8 | LEFT OUTER JOIN employees M 9 | ON E.manager_id = M.employee_id; 10 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/12. Employees who work in the same department as Taylor.txt: -------------------------------------------------------------------------------- 1 | 12. From the following tables, write a SQL query to find the employees who work in the same department as the employee with the last name Taylor. Return first name, last name and department ID. 2 | 3 | Ans--> 4 | 5 | SELECT E.first_name, E.last_name, E.department_id 6 | FROM employees E 7 | JOIN employees S 8 | ON E.department_id = S.department_id 9 | AND S.last_name = 'Taylor'; 10 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/13. Jobs which started between two given dates.txt: -------------------------------------------------------------------------------- 1 | 13. From the following tables, write a SQL query to find all employees who joined on or after 1st January 1993 and on or before 31 August 1997. Return job title, department name, employee name, and joining date of the job. 2 | 3 | Ans--> 4 | 5 | SELECT job_title, department_name, first_name || ' ' || last_name AS Employee_name, start_date 6 | FROM job_history 7 | JOIN jobs USING (job_id) 8 | JOIN departments USING (department_id) 9 | JOIN employees USING (employee_id) 10 | WHERE start_date>='1993-01-01' AND start_date<='1997-08-31'; 11 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/14. Salary difference between jobs maximum and employees.txt: -------------------------------------------------------------------------------- 1 | 14. From the following tables, write a SQL query to calculate the difference between the maximum salary of the job and the employee's salary. Return job title, employee name, and salary difference. 2 | 3 | Ans--> 4 | 5 | SELECT job_title, first_name || ' ' || last_name AS Employee_name, 6 | max_salary-salary AS salary_difference 7 | FROM employees 8 | NATURAL JOIN jobs; 9 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/15. Employees working in the department who got commission.txt: -------------------------------------------------------------------------------- 1 | 15. From the following table, write a SQL query to calculate the average salary, the number of employees receiving commissions in that department. Return department name, average salary and number of employees. 2 | 3 | Ans--> 4 | 5 | SELECT department_name, AVG(salary), COUNT(commission_pct) 6 | FROM departments 7 | JOIN employees USING (department_id) 8 | GROUP BY department_name; 9 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/16. Salary differences of employees in the department 80.txt: -------------------------------------------------------------------------------- 1 | 16. From the following tables, write a SQL query to calculate the difference between the maximum salary and the salary of all the employees who work in the department of ID 80. Return job title, employee name and salary difference. 2 | 3 | Ans--> 4 | 5 | SELECT job_title, first_name || ' ' || last_name AS Employee_name, 6 | max_salary-salary AS salary_difference 7 | FROM employees 8 | NATURAL JOIN jobs 9 | WHERE department_id = 80; 10 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/17. Display country, city, and the departments.txt: -------------------------------------------------------------------------------- 1 | 17. From the following table, write a SQL query to find the name of the country, city, and departments, which are running there. 2 | 3 | Ans--> 4 | 5 | SELECT country_name,city, department_name 6 | FROM countries 7 | JOIN locations USING (country_id) 8 | JOIN departments USING (location_id); 9 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/18. Department name and the full name of the manager.txt: -------------------------------------------------------------------------------- 1 | 18. From the following tables, write a SQL query to find the department name and the full name (first and last name) of the manager. 2 | 3 | Ams--> 4 | 5 | SELECT department_name, first_name || ' ' || last_name AS name_of_manager 6 | FROM departments D 7 | JOIN employees E 8 | ON (D.manager_id=E.employee_id); 9 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/19. Display the job title and average salary of employees.txt: -------------------------------------------------------------------------------- 1 | 19. From the following table, write a SQL query to calculate the average salary of employees for each job title. 2 | 3 | Ans--> 4 | 5 | SELECT job_title, AVG(salary) 6 | FROM employees 7 | NATURAL JOIN jobs 8 | GROUP BY job_title; 9 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/2. Display name, department, city and state each employee.txt: -------------------------------------------------------------------------------- 1 | 2. From the following tables, write a SQL query to find the first name, last name, department, city, and state province for each employee. 2 | 3 | Ans--> 4 | 5 | SELECT E.first_name,E.last_name, 6 | D.department_name, L.city, L.state_province 7 | FROM employees E 8 | JOIN departments D 9 | ON E.department_id = D.department_id 10 | JOIN locations L 11 | ON D.location_id = L.location_id; 12 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/20. Employees who earn a salary over 12000.txt: -------------------------------------------------------------------------------- 1 | 20. From the following table, write a SQL query to find the employees who earn $12000 or more. Return employee ID, starting date, end date, job ID and department ID. 2 | 3 | Ans--> 4 | 5 | SELECT a.* 6 | FROM job_history a 7 | JOIN employees m 8 | ON (a.employee_id = m.employee_id) 9 | WHERE salary >= 12000; 10 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/21. Departments where at least 2 employees are working.txt: -------------------------------------------------------------------------------- 1 | 21. From the following tables, write a SQL query to find out which departments have at least two employees. Group the result set on country name and city. Return country name, city, and number. 2 | 3 | Ans--> 4 | 5 | SELECT country_name,city, COUNT(department_id) 6 | FROM countries 7 | JOIN locations USING (country_id) 8 | JOIN departments USING (location_id) 9 | WHERE department_id IN 10 | (SELECT department_id 11 | FROM employees 12 | GROUP BY department_id 13 | HAVING COUNT(department_id)>=2) 14 | GROUP BY country_name,city; 15 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/22. Display the department, manager name, and their city.txt: -------------------------------------------------------------------------------- 1 | 22. From the following tables, write a SQL query to find the department name, full name (first and last name) of the manager and their city. 2 | 3 | Ans--> 4 | 5 | SELECT department_name, first_name || ' ' || last_name AS name_of_manager, city 6 | FROM departments D 7 | JOIN employees E 8 | ON (D.manager_id=E.employee_id) 9 | JOIN locations L USING (location_id); 10 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/23. Number of days worked for all jobs in department 80.txt: -------------------------------------------------------------------------------- 1 | 23. From the following tables, write a SQL query to calculate the number of days worked by employees in a department of ID 80. Return employee ID, job title, number of days worked. 2 | 3 | Ans--> 4 | 5 | SELECT employee_id, job_title, end_date-start_date DAYS 6 | FROM job_history 7 | NATURAL JOIN jobs 8 | WHERE department_id=80; 9 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/24. Employees worked without a commission percentage.txt: -------------------------------------------------------------------------------- 1 | 25. From the following tables, write a SQL query to find full name (first and last name), job title, start and end date of last jobs of employees who did not receive commissions. 2 | 3 | Ans--> 4 | 5 | 6 | SELECT CONCAT(e.first_name, ' ', e.last_name) AS Employee_name, 7 | j.job_title, 8 | h.* 9 | FROM employees e 10 | JOIN 11 | (SELECT MAX(start_date), 12 | MAX(end_date), 13 | employee_id 14 | FROM job_history 15 | GROUP BY employee_id) h ON e.employee_id=h.employee_id 16 | JOIN jobs j ON j.job_id=e.job_id 17 | WHERE e.commission_pct = 0; 18 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/25. Employees who work in any department located in London.txt: -------------------------------------------------------------------------------- 1 | 24. From the following tables, write a SQL query to find full name (first and last name), and salary of all employees working in any department in the city of London. 2 | 3 | Ans--> 4 | 5 | SELECT first_name || ' ' || last_name AS Employee_name, salary 6 | FROM employees 7 | JOIN departments USING (department_id) 8 | JOIN locations USING (location_id) 9 | WHERE city = 'London'; 10 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/26. Number of employees in each of the department.txt: -------------------------------------------------------------------------------- 1 | 26. From the following tables, write a SQL query to find the department name, department ID, and number of employees in each department. 2 | 3 | Ans--> 4 | 5 | SELECT d.department_name, 6 | e.* 7 | FROM departments d 8 | JOIN 9 | (SELECT count(employee_id), 10 | department_id 11 | FROM employees 12 | GROUP BY department_id) e USING (department_id);; 13 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/27. Display the employee with ID and present country.txt: -------------------------------------------------------------------------------- 1 | 27. From the following tables, write a SQL query to find out the full name (first and last name) of the employee with an ID and the name of the country where he/she is currently employed. 2 | 3 | Ans--> 4 | 5 | SELECT first_name || ' ' || last_name 6 | AS Employee_name, employee_id, country_name 7 | FROM employees 8 | JOIN departments 9 | USING(department_id) 10 | JOIN locations 11 | USING( location_id) 12 | JOIN countries 13 | USING ( country_id); 14 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/3. List all employees names, salaries, and job grades.txt: -------------------------------------------------------------------------------- 1 | 3. From the following table, write a SQL query to find the first name, last name, salary, and job grade for all employees. 2 | 3 | Ans--> 4 | 5 | SELECT E.first_name, E.last_name, E.salary, J.grade_level 6 | FROM employees E 7 | JOIN job_grades J 8 | ON E.salary BETWEEN J.lowest_sal AND J.highest_sal; 9 | 10 | 11 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/4. Display all employees for departments 80 or 40.txt: -------------------------------------------------------------------------------- 1 | 4. From the following tables, write a SQL query to find all those employees who work in department ID 80 or 40. Return first name, last name, department number and department name. 2 | 3 | Ans--> 4 | 5 | SELECT E.first_name , E.last_name , 6 | E.department_id , D.department_name 7 | FROM employees E 8 | JOIN departments D 9 | ON E.department_id = D.department_id 10 | AND E.department_id IN (80 , 40) 11 | ORDER BY E.last_name; 12 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/5. Employees who contain a letter z to their first name.txt: -------------------------------------------------------------------------------- 1 | 5. From the following tables, write a SQL query to find those employees whose first name contains the letter ‘z’. Return first name, last name, department, city, and state province. 2 | 3 | Ans--> 4 | 5 | SELECT E.first_name,E.last_name, 6 | D.department_name, L.city, L.state_province 7 | FROM employees E 8 | JOIN departments D 9 | ON E.department_id = D.department_id 10 | JOIN locations L 11 | ON D.location_id = L.location_id 12 | WHERE E.first_name LIKE '%z%'; 13 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/6. Departments which does not have any employee.txt: -------------------------------------------------------------------------------- 1 | 6. From the following tables, write a SQL query to find all departments, including those without employees. Return first name, last name, department ID, department name. 2 | 3 | Ans--> 4 | 5 | SELECT E.first_name, E.last_name, D.department_id, D.department_name 6 | FROM employees E 7 | RIGHT OUTER JOIN departments D 8 | ON E.department_id = D.department_id; 9 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/7. Employees who earn less than employee 182.txt: -------------------------------------------------------------------------------- 1 | 7. From the following table, write a SQL query to find the employees who earn less than the employee of ID 182. Return first name, last name and salary. 2 | 3 | Ans--> 4 | 5 | SELECT E.first_name, E.last_name, E.salary 6 | FROM employees E 7 | JOIN employees S 8 | ON E.salary < S.salary 9 | AND S.employee_id = 182; 10 | 11 | 12 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/8. Employees including the first name of their manager.txt: -------------------------------------------------------------------------------- 1 | 8. From the following table, write a SQL query to find the employees and their managers. Return the first name of the employee and manager. 2 | 3 | Ans--> 4 | 5 | SELECT E.first_name AS "Employee Name", 6 | M.first_name AS "Manager" 7 | FROM employees E 8 | JOIN employees M 9 | ON E.manager_id = M.employee_id; 10 | 11 | -------------------------------------------------------------------------------- /SQL JOINS on HR Database/9. Display the department name, city for each department.txt: -------------------------------------------------------------------------------- 1 | 9. From the following tables, write a SQL query to display the department name, city, and state province for each department. 2 | 3 | Ans--> 4 | 5 | SELECT D.department_name , L.city , L.state_province 6 | FROM departments D 7 | JOIN locations L 8 | ON D.location_id = L.location_id; 9 | 10 | -------------------------------------------------------------------------------- /SQL JOINS/1. Salesmen and customer, who belongs to same city.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find the 2 | salesperson and customer who reside in the same city. Return Salesman, 3 | cust_name and city. 4 | 5 | 6 | Ans--> 7 | 8 | SELECT salesman.name AS "Salesman", 9 | customer.cust_name, customer.city 10 | FROM salesman,customer 11 | WHERE salesman.city=customer.city; 12 | -------------------------------------------------------------------------------- /SQL JOINS/10. Customer have placed no order or one or more orders.txt: -------------------------------------------------------------------------------- 1 | Q. Write a SQL statement to make a report with customer name, city, order 2 | number, order date, and order amount in ascending order according to the 3 | order date to determine whether any of the existing customers have placed 4 | an order or not. 5 | 6 | Ans--> 7 | 8 | SELECT a.cust_name,a.city, b.ord_no, 9 | b.ord_date,b.purch_amt AS "Order Amount" 10 | FROM customer a 11 | LEFT OUTER JOIN orders b 12 | ON a.customer_id=b.customer_id 13 | order by b.ord_date; 14 | -------------------------------------------------------------------------------- /SQL JOINS/11. Find customers who have placed no order or one or more.txt: -------------------------------------------------------------------------------- 1 | Q. SQL statement to generate a report with customer name, city, order number, order date, order amount, salesperson name, and commission to determine 2 | if any of the existing customers have not placed orders or if they have placed orders through their salesman or by themselves. 3 | 4 | Ans--> 5 | 6 | SELECT a.cust_name,a.city, b.ord_no, 7 | b.ord_date,b.purch_amt AS "Order Amount", 8 | c.name,c.commission 9 | FROM customer a 10 | LEFT OUTER JOIN orders b 11 | ON a.customer_id=b.customer_id 12 | LEFT OUTER JOIN salesman c 13 | ON c.salesman_id=b.salesman_id; 14 | -------------------------------------------------------------------------------- /SQL JOINS/12. Salesmen works either for one or more customer or not.txt: -------------------------------------------------------------------------------- 1 | Q. Write a SQL statement to generate a list in ascending order of salespersons who work 2 | either for one or more customers or have not yet joined any of the customers. 3 | 4 | 5 | Ans--> 6 | 7 | SELECT a.cust_name,a.city,a.grade, 8 | b.name AS "Salesman", b.city 9 | FROM customer a 10 | RIGHT OUTER JOIN salesman b 11 | ON b.salesman_id=a.salesman_id 12 | ORDER BY b.salesman_id; 13 | -------------------------------------------------------------------------------- /SQL JOINS/13. Salesmen who works either for one or more customer.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to list all salespersons along with customer name, 2 | city, grade, order number, date, and amount. Condition for selecting list of salesmen : 1. Salesmen 3 | who works for one or more customer or, 2. Salesmen who not yet join under any customer, Condition for 4 | selecting list of customer : 3. placed one or more orders, or 4. no order placed to their salesman. 5 | 6 | Ans--> 7 | 8 | SELECT a.cust_name,a.city,a.grade, 9 | b.name AS "Salesman", 10 | c.ord_no, c.ord_date, c.purch_amt 11 | FROM customer a 12 | RIGHT OUTER JOIN salesman b 13 | ON b.salesman_id=a.salesman_id 14 | RIGHT OUTER JOIN orders c 15 | ON c.customer_id=a.customer_id; 16 | -------------------------------------------------------------------------------- /SQL JOINS/14. Salesmen work for one or more customers or yet to join.txt: -------------------------------------------------------------------------------- 1 | Q. Write a SQL statement to make a list for the salesmen who either work for one or more customers 2 | or yet to join any of the customer. The customer, may have placed, either one or more orders on 3 | or above order amount 2000 and must have a grade, or he may not have placed any order to the 4 | associated supplier. 5 | 6 | Ans--> 7 | 8 | SELECT a.cust_name,a.city,a.grade, 9 | b.name AS "Salesman", 10 | c.ord_no, c.ord_date, c.purch_amt 11 | FROM customer a 12 | RIGHT OUTER JOIN salesman b 13 | ON b.salesman_id=a.salesman_id 14 | LEFT OUTER JOIN orders c 15 | ON c.customer_id=a.customer_id 16 | WHERE c.purch_amt>=2000 17 | AND a.grade IS NOT NULL; 18 | -------------------------------------------------------------------------------- /SQL JOINS/15. One or more customers ordered from the existing list.txt: -------------------------------------------------------------------------------- 1 | Q. Write a SQL statement to generate a list of all the salesmen who either work for one or more customers 2 | or have yet to join any of them. The customer may have placed one or more orders at or above order amount 3 | 2000, and must have a grade, or he may not have placed any orders to the associated supplier. 4 | 5 | Ans--> 6 | 7 | SELECT a.cust_name,a.city, b.ord_no, 8 | b.ord_date,b.purch_amt AS "Order Amount" 9 | FROM customer a 10 | LEFT OUTER JOIN orders b 11 | ON a.customer_id=b.customer_id; 12 | -------------------------------------------------------------------------------- /SQL JOINS/16. Customer who is neither in the list nor have a grade.txt: -------------------------------------------------------------------------------- 1 | Q. Write a SQL statement to generate a report with the customer name, city, 2 | order no. order date, purchase amount for only those customers on the list 3 | who must have a grade and placed one or more orders or which order(s) have 4 | been placed by the customer who neither is on the list nor has a grade. 5 | 6 | Ans--> 7 | 8 | SELECT a.cust_name,a.city, b.ord_no, 9 | b.ord_date,b.purch_amt AS "Order Amount" 10 | FROM customer a 11 | FULL OUTER JOIN orders b 12 | ON a.customer_id=b.customer_id 13 | WHERE a.grade IS NOT NULL; 14 | -------------------------------------------------------------------------------- /SQL JOINS/17. Salesman will appear for all customer and vice versa.txt: -------------------------------------------------------------------------------- 1 | Q. Write a SQL query to combine each row of the salesman table with each 2 | row of the customer table. 3 | 4 | Ans--> 5 | 6 | SELECT * 7 | FROM salesman a 8 | CROSS JOIN customer b; 9 | -------------------------------------------------------------------------------- /SQL JOINS/18. All customers will be served by a salesman from a city.txt: -------------------------------------------------------------------------------- 1 | Q. Write a SQL statement to create a Cartesian product between salesperson 2 | and customer, i.e. each salesperson will appear for all customers and vice 3 | versa for that salesperson who belongs to that city. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM salesman a 9 | CROSS JOIN customer b 10 | WHERE a.city IS NOT NULL; 11 | -------------------------------------------------------------------------------- /SQL JOINS/19. Salesmen belongs to a city, customers have a grade.txt: -------------------------------------------------------------------------------- 1 | Q. Write a SQL statement to create a Cartesian product between salesperson 2 | and customer, i.e. each salesperson will appear for every customer and vice 3 | versa for those salesmen who belong to a city and customers who require a 4 | grade. 5 | 6 | Ans--> 7 | 8 | SELECT * 9 | FROM salesman a 10 | CROSS JOIN customer b 11 | WHERE a.city IS NOT NULL 12 | AND b.grade IS NOT NULL; 13 | -------------------------------------------------------------------------------- /SQL JOINS/2. Customers and their cities for given range of orders.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find those orders 2 | where the order amount exists between 500 and 2000. Return ord_no, 3 | purch_amt, cust_name, city 4 | 5 | Ans--> 6 | 7 | SELECT a.ord_no,a.purch_amt, 8 | b.cust_name,b.city 9 | FROM orders a,customer b 10 | WHERE a.customer_id=b.customer_id 11 | AND a.purch_amt BETWEEN 500 AND 2000; 12 | -------------------------------------------------------------------------------- /SQL JOINS/20. Customers from a different city than the salesmen.txt: -------------------------------------------------------------------------------- 1 | Q. Write a SQL statement to make a Cartesian product between salesman and 2 | customer i.e. each salesman will appear for all customers and vice versa 3 | for those salesmen who must belong to a city which is not the same as his 4 | customer and the customers should have their own grade. 5 | 6 | 7 | Ans--> 8 | 9 | SELECT * 10 | FROM salesman a 11 | CROSS JOIN customer b 12 | WHERE a.city IS NOT NULL 13 | AND b.grade IS NOT NULL 14 | AND a.city<>b.city; 15 | 16 | -------------------------------------------------------------------------------- /SQL JOINS/21. Display each item producer company.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to select all rows from both 2 | participating tables as long as there is a match between pro_com and com_id. 3 | 4 | Ans--> 5 | 6 | SELECT * 7 | FROM item_mast 8 | INNER JOIN company_mast 9 | ON item_mast.pro_com= company_mast.com_id; 10 | 11 | 12 | -------------------------------------------------------------------------------- /SQL JOINS/22. Display the item name, price, and company name.txt: -------------------------------------------------------------------------------- 1 | Q. Write a SQL query to display the item name, price, and company name 2 | of all the products. 3 | 4 | Ans--> 5 | 6 | SELECT item_mast.pro_name, pro_price, company_mast.com_name 7 | FROM item_mast 8 | INNER JOIN company_mast 9 | ON item_mast.pro_com = company_mast.com_id; 10 | -------------------------------------------------------------------------------- /SQL JOINS/23. Display the average price of items of each company.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to calculate the average price 2 | of items of each company. Return average value and company name. 3 | 4 | Ans--> 5 | 6 | SELECT AVG(pro_price), company_mast.com_name 7 | FROM item_mast INNER 8 | JOIN company_mast 9 | ON item_mast.pro_com= company_mast.com_id 10 | GROUP BY company_mast.com_name; 11 | -------------------------------------------------------------------------------- /SQL JOINS/24. Company whose products have an average price.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to calculate and find the 2 | average price of items of each company higher than or equal to Rs. 350. 3 | Return average value and company name. 4 | 5 | Ans--> 6 | 7 | SELECT AVG(pro_price), company_mast.com_name 8 | FROM item_mast INNER JOIN company_mast 9 | ON item_mast.pro_com= company_mast.com_id 10 | GROUP BY company_mast.com_name 11 | HAVING AVG(pro_price) >= 350; 12 | -------------------------------------------------------------------------------- /SQL JOINS/25. Display ID and price of most expensive product.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find the most expensive 2 | product of each company. Return pro_name, pro_price and com_name. 3 | 4 | Ans--> 5 | 6 | SELECT A.pro_name, A.pro_price, F.com_name 7 | FROM item_mast A INNER JOIN company_mast F 8 | ON A.pro_com = F.com_id 9 | AND A.pro_price = 10 | ( 11 | SELECT MAX(A.pro_price) 12 | FROM item_mast A 13 | WHERE A.pro_com = F.com_id 14 | ); 15 | -------------------------------------------------------------------------------- /SQL JOINS/26. Display employees including their department.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to display all the data of 2 | employees including their department. 3 | 4 | Ans--> 5 | 6 | SELECT emp_idno, A.emp_fname AS "First Name", emp_lname AS "Last Name", 7 | B.dpt_name AS "Department", emp_dept, dpt_code, dpt_allotment 8 | FROM emp_details A 9 | INNER JOIN emp_department B 10 | ON A.emp_dept = B.dpt_code; 11 | -------------------------------------------------------------------------------- /SQL JOINS/27. Employee and sanction amount for their department.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to display the first and 2 | last names of each employee, as well as the department name and sanction 3 | amount. 4 | 5 | Ans--> 6 | 7 | SELECT emp_details.emp_fname AS "First Name", emp_lname AS "Last Name", 8 | emp_department.dpt_name AS "Department", 9 | dpt_allotment AS "Amount Allotted" 10 | FROM emp_details 11 | INNER JOIN emp_department 12 | ON emp_details.emp_dept = emp_department.dpt_code; 13 | -------------------------------------------------------------------------------- /SQL JOINS/28. Find employees and departments with a given budget.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find the departments with 2 | budgets more than Rs. 50000 and display the first name and last name of 3 | employees. 4 | 5 | Ans--> 6 | 7 | SELECT emp_details.emp_fname AS "First Name", emp_lname AS "Last Name" 8 | FROM emp_details 9 | INNER JOIN emp_department 10 | ON emp_details.emp_dept = emp_department.dpt_code 11 | AND emp_department.dpt_allotment > 50000; 12 | -------------------------------------------------------------------------------- /SQL JOINS/29. Departments where more than two employees are working.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find the names of departments where more 2 | than two employees are employed. Return dpt_name. 3 | 4 | Ans--> 5 | 6 | SELECT emp_department.dpt_name 7 | FROM emp_details 8 | INNER JOIN emp_department 9 | ON emp_dept =dpt_code 10 | GROUP BY emp_department.dpt_name 11 | HAVING COUNT(*) > 2; 12 | -------------------------------------------------------------------------------- /SQL JOINS/3. From the following tables write a SQL query to find the salesperson(s) and the customer(s) he represents. Return Customer Name, city, Salesman, commission..txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find the salesperson(s) 2 | and the customer(s) he represents. Return Customer Name, city, Salesman, 3 | commission. 4 | 5 | Ans--> 6 | 7 | select a.cust_name as "Customer Name", 8 | a.city, b.name as "Salesman", b.commision 9 | from customer a 10 | inner join salesman b 11 | on a.salesman_id=b.salesman_id; 12 | -------------------------------------------------------------------------------- /SQL JOINS/4. From the following tables write a SQL query to find salespeople who received commissions of more than 12 percent from the company. Return Customer Name, customer city, Salesman, commission..txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find 2 | salespeople who received commissions of more than 12 percent from the company. 3 | Return Customer Name, customer city, Salesman, commission. 4 | 5 | Ans--> 6 | 7 | SELECT a.cust_name AS "Customer Name", 8 | a.city, b.name AS "Salesman", b.commission 9 | FROM customer a 10 | INNER JOIN salesman b 11 | ON a.salesman_id=b.salesman_id 12 | WHERE b.commission>.12; 13 | -------------------------------------------------------------------------------- /SQL JOINS/5. Customers and salespeople live in different cities.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to locate those salespeople 2 | who do not live in the same city where their customers live and have received 3 | a commission of more than 12% from the company. Return Customer Name, customer 4 | city, Salesman, salesman city, commission. 5 | 6 | Ans--> 7 | 8 | SELECT a.cust_name AS "Customer Name", 9 | a.city, b.name AS "Salesman", b.city,b.commission 10 | FROM customer a 11 | INNER JOIN salesman b 12 | ON a.salesman_id=b.salesman_id 13 | WHERE b.commission>.12 14 | AND a.city<>b.city; 15 | -------------------------------------------------------------------------------- /SQL JOINS/6. Display commission of the salesman for an order.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find the details 2 | of an order. Return ord_no, ord_date, purch_amt, Customer Name, grade, 3 | Salesman, commission. 4 | 5 | Ans--> 6 | 7 | SELECT a.ord_no,a.ord_date,a.purch_amt, 8 | b.cust_name AS "Customer Name", b.grade, 9 | c.name AS "Salesman", c.commission 10 | FROM orders a 11 | INNER JOIN customer b 12 | ON a.customer_id=b.customer_id 13 | INNER JOIN salesman c 14 | ON a.salesman_id=c.salesman_id; 15 | -------------------------------------------------------------------------------- /SQL JOINS/7. Join within the tables salesman, customer and orders.txt: -------------------------------------------------------------------------------- 1 | Q. Write a SQL statement to join the tables salesman, customer and orders 2 | so that the same column of each table appears once and only the relational 3 | rows are returned. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM orders 9 | NATURAL JOIN customer 10 | NATURAL JOIN salesman; 11 | -------------------------------------------------------------------------------- /SQL JOINS/8. Customer who works either through a salesman or by own.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to display the customer 2 | name, customer city, grade, salesman, salesman city. The results should 3 | be sorted by ascending customer_id. 4 | 5 | Ans--> 6 | 7 | SELECT a.cust_name,a.city,a.grade, 8 | b.name AS "Salesman",b.city 9 | FROM customer a 10 | LEFT JOIN salesman b 11 | ON a.salesman_id=b.salesman_id 12 | order by a.customer_id; 13 | -------------------------------------------------------------------------------- /SQL JOINS/9. Customers who holds a grade less than 300.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find those customers 2 | with a grade less than 300. Return cust_name, customer city, grade, Salesman, 3 | salesmancity. The result should be ordered by ascending customer_id. 4 | 5 | Ans--> 6 | 7 | SELECT a.cust_name,a.city,a.grade, 8 | b.name AS "Salesman", b.city 9 | FROM customer a 10 | LEFT OUTER JOIN salesman b 11 | ON a.salesman_id=b.salesman_id 12 | WHERE a.grade<300 13 | ORDER BY a.customer_id; 14 | -------------------------------------------------------------------------------- /SQL UNION/1. Display all salesmen and customer located in London.txt: -------------------------------------------------------------------------------- 1 | 1. From the following tables, write a SQL query to find all salespeople and customers located in the city of London. 2 | 3 | Ans--> 4 | 5 | SELECT salesman_id "ID", name, 'Salesman' 6 | FROM salesman 7 | WHERE city='London' 8 | UNION 9 | (SELECT customer_id "ID", cust_name, 'Customer' 10 | FROM customer 11 | WHERE city='London') 12 | -------------------------------------------------------------------------------- /SQL UNION/2. Display distinct salesman and their cities.txt: -------------------------------------------------------------------------------- 1 | 2. From the following tables, write a SQL query to find distinct salespeople and their cities. Return salesperson ID and city. 2 | 3 | Ans--> 4 | 5 | SELECT salesman_id, city 6 | FROM customer 7 | UNION 8 | (SELECT salesman_id, city 9 | FROM salesman) 10 | -------------------------------------------------------------------------------- /SQL UNION/3. Salesmen, customer involved in inventory management.txt: -------------------------------------------------------------------------------- 1 | 3. From the following tables, write a SQL query to find all those salespeople and customers who are involved in the inventory management system. Return salesperson ID, customer ID. 2 | 3 | Ans--> 4 | 5 | SELECT salesman_id, customer_id 6 | FROM customer 7 | UNION 8 | (SELECT salesman_id, customer_id 9 | FROM orders) 10 | -------------------------------------------------------------------------------- /SQL UNION/4. Largest and smallest orders are produced on each date.txt: -------------------------------------------------------------------------------- 1 | 4. From the following table, write a SQL query to find the salespersons who generated the largest and smallest orders on each date. Return salesperson ID, name, order no., highest on/lowest on, order date. 2 | 3 | Ans--> 4 | 5 | SELECT a.salesman_id, name, ord_no, 'highest on', ord_date 6 | FROM salesman a, orders b 7 | WHERE a.salesman_id =b.salesman_id 8 | AND b.purch_amt= 9 | (SELECT MAX (purch_amt) 10 | FROM orders c 11 | WHERE c.ord_date = b.ord_date) 12 | UNION 13 | (SELECT a.salesman_id, name, ord_no, 'lowest on', ord_date 14 | FROM salesman a, orders b 15 | WHERE a.salesman_id =b.salesman_id 16 | AND b.purch_amt= 17 | (SELECT MIN (purch_amt) 18 | FROM orders c 19 | WHERE c.ord_date = b.ord_date)) 20 | -------------------------------------------------------------------------------- /SQL UNION/5. Largest and smallest orders on each date.txt: -------------------------------------------------------------------------------- 1 | 5. From the following tables, write a SQL query to find the salespeople who generated the largest and smallest orders on each date. Sort the result-set on third field. Return salesperson ID, name, order no., highest on/lowest on, order date. 2 | 3 | Ans--> 4 | 5 | SELECT a.salesman_id, name, ord_no, 'highest on', ord_date 6 | FROM salesman a, orders b 7 | WHERE a.salesman_id =b.salesman_id 8 | AND b.purch_amt= 9 | (SELECT MAX (purch_amt) 10 | FROM orders c 11 | WHERE c.ord_date = b.ord_date) 12 | UNION 13 | (SELECT a.salesman_id, name, ord_no, 'lowest on', ord_date 14 | FROM salesman a, orders b 15 | WHERE a.salesman_id =b.salesman_id 16 | AND b.purch_amt= 17 | (SELECT MIN (purch_amt) 18 | FROM orders c 19 | WHERE c.ord_date = b.ord_date)) 20 | ORDER BY 3 21 | -------------------------------------------------------------------------------- /SQL UNION/6. Salesmen who do not have customers in their cities.txt: -------------------------------------------------------------------------------- 1 | 6. From the following tables, write a SQL query to find those salespeople who live in the same city where the customer lives as well as those who do not have customers in their cities by indicating 'NO MATCH'. Sort the result set on 2nd column (i.e. name) in descending order. Return salesperson ID, name, customer name, commission. 2 | 3 | Ans--> 4 | 5 | SELECT salesman.salesman_id, name, cust_name, commission 6 | FROM salesman, customer 7 | WHERE salesman.city = customer.city 8 | UNION 9 | (SELECT salesman_id, name, 'NO MATCH', commission 10 | FROM salesman 11 | WHERE NOT city = ANY 12 | (SELECT city 13 | FROM customer)) 14 | ORDER BY 2 DESC 15 | -------------------------------------------------------------------------------- /SQL UNION/7. Any salesman was matched to the city of any customer.txt: -------------------------------------------------------------------------------- 1 | 7. From the following tables, write a SQL query that appends strings to the selected fields, indicating whether the city of any salesperson is matched with the city of any customer. Return salesperson ID, name, city, MATCHED/NO MATCH. 2 | 3 | Ans--> 4 | 5 | SELECT a.salesman_id, name, a.city, 'MATCHED' 6 | FROM salesman a, customer b 7 | WHERE a.city = b.city 8 | UNION 9 | (SELECT salesman_id, name, city, 'NO MATCH' 10 | FROM salesman 11 | WHERE NOT city = ANY 12 | (SELECT city 13 | FROM customer)) 14 | ORDER BY 2 DESC 15 | -------------------------------------------------------------------------------- /SQL UNION/8. Ratings of all customers with a comment string.txt: -------------------------------------------------------------------------------- 1 | 8. From the following table, write a SQL query to create a union of two queries that shows the customer id, cities, and ratings of all customers. Those with a rating of 300 or greater will have the words 'High Rating', while the others will have the words 'Low Rating'. 2 | 3 | Ans--> 4 | 5 | 6 | SELECT customer_id, city, grade, 'High Rating' 7 | FROM customer 8 | WHERE grade >= 300 9 | UNION 10 | (SELECT customer_id, city, grade, 'Low Rating' 11 | FROM customer 12 | WHERE grade < 300) 13 | -------------------------------------------------------------------------------- /SQL UNION/9. Customer with more than one current order.txt: -------------------------------------------------------------------------------- 1 | 9. From the following table, write a SQL query to find those salespersons and customers who have placed more than one order. Return ID, name. 2 | 3 | Ans--> 4 | 5 | SELECT customer_id as “ID”, cust_name as “NAME” 6 | FROM customer a 7 | WHERE 1< 8 | (SELECT COUNT (*) 9 | FROM orders b 10 | WHERE a.customer_id = b.customer_id) 11 | UNION 12 | (SELECT salesman_idas “ID”, nameas “NAME” 13 | FROM salesman a 14 | WHERE 1 < 15 | (SELECT COUNT (*) 16 | FROM orders b 17 | WHERE a.salesman_id = b.salesman_id)) 18 | ORDER BY 2 19 | -------------------------------------------------------------------------------- /SQL VIEW/1. View for the salesmen who belong to the city New York.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, create a view for those salespeople who belong to the city of New York. 2 | 3 | Ans--> 4 | 5 | CREATE VIEW newyorkstaff 6 | AS SELECT * 7 | FROM salesman 8 | WHERE city = 'New York'; 9 | 10 | 11 | -------------------------------------------------------------------------------- /SQL VIEW/2. View to show salesmen with columns id, name and city.txt: -------------------------------------------------------------------------------- 1 | 2. From the following table, create a view for all salespersons. Return salesperson ID, name, and city. 2 | 3 | Ans--> 4 | 5 | CREATE VIEW salesown 6 | AS SELECT salesman_id, name, city 7 | FROM salesman; 8 | -------------------------------------------------------------------------------- /SQL VIEW/3. View to find the salesmen of the city New York.txt: -------------------------------------------------------------------------------- 1 | 3. From the following table, create a view to locate the salespeople in the city 'New York'. 2 | 3 | Ans--> 4 | 5 | CREATE VIEW newyorkstaff 6 | AS SELECT * 7 | FROM salesman 8 | WHERE city = 'New York'; 9 | -------------------------------------------------------------------------------- /SQL VIEW/4. View to count many customers at each lavel of grade.txt: -------------------------------------------------------------------------------- 1 | 4. From the following table, create a view that counts the number of customers in each grade. 2 | 3 | Ans--> 4 | 5 | CREATE VIEW gradecount (grade, number) 6 | AS SELECT grade, COUNT(*) 7 | FROM customer 8 | GROUP BY grade; 9 | -------------------------------------------------------------------------------- /SQL VIEW/5. View to keep track the number of customers ordering.txt: -------------------------------------------------------------------------------- 1 | 5. From the following table, create a view to count the number of unique customers, compute the average and the total purchase amount of customer orders by each date. 2 | 3 | Ans--> 4 | CREATE VIEW totalforday 5 | AS SELECT ord_date, COUNT(DISTINCT customer_id), 6 | AVG(purch_amt), SUM(purch_amt) 7 | FROM orders 8 | GROUP BY ord_date; 9 | -------------------------------------------------------------------------------- /SQL VIEW/6. View to show for each order the salesman and customer.txt: -------------------------------------------------------------------------------- 1 | 6. From the following tables, create a view to get the salesperson and customer by name. Return order name, purchase amount, salesperson ID, name, customer name. 2 | 3 | Ans--> 4 | 5 | CREATE VIEW nameorders 6 | AS SELECT ord_no, purch_amt, a.salesman_id, name, cust_name 7 | FROM orders a, customer b, salesman c 8 | WHERE a.customer_id = b.customer_id 9 | AND a.salesman_id = c.salesman_id; 10 | -------------------------------------------------------------------------------- /SQL VIEW/7. View to find salesman with the highest order of a day.txt: -------------------------------------------------------------------------------- 1 | 7. From the following table, create a view to find the salesperson who handles a customer who makes the highest order of the day. Return order date, salesperson ID, name. 2 | 3 | CREATE VIEW elitsalesman 4 | AS SELECT b.ord_date, a.salesman_id, a.name 5 | FROM salesman a, orders b 6 | WHERE a.salesman_id = b.salesman_id 7 | AND b.purch_amt = 8 | (SELECT MAX (purch_amt) 9 | FROM orders c 10 | WHERE c.ord_date = b.ord_date); 11 | -------------------------------------------------------------------------------- /SQL VIEW/8. View to find the salesman with the highest order.txt: -------------------------------------------------------------------------------- 1 | 8. From the following table, create a view to find the salesperson who deals with the customer with the highest order at least three times per day. Return salesperson ID and name. 2 | 3 | Ans--> 4 | 5 | CREATE VIEW incentive 6 | AS SELECT DISTINCT salesman_id, name 7 | FROM elitsalesman a 8 | WHERE 3 <= 9 | (SELECT COUNT (*) 10 | FROM elitsalesman b 11 | WHERE a.salesman_id = b.salesman_id); 12 | -------------------------------------------------------------------------------- /SUBQUERIES/1. Display all orders issued by Paul Adam..txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find all the orders issued 2 | by the salesman 'Paul Adam'. Return ord_no, purch_amt, ord_date, customer_id 3 | and salesman_id. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM orders 9 | WHERE salesman_id = 10 | (SELECT salesman_id 11 | FROM salesman 12 | WHERE name='Paul Adam'); 13 | -------------------------------------------------------------------------------- /SUBQUERIES/10. Customers who ordered on August 17, 2012.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write SQL query to find the customers who placed 2 | orders on 17th August 2012. Return ord_no, purch_amt, ord_date, customer_id, 3 | salesman_id and cust_name. 4 | 5 | Ans--> 6 | 7 | SELECT b.*, a.cust_name 8 | FROM orders b, customer a 9 | WHERE a.customer_id=b.customer_id 10 | AND b.ord_date='2012-08-17'; 11 | 12 | 13 | -------------------------------------------------------------------------------- /SUBQUERIES/11. List all salesmen with more than one customer.txt: -------------------------------------------------------------------------------- 1 | Q.From the following tables write a SQL query to find salespeople who had 2 | more than one customer. Return salesman_id and name. 3 | 4 | Ans--> 5 | 6 | 7 | SELECT salesman_id,name 8 | FROM salesman a 9 | WHERE 1 < 10 | (SELECT COUNT(*) 11 | FROM customer 12 | WHERE salesman_id=a.salesman_id); 13 | -------------------------------------------------------------------------------- /SUBQUERIES/12. Find all orders with above-average amounts.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find those orders, which 2 | are higher than the average amount of the orders. Return ord_no, purch_amt, 3 | ord_date, customer_id and salesman_id. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM orders a 9 | WHERE purch_amt > 10 | (SELECT AVG(purch_amt) FROM orders b 11 | WHERE b.customer_id = a.customer_id); 12 | -------------------------------------------------------------------------------- /SUBQUERIES/13. Customer order amounts that are on or above average.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find those orders that are 2 | equal or higher than the average amount of the orders. Return ord_no, 3 | purch_amt, ord_date, customer_id and salesman_id. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM orders a 9 | WHERE purch_amt >= 10 | (SELECT AVG(purch_amt) FROM orders b 11 | WHERE b.customer_id = a.customer_id); 12 | 13 | -------------------------------------------------------------------------------- /SUBQUERIES/14. Sums of all order amounts, grouped by date.txt: -------------------------------------------------------------------------------- 1 | Q. Write a query to find the sums of the amounts from the orders table, 2 | grouped by date, and eliminate all dates where the sum was not at least 3 | 1000.00 above the maximum order amount for that date. 4 | 5 | Ans--> 6 | 7 | SELECT ord_date, SUM (purch_amt) 8 | FROM orders a 9 | GROUP BY ord_date 10 | HAVING SUM (purch_amt) > 11 | (SELECT 1000.00 + MAX(purch_amt) 12 | FROM orders b 13 | WHERE a.ord_date = b.ord_date); 14 | -------------------------------------------------------------------------------- /SUBQUERIES/15. Find out if any of the customers are located in London..txt: -------------------------------------------------------------------------------- 1 | Q.Write a query to extract all data from the customer table if and only if 2 | one or more of the customers in the customer table are located in London. 3 | 4 | Ans--> 5 | 6 | SELECT customer_id,cust_name, city 7 | FROM customer 8 | WHERE EXISTS 9 | (SELECT * 10 | FROM customer 11 | WHERE city='London'); 12 | -------------------------------------------------------------------------------- /SUBQUERIES/16. Find the salesmen who have multiple customers.txt: -------------------------------------------------------------------------------- 1 | Q . From the following tables write a SQL query to find salespeople who deal 2 | with multiple customers. Return salesman_id, name, city and commission. 3 | 4 | Ans--> 5 | 6 | SELECT * 7 | FROM salesman 8 | WHERE salesman_id IN ( 9 | SELECT DISTINCT salesman_id 10 | FROM customer a 11 | WHERE EXISTS ( 12 | SELECT * 13 | FROM customer b 14 | WHERE b.salesman_id=a.salesman_id 15 | AND b.cust_name<>a.cust_name)); 16 | -------------------------------------------------------------------------------- /SUBQUERIES/17. Find all the salesmen worked for only one customer.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find salespeople who deal 2 | with a single customer. Return salesman_id, name, city and commission. 3 | 4 | Ans--> 5 | 6 | SELECT * 7 | FROM salesman 8 | WHERE salesman_id IN ( 9 | SELECT DISTINCT salesman_id 10 | FROM customer a 11 | WHERE NOT EXISTS ( 12 | SELECT * FROM customer b 13 | WHERE a.salesman_id=b.salesman_id 14 | AND a.cust_name<>b.cust_name)); 15 | -------------------------------------------------------------------------------- /SUBQUERIES/18. Show all salesmen with more than one order.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find the salespeople who 2 | deal the customers with more than one order. Return salesman_id, name, city 3 | and commission. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM salesman a 9 | WHERE EXISTS 10 | (SELECT * FROM customer b 11 | WHERE a.salesman_id=b.salesman_id 12 | AND 1< 13 | (SELECT COUNT (*) 14 | FROM orders 15 | WHERE orders.customer_id = 16 | b.customer_id)); 17 | -------------------------------------------------------------------------------- /SUBQUERIES/19. Find salespeople in the city where the customer lives.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find the salespeople who 2 | deal with those customers who live in the same city. Return salesman_id, 3 | name, city and commission. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM salesman 9 | WHERE city=ANY 10 | (SELECT city 11 | FROM customer); 12 | -------------------------------------------------------------------------------- /SUBQUERIES/2. Show all the orders for the London-based salesman.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find all orders generated 2 | by London-based salespeople. Return ord_no, purch_amt, ord_date, customer_id, 3 | salesman_id. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM orders 9 | WHERE salesman_id IN 10 | (SELECT salesman_id 11 | FROM salesman 12 | WHERE city='London'); 13 | -------------------------------------------------------------------------------- /SUBQUERIES/20. Display the salesmen with customers following them.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find salespeople whose place 2 | of residence matches any city where customers live. Return salesman_id, name, 3 | city and commission. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM salesman 9 | WHERE city IN 10 | (SELECT city 11 | FROM customer); 12 | -------------------------------------------------------------------------------- /SUBQUERIES/21. Alphabetically list the salesmen below their customers.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find all those salespeople 2 | whose names appear alphabetically lower than the customer’s name. Return 3 | salesman_id, name, city, commission. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM salesman a 9 | WHERE EXISTS 10 | (SELECT * 11 | FROM CUSTOMER b 12 | WHERE a.name < b.cust_name); 13 | 14 | -------------------------------------------------------------------------------- /SUBQUERIES/22. Customers with the highest grade in alphabetical order.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table write a SQL query to find all those customers 2 | with a higher grade than any customer who belongs to the alphabetically lower 3 | than the city New York. Return customer_id, cust_name, city, grade, 4 | salesman_id. 5 | 6 | Ans--> 7 | 8 | SELECT * 9 | FROM customer 10 | WHERE grade > ANY 11 | (SELECT grade 12 | FROM CUSTOMER 13 | WHERE city < 'New York'); 14 | -------------------------------------------------------------------------------- /SUBQUERIES/23. All orders with amounts greater than one on given date.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table write a SQL query to find all those orders whose 2 | order amount exceeds at least one of the orders placed on September 3 | 10th 2012. Return ord_no, purch_amt, ord_date, customer_id and salesman_id. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM Orders 9 | WHERE purch_amt > ANY 10 | (SELECT purch_amt 11 | FROM orders 12 | WHERE ord_date='2012/09/10'); 13 | -------------------------------------------------------------------------------- /SUBQUERIES/24. Orders smaller than any amount for a London customer.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find orders where the order 2 | amount is less than the order amount of a customer residing in London City. 3 | Return ord_no, purch_amt, ord_date, customer_id and salesman_id. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM orders 9 | WHERE purch_amt < ANY 10 | (SELECT purch_amt 11 | FROM orders a, customer b 12 | WHERE a.customer_id=b.customer_id 13 | AND b.city='London'); 14 | -------------------------------------------------------------------------------- /SUBQUERIES/25. Orders higher than any amount for a London customer.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find those orders where 2 | every order amount is less than the maximum order amount of a customer who 3 | lives in London City. Return ord_no, purch_amt, ord_date, customer_id and 4 | salesman_id. 5 | 6 | Ans--> 7 | 8 | SELECT * 9 | FROM orders 10 | WHERE purch_amt < 11 | (SELECT MAX (purch_amt) 12 | FROM orders a, customer b 13 | WHERE a.customer_id=b.customer_id 14 | AND b.city='London'); 15 | -------------------------------------------------------------------------------- /SUBQUERIES/26. Customers with higher grades than everyone in New York.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find those customers whose 2 | grades are higher than those living in New York City. Return customer_id, 3 | cust_name, city, grade and salesman_id. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM customer 9 | WHERE grade > ALL 10 | (SELECT grade 11 | FROM customer 12 | WHERE city='New York'); 13 | -------------------------------------------------------------------------------- /SUBQUERIES/27. Salesperson live in the same city as the customers.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to calculate the total order 2 | amount generated by a salesperson. Salespersons should be from the cities 3 | where the customers reside. Return salesperson name, city and total order 4 | amount. 5 | 6 | Ans--> 7 | 8 | SELECT salesman.name, salesman.city, subquery1.total_amt FROM 9 | salesman, (SELECT salesman_id, SUM(orders.purch_amt) AS total_amt 10 | FROM orders GROUP BY salesman_id) subquery1 WHERE subquery1.salesman_id = salesman.salesman_id AND 11 | salesman.city IN (SELECT DISTINCT city FROM customer); 12 | 13 | -------------------------------------------------------------------------------- /SUBQUERIES/28. Customers whose grade differs from a London customer.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find those customers whose 2 | grades are not the same as those who live in London City. Return customer_id, 3 | cust_name, city, grade and salesman_id. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | SELECT * FROM customer WHERE grade <> ALL 9 | (SELECT grade FROM customer WHERE city='London' AND NOT grade IS NULL); 10 | -------------------------------------------------------------------------------- /SUBQUERIES/29. Customers who differ from the grade in the city Paris.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find those customers whose 2 | grades are different from those living in Paris. Return customer_id, cust_name 3 | , city, grade and salesman_id. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM customer 9 | WHERE grade NOT IN 10 | (SELECT grade 11 | FROM customer 12 | WHERE city='Paris'); 13 | -------------------------------------------------------------------------------- /SUBQUERIES/3. Orders of the salesman working for the customer 3007.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find all orders generated 2 | by the salespeople who may work for customers whose id is 3007. Return ord_no, 3 | purch_amt, ord_date, customer_id, salesman_id. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM orders 9 | WHERE salesman_id = 10 | (SELECT DISTINCT salesman_id 11 | FROM orders 12 | WHERE customer_id =3007); 13 | -------------------------------------------------------------------------------- /SUBQUERIES/30. Different grades than other Dallas customers.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find all those customers who 2 | have different grades than any customer who lives in Dallas City. Return 3 | customer_id, cust_name,city, grade and salesman_id. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM customer 9 | WHERE NOT grade = ANY 10 | (SELECT grade 11 | FROM customer 12 | WHERE city='Dallas'); 13 | -------------------------------------------------------------------------------- /SUBQUERIES/31. Find the average price of each manufacturer products.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to calculate the average price 2 | of each manufacturer's product along with their name. Return Average Price and 3 | Company. 4 | 5 | Ans--> 6 | 7 | SELECT AVG(pro_price) AS "Average Price", 8 | company_mast.com_name As "Company" 9 | FROM item_mast, company_mast 10 | WHERE item_mast.pro_com= company_mast.com_id 11 | GROUP BY company_mast.com_name; 12 | -------------------------------------------------------------------------------- /SUBQUERIES/32. List of products with an average price of 350 or more.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to calculate the average price 2 | of each manufacturer's product of 350 or more. Return Average Price and 3 | Company. 4 | 5 | Ans--> 6 | 7 | SELECT AVG(pro_price) AS "Average Price", 8 | company_mast.com_name AS "Company" 9 | FROM item_mast, company_mast 10 | WHERE item_mast.pro_com= company_mast.com_id 11 | GROUP BY company_mast.com_name 12 | HAVING AVG(pro_price) >= 350; 13 | -------------------------------------------------------------------------------- /SUBQUERIES/33. Prices for the most expensive products by each company.txt: -------------------------------------------------------------------------------- 1 | Q.From the following tables, write a SQL query to find the most expensive 2 | product of each company. Return Product Name, Price and Company. 3 | 4 | Ans--> 5 | 6 | SELECT P.pro_name AS "Product Name", 7 | P.pro_price AS "Price", 8 | C.com_name AS "Company" 9 | FROM item_mast P, company_mast C 10 | WHERE P.pro_com = C.com_id 11 | AND P.pro_price = 12 | ( 13 | SELECT MAX(P.pro_price) 14 | FROM item_mast P 15 | WHERE P.pro_com = C.com_id 16 | ); 17 | -------------------------------------------------------------------------------- /SUBQUERIES/34. Find all employees with last names Gabriel or Dosio.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find employees whose last 2 | name is Gabriel or Dosio. Return emp_idno, emp_fname, emp_lname and emp_dept. 3 | 4 | Ans--> 5 | 6 | SELECT * 7 | FROM emp_details 8 | WHERE emp_lname IN ('Gabriel' , 'Dosio'); 9 | -------------------------------------------------------------------------------- /SUBQUERIES/35. List all employees in departments 89 and 63.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find the employees who work 2 | in department 89 or 63. Return emp_idno, emp_fname, emp_lname and emp_dept. 3 | 4 | Ans--> 5 | 6 | SELECT * 7 | FROM emp_details 8 | WHERE emp_dept IN (89,63); 9 | -------------------------------------------------------------------------------- /SUBQUERIES/36. Departments with allotment amounts exceeding Rs. 50000.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find those employees who work 2 | for the department where the departmental allotment amount is more than Rs. 50000. 3 | Return emp_fname and emp_lname. 4 | 5 | Ans--> 6 | 7 | SELECT emp_fname, emp_lname 8 | FROM emp_details 9 | WHERE emp_dept IN 10 | (SELECT dpt_code 11 | FROM emp_department 12 | WHERE dpt_allotment > 50000); 13 | -------------------------------------------------------------------------------- /SUBQUERIES/37. Departments with a sanction amount higher than average.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find the departments whose 2 | sanction amount is higher than the average sanction amount for all departments 3 | . Return dpt_code, dpt_name and dpt_allotment. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM emp_department 9 | WHERE dpt_allotment > 10 | ( 11 | SELECT AVG(dpt_allotment) 12 | FROM emp_department 13 | ); 14 | -------------------------------------------------------------------------------- /SUBQUERIES/38. Find departments with more than two employees.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find which departments have 2 | more than two employees. Return dpt_name. 3 | 4 | Ans--> 5 | 6 | SELECT dpt_name FROM emp_department 7 | WHERE dpt_code IN 8 | ( 9 | SELECT emp_dept 10 | FROM emp_details 11 | GROUP BY emp_dept 12 | HAVING COUNT(*) >2 13 | ); 14 | -------------------------------------------------------------------------------- /SUBQUERIES/39. Employees working with the lowest sanction amount.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find the departments with 2 | the second lowest sanction amount. Return emp_fname and emp_lname. 3 | 4 | Ans--> 5 | 6 | SELECT emp_fname, emp_lname 7 | FROM emp_details 8 | WHERE emp_dept IN ( 9 | SELECT dpt_code 10 | FROM emp_department 11 | WHERE dpt_allotment= ( 12 | SELECT MIN(dpt_allotment) 13 | FROM emp_department 14 | WHERE dpt_allotment > 15 | (SELECT MIN(dpt_allotment) 16 | FROM emp_department ))); 17 | -------------------------------------------------------------------------------- /SUBQUERIES/4. Orders with values greater than the given date average.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to find the order values greater than the average 2 | order value of 10th October 2012. Return ord_no, purch_amt, ord_date, customer_id, salesman_id. 3 | 4 | Ans--> 5 | 6 | SELECT * 7 | FROM orders 8 | WHERE purch_amt > 9 | (SELECT AVG(purch_amt) 10 | FROM orders 11 | WHERE ord_date ='10/10/2012'); 12 | -------------------------------------------------------------------------------- /SUBQUERIES/5. Find all orders attributed to salesman in New York.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find all the orders 2 | generated in New York city. Return ord_no, purch_amt, ord_date, customer_id 3 | and salesman_id. 4 | 5 | Ans--> 6 | 7 | SELECT * 8 | FROM orders 9 | WHERE salesman_id IN 10 | (SELECT salesman_id 11 | FROM salesman 12 | WHERE city ='New York'); 13 | -------------------------------------------------------------------------------- /SUBQUERIES/6. Display the commissions of all salespeople in Paris.txt: -------------------------------------------------------------------------------- 1 | Q.From the following tables write a SQL query to determine the commission of the salespeople in Paris. Return commission. 2 | 3 | Ans--> 4 | 5 | SELECT commission 6 | FROM salesman 7 | WHERE salesman_id IN 8 | (SELECT salesman_id 9 | FROM customer 10 | WHERE city = 'Paris'); 11 | 12 | -------------------------------------------------------------------------------- /SUBQUERIES/7. The customer whose ID is 2001 is below Mc Lyons.txt: -------------------------------------------------------------------------------- 1 | Q. Write a query to display all the customers whose ID is 2001 below the 2 | salesperson ID of Mc Lyon. 3 | 4 | Ans--> 5 | 6 | SELECT * 7 | FROM customer 8 | WHERE customer_id = 9 | (SELECT salesman_id -2001 10 | FROM salesman 11 | WHERE name = 'Mc Lyon'); 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /SUBQUERIES/8. Counts the customers with grades over New York average.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables write a SQL query to count the number of 2 | customers with grades above the average in New York City. Return grade 3 | and count. 4 | 5 | Ans--> 6 | 7 | SELECT grade, COUNT (*) 8 | FROM customer 9 | GROUP BY grade 10 | HAVING grade > 11 | (SELECT AVG(grade) 12 | FROM customer 13 | WHERE city = 'New York'); 14 | -------------------------------------------------------------------------------- /SUBQUERIES/9. Find salesmen who earned the maximum commission..txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find those salespeople 2 | who earned the maximum commission. Return ord_no, purch_amt, ord_date, and 3 | salesman_id. 4 | 5 | Ans--> 6 | 7 | SELECT ord_no, purch_amt, ord_date, salesman_id 8 | FROM orders 9 | WHERE salesman_id IN( 10 | SELECT salesman_id 11 | FROM salesman 12 | WHERE commission = ( 13 | SELECT MAX(commission) 14 | FROM salesman)); 15 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/1. Employees getting more salary than employee 163.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who receive a higher salary than the employee with ID 163. Return first name, last name. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name 6 | FROM employees 7 | WHERE salary > 8 | ( SELECT salary 9 | FROM employees 10 | WHERE employee_id=163 11 | ); 12 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/10. Employees, salary is between smallest salary and 2500.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table and write a SQL query to find those employees whose salary falls within the range of the smallest salary and 2500. Return all the fields. 2 | 3 | Ans--> 4 | 5 | SELECT * 6 | FROM employees 7 | WHERE salary BETWEEN 8 | (SELECT MIN(salary) 9 | FROM employees) AND 2500; 10 | 11 | 12 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/11. Display employees not working in certain departments.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find those employees who do not work in the departments where managers’ IDs are between 100 and 200 (Begin and end values are included.). Return all the fields of the employees. 2 | 3 | Ans--> 4 | 5 | SELECT * 6 | FROM employees 7 | WHERE department_id NOT IN 8 | (SELECT department_id 9 | FROM departments 10 | WHERE manager_id BETWEEN 100 AND 200); 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/12. Display employees who get the second highest salary.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who get second-highest salary. Return all the fields of the employees. 2 | 3 | 4 | Ans--> 5 | 6 | SELECT * 7 | FROM employees 8 | WHERE employee_id IN 9 | (SELECT employee_id 10 | FROM employees 11 | WHERE salary = 12 | (SELECT MAX(salary) 13 | FROM employees 14 | WHERE salary < 15 | (SELECT MAX(salary) 16 | FROM employees))); 17 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/13. Employee names and hire dates for Claras department.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find those employees who work in the same department as ‘Clara’. Exclude all those records where first name is ‘Clara’. Return first name, last name and hire date. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name, hire_date 6 | FROM employees 7 | WHERE department_id = 8 | ( SELECT department_id 9 | FROM employees 10 | WHERE first_name = 'Clara') 11 | AND first_name <> 'Clara'; 12 | 13 | 14 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/14. Employees in a department beginning with T.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find those employees who work in a department where the employee’s first name contains the letter 'T'. Return employee ID, first name and last name. 2 | 3 | 4 | Ans--> 5 | 6 | SELECT employee_id, first_name, last_name 7 | FROM employees 8 | WHERE department_id IN 9 | ( SELECT department_id 10 | FROM employees 11 | WHERE first_name LIKE '%T%' ); 12 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/15. J-named employees earn more than average.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find those employees who earn more than the average salary and work in the same department as an employee whose first name contains the letter 'J'. Return employee ID, first name and salary. 2 | 3 | Ans--> 4 | 5 | SELECT employee_id, first_name , salary 6 | FROM employees 7 | WHERE salary > 8 | (SELECT AVG (salary) 9 | FROM employees ) 10 | AND department_id IN 11 | ( SELECT department_id 12 | FROM employees 13 | WHERE first_name LIKE '%J%'); 14 | 15 | 16 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/16. Employees whose department location is Toronto.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees whose department is located at ‘Toronto’. Return first name, last name, employee ID, job ID. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name, employee_id, job_id 6 | FROM employees 7 | WHERE department_id = 8 | (SELECT department_id 9 | FROM departments 10 | WHERE location_id = 11 | (SELECT location_id 12 | FROM locations 13 | WHERE city ='Toronto')); 14 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/17. Employees salary is smaller than job title MK_MAN.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees whose salary is lower than that of employees whose job title is ‘MK_MAN’. Return employee ID, first name, last name, job ID. 2 | 3 | Ans--> 4 | 5 | SELECT employee_id,first_name,last_name,job_id 6 | FROM employees 7 | WHERE salary < ANY 8 | ( SELECT salary 9 | FROM employees 10 | WHERE job_id = 'MK_MAN' ); 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/18. Employees whose job title is MK_MAN, exclude MK_MAN.txt: -------------------------------------------------------------------------------- 1 | Q.From the following table, write a SQL query to find those employees whose salary is lower than that of employees whose job title is "MK_MAN". Exclude employees of Job title ‘MK_MAN’. Return employee ID, first name, last name, job ID. 2 | 3 | Ans--> 4 | 5 | SELECT employee_id,first_name,last_name, job_id 6 | FROM employees 7 | WHERE salary < ANY 8 | ( SELECT salary 9 | FROM employees 10 | WHERE job_id = 'MK_MAN' ) 11 | AND job_id <> 'MK_MAN'; 12 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/19. Employees whose salary is higher than title is PU_MAN.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees whose salary exceeds the salary of all those employees whose job title is "PU_MAN". Exclude job title ‘PU_MAN’. Return employee ID, first name, last name, job ID. 2 | 3 | Ans--> 4 | 5 | SELECT employee_id, first_name, last_name, job_id 6 | FROM employees 7 | WHERE salary > ALL 8 | ( SELECT salary 9 | FROM employees 10 | WHERE job_id = 'PU_MAN' ) 11 | AND job_id <> 'PU_MAN'; 12 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/2. Employees who work in the same designation as ID 169.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find out which employees have the same designation as the employee whose ID is 169. Return first name, last name, department ID and job ID. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name, salary, department_id, job_id 6 | FROM employees 7 | WHERE job_id = 8 | ( SELECT job_id 9 | FROM employees 10 | WHERE employee_id=169 11 | ); 12 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/20. Salary exceeds the average salary of any department.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees whose salaries are higher than the average for all departments. Return employee ID, first name, last name, job ID. 2 | 3 | Ans--> 4 | 5 | SELECT employee_id, first_name, last_name, job_id 6 | FROM employees 7 | WHERE salary > ALL 8 | ( SELECT AVG(salary) 9 | FROM employees 10 | GROUP BY department_id 11 | ); -------------------------------------------------------------------------------- /Sub Queries On HR Database/21. Employees whose salary is more than 3700.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to check whether there are any employees with salaries exceeding 3700. Return first name, last name and department ID. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name, department_id 6 | FROM employees 7 | WHERE EXISTS 8 | (SELECT * 9 | FROM employees 10 | WHERE salary >3700 ); 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/22. Departments which contains at least one employee.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to calculate total salary of the departments where at least one employee works. Return department ID, total salary. 2 | 3 | Ans--> 4 | 5 | SELECT departments.department_id, result1.total_amt 6 | FROM departments, 7 | ( SELECT employees.department_id, SUM(employees.salary) total_amt 8 | FROM employees 9 | GROUP BY department_id) result1 10 | WHERE result1.department_id = departments.department_id; 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/23. Employees whose job title is ST_MAN, IT_PROG.txt: -------------------------------------------------------------------------------- 1 | Q. Write a query to display the employee id, name ( first name and last name ) and the job id column with a modified title SALESMAN for those employees whose job title is ST_MAN and DEVELOPER for whose job title is IT_PROG. 2 | 3 | Ans--> 4 | 5 | SELECT employee_id, first_name, last_name, 6 | CASE job_id 7 | WHEN 'ST_MAN' THEN 'SALESMAN' 8 | WHEN 'IT_PROG' THEN 'DEVELOPER' 9 | ELSE job_id 10 | END AS designation, salary 11 | FROM employees; 12 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/24. Employees salary is more and less than the average.txt: -------------------------------------------------------------------------------- 1 | Q. Write a query to display the employee id, name ( first name and last name ), salary and the SalaryStatus column with a title HIGH and LOW respectively for those employees whose salary is more than and less than the average salary of all employees. 2 | 3 | Ans--> 4 | 5 | SELECT employee_id, first_name, last_name, salary, 6 | CASE WHEN salary >= (SELECT AVG(salary) 7 | FROM employees) THEN 'HIGH' 8 | ELSE 'LOW' 9 | END AS SalaryStatus 10 | FROM employees; 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/25. SalaryStatus column with a title HIGH and LOW.txt: -------------------------------------------------------------------------------- 1 | Q. Write a query to display the employee id, name ( first name and last name ), SalaryDrawn, AvgCompare (salary - the average salary of all employees) and the SalaryStatus column with a title HIGH and LOW respectively for those employees whose salary is more than and less than the average salary of all employees. 2 | 3 | 4 | Ans--> 5 | 6 | SELECT employee_id, first_name, last_name, salary AS SalaryDrawn, 7 | ROUND((salary -(SELECT AVG(salary) FROM employees)),2) AS AvgCompare, 8 | CASE WHEN salary >= 9 | (SELECT AVG(salary) 10 | FROM employees) THEN 'HIGH' 11 | ELSE 'LOW' 12 | END AS SalaryStatus 13 | FROM employees; 14 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/26. Departments that have one or more employees.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find all those departments where at least one employee is employed. Return department name. 2 | 3 | Ans--> 4 | 5 | SELECT department_name 6 | FROM departments 7 | WHERE department_id IN 8 | (SELECT DISTINCT(department_id) 9 | FROM employees); 10 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/27. Employees in departments in the United Kingdom.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find employees who work in departments located in the United Kingdom. Return first name. 2 | 3 | 4 | Ans--> 5 | 6 | SELECT first_name 7 | FROM employees 8 | WHERE department_id IN 9 | (SELECT department_id 10 | FROM departments 11 | WHERE location_id IN 12 | (SELECT location_id 13 | FROM locations 14 | WHERE country_id = 15 | (SELECT country_id 16 | FROM countries 17 | WHERE country_name='United Kingdom'))); 18 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/28. IT employees who earn more than the average.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find out which employees are earning more than the average salary and who work in any of the IT departments. Return last name. 2 | 3 | Ans--> 4 | 5 | SELECT last_name 6 | FROM employees 7 | WHERE department_id IN 8 | (SELECT department_id 9 | FROM departments 10 | WHERE department_name LIKE 'IT%') 11 | AND salary > 12 | (SELECT avg(salary) 13 | FROM employees); 14 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/29. Determine who earns more than Mr. Ozer.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find all those employees who earn more than an employee whose last name is 'Ozer'. Sort the result in ascending order by last name. Return first name, last name and salary. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name, salary 6 | FROM employees 7 | WHERE salary > 8 | (SELECT salary 9 | FROM employees 10 | WHERE last_name='Ozer') 11 | ORDER BY last_name; 12 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/3. Smallest salary of any of the departments.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees whose salary matches the lowest salary of any of the departments. Return first name, last name and department ID. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name, salary, department_id 6 | FROM employees 7 | WHERE salary IN 8 | ( SELECT MIN(salary) 9 | FROM employees 10 | GROUP BY department_id 11 | ); 12 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/30. Manager who works for a department based in the US.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query find the employees who report to a manager based in the United States. Return first name, last name. 2 | 3 | Ans--> 4 | 5 | SELECT first_name,last_name FROM employees 6 | WHERE manager_id IN 7 | (SELECT employee_id 8 | FROM employees 9 | WHERE department_id IN 10 | (SELECT department_id 11 | FROM departments 12 | WHERE location_id IN 13 | (SELECT location_id 14 | FROM locations 15 | WHERE country_id='US'))); 16 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/31. Salary exceeds 50% of their departments total salary.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find those employees whose salaries exceed 50% of their department's total salary bill. Return first name, last name. 2 | 3 | Ans--> 4 | 5 | SELECT e1.first_name, e1.last_name 6 | FROM employees e1 7 | WHERE salary > 8 | ( SELECT (SUM(salary))*.5 9 | FROM employees e2 10 | WHERE e1.department_id=e2.department_id); 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/32. Find out the details of employees who are managers.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find those employees who are managers. Return all the fields of employees table. 2 | 3 | Ans--> 4 | 5 | SELECT * 6 | FROM employees 7 | WHERE employee_id IN 8 | (SELECT DISTINCT manager_id FROM employees); 9 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/33. A department 50 employee without a commission %.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who do not have commission percentage and have salaries between 7000, 12000 (Begin and end values are included.) and who are employed in the department number 50. Return all the fields of employees. 2 | 3 | Ans--> 4 | 5 | SELECT * 6 | FROM employees 7 | WHERE commission_pct IS NULL 8 | AND salary BETWEEN 7000 AND 12000 9 | AND department_id=50; 10 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/34. Jobs which average salary is above 8000.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to compute the average salary of each job ID. Exclude those records where average salary is higher than 8000. Return job ID, average salary. 2 | 3 | Ans--> 4 | 5 | SELECT job_id, AVG(salary) 6 | FROM employees 7 | GROUP BY job_id 8 | HAVING AVG(salary)>8000; 9 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/35. Departments which located in the city London.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find those departments that are located in the city of London. Return department ID, department name. 2 | 3 | Ans--> 4 | 5 | SELECT department_id, department_name 6 | FROM departments 7 | WHERE location_id = 8 | ( 9 | SELECT location_id 10 | FROM locations 11 | WHERE city = 'London' 12 | ); 13 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/36. Employees earn more than average salary in ASC order.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who earn more than the average salary. Sort the result-set in descending order by salary. Return first name, last name, salary, and department ID. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name , salary, department_id 6 | FROM employees 7 | WHERE salary > ( 8 | SELECT AVG(salary) 9 | FROM employees ) 10 | ORDER BY salary DESC; 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/37. Employees earn more than the maximum salary.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who earn more than the maximum salary for a department of ID 40. Return first name, last name and department ID. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name, salary, department_id 6 | FROM employees 7 | WHERE salary > ALL 8 | (SELECT salary 9 | FROM employees 10 | WHERE department_id = 40); 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/38. Location where department number 30 is located.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find departments for a particular location. The location matches the location of the department of ID 30. Return department name and department ID. 2 | 3 | Ans--> 4 | 5 | SELECT department_name, department_id 6 | FROM departments 7 | WHERE location_id = 8 | ( 9 | SELECT location_id 10 | FROM departments 11 | WHERE department_id = 30); 12 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/39. Department where the employee works with ID 201.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find employees who work for the department in which employee ID 201 is employed. Return first name, last name, salary, and department ID. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name, salary, department_id 6 | FROM employees 7 | WHERE department_id = 8 | (SELECT department_id 9 | FROM employees 10 | WHERE employee_id = 201); 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/4. Employees who earn more than the average salary.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who earn more than the average salary. Return employee ID, first name, last name. 2 | 3 | Ans--> 4 | 5 | SELECT employee_id, first_name,last_name 6 | FROM employees 7 | WHERE salary > 8 | ( SELECT AVG(salary) 9 | FROM employees 10 | ); 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/40. Employee who works in that department which ID is 40.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees whose salary matches that of the employee who works in department ID 40. Return first name, last name, salary, and department ID. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name, salary, department_id 6 | FROM employees 7 | WHERE salary IN ( 8 | SELECT salary 9 | FROM employees 10 | WHERE department_id = 40); 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/41. Employees who work in the department Marketing.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who work in the department 'Marketing'. Return first name, last name and department ID. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name, department_id 6 | FROM employees 7 | WHERE department_id = 8 | (SELECT department_id 9 | FROM departments 10 | WHERE department_name = 'Marketing'); 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/42. Employees earning more than departments minimum wage.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who earn more than the minimum salary of a department of ID 40. Return first name, last name, salary, and department ID. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name, salary, department_id 6 | FROM employees 7 | WHERE salary > ANY 8 | (SELECT salary 9 | FROM employees 10 | WHERE department_id = 40); 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/43. Employees who was hired after the employee with ID 165.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who joined after the employee whose ID is 165. Return first name, last name and hire date. 2 | 3 | Ans--> 4 | 5 | SELECT first_name ||' '|| last_name AS Full_Name , hire_date 6 | FROM employees 7 | WHERE hire_date > ( 8 | SELECT hire_date 9 | FROM employees 10 | WHERE employee_id = 165); 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/44. Minimum salary of a department which ID is 70.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who earn less than the minimum salary of a department of ID 70. Return first name, last name, salary, and department ID. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name, salary, department_id 6 | FROM employees 7 | WHERE salary < ALL 8 | (SELECT salary 9 | FROM employees 10 | WHERE department_id = 70); 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/45. Employees earn less than the average salary like Laura.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who earn less than the average salary and work at the department where Laura (first name) is employed. Return first name, last name, salary, and department ID. 2 | 3 | Ans--> 4 | 5 | 6 | SELECT first_name, last_name, salary, department_id 7 | FROM employees 8 | WHERE salary < 9 | (SELECT AVG(salary) 10 | FROM employees ) 11 | AND department_id = 12 | (SELECT department_id 13 | FROM employees 14 | WHERE first_name = 'Laura'); 15 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/46. Employees whose department is in London.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find all employees whose department is located in London. Return first name, last name, salary, and department ID. 2 | 3 | Ans--> 4 | 5 | SELECT first_name, last_name, salary, department_id 6 | FROM employees 7 | WHERE department_id IN 8 | (SELECT department_id 9 | FROM departments 10 | WHERE location_id = 11 | (SELECT location_id 12 | FROM locations 13 | WHERE city = 'London')); 14 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/47. Display the city where the employee with ID 134 works.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find the city of the employee of ID 134. Return city. 2 | 3 | 4 | Ans--> 5 | 6 | SELECT city 7 | FROM locations 8 | WHERE location_id = 9 | (SELECT location_id 10 | FROM departments 11 | WHERE department_id = 12 | (SELECT department_id 13 | FROM employees 14 | WHERE employee_id=134)); 15 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/48. Employees who already done one or more jobs.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find those departments where maximum salary is 7000 and above. The employees worked in those departments have already completed one or more jobs. Return all the fields of the departments. 2 | 3 | Ans--> 4 | 5 | SELECT * 6 | FROM departments 7 | WHERE DEPARTMENT_ID IN 8 | (SELECT DEPARTMENT_ID 9 | FROM employees 10 | WHERE EMPLOYEE_ID IN 11 | (SELECT EMPLOYEE_ID 12 | FROM job_history 13 | GROUP BY EMPLOYEE_ID 14 | HAVING COUNT(EMPLOYEE_ID) > 1) 15 | GROUP BY DEPARTMENT_ID 16 | HAVING MAX(SALARY) > 7000); 17 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/49. Departments which starting salary is at least 8000.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find those departments where the starting salary is at least 8000. Return all the fields of departments. 2 | 3 | Ans--> 4 | 5 | SELECT * FROM departments 6 | WHERE department_id IN 7 | ( SELECT department_id 8 | FROM employees 9 | GROUP BY department_id 10 | HAVING MIN(salary)>=8000); 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/5. Salary of all employees who report to Payam.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who report to that manager whose first name is ‘Payam’. Return first name, last name, employee ID and salary. 2 | 3 | Ans--> 4 | 5 | 6 | 7 | SELECT first_name, last_name, employee_id, salary 8 | FROM employees 9 | WHERE manager_id = 10 | (SELECT employee_id 11 | FROM employees 12 | WHERE first_name = 'Payam' 13 | ); 14 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/50. Manager who is supervising 4 or more employees.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those managers who supervise four or more employees. Return manager name, department ID. 2 | 3 | Ans--> 4 | 5 | SELECT first_name || ' ' || last_name AS Manager_name,department_id 6 | FROM employees 7 | WHERE employee_id IN 8 | (SELECT manager_id 9 | FROM employees 10 | GROUP BY manager_id 11 | HAVING COUNT(*)>=4); 12 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/51. Employees worked as a Sales Representative in the past.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find employees who have previously worked as 'Sales Representatives'. Return all the fields of jobs. 2 | 3 | Ans--> 4 | 5 | SELECT * 6 | FROM jobs 7 | WHERE job_id IN 8 | (SELECT job_id 9 | FROM employees 10 | WHERE employee_id IN 11 | (SELECT employee_id 12 | FROM job_history 13 | WHERE job_id='SA_REP')); 14 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/52. Employees who earn second lowest salary of all.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who earn the second-lowest salary of all the employees. Return all the fields of employees . 2 | 3 | Ans--> 4 | 5 | SELECT * 6 | FROM employees m 7 | WHERE 2 = (SELECT COUNT(DISTINCT salary ) 8 | FROM employees 9 | WHERE salary <= m.salary); 10 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/53. Display the details of departments managed by Susan.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the departments managed by Susan. Return all the fields of departments. 2 | 3 | 4 | Ans--> 5 | 6 | SELECT * 7 | FROM departments 8 | WHERE manager_id IN 9 | (SELECT employee_id 10 | FROM employees 11 | WHERE first_name='Susan'); 12 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/54. Highest salary drawar in a department.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who earn the highest salary in a department. Return department ID, employee name, and salary. 2 | 3 | Ans--> 4 | 5 | SELECT department_id, first_name || ' ' || last_name AS Employee_name, salary 6 | FROM employees a 7 | WHERE salary = 8 | (SELECT MAX(salary) 9 | FROM employees 10 | WHERE department_id = a.department_id); 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/55. Employees who did not have any job in the past.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees who have not had a job in the past. Return all the fields of employees. 2 | 3 | 4 | Ans--> 5 | 6 | SELECT * 7 | FROM employees 8 | WHERE employee_id NOT IN 9 | (SELECT employee_id 10 | FROM job_history); 11 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/6. All employees in the Finance department.txt: -------------------------------------------------------------------------------- 1 | Q. From the following tables, write a SQL query to find all those employees who work in the Finance department. Return department ID, name (first name), job ID and department name. 2 | 3 | Ans--> 4 | 5 | SELECT e.department_id, e.first_name, e.job_id , d.department_name 6 | FROM employees e , departments d 7 | WHERE e.department_id = d.department_id 8 | AND d.department_name = 'Finance'; 9 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/7. Salary and reporting person id is 3000 and 121.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find the employee whose salary is 3000 and reporting person’s ID is 121. Return all fields. 2 | 3 | Ans--> 4 | 5 | SELECT * 6 | FROM employees 7 | WHERE (salary,manager_id)= 8 | (SELECT 3000,121); 9 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/8. Employee whose id is any of the number 134, 159, 183.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees whose ID matches any of the numbers 134, 159 and 183. Return all the fields. 2 | 3 | Ans--> 4 | SELECT * 5 | FROM employees 6 | WHERE employee_id IN (134,159,183); 7 | 8 | 9 | -------------------------------------------------------------------------------- /Sub Queries On HR Database/9. Employees whose salary is within the given range.txt: -------------------------------------------------------------------------------- 1 | Q. From the following table, write a SQL query to find those employees whose salary is in the range of 1000, and 3000 (Begin and end values have included.). Return all the fields. 2 | 3 | Ans--> 4 | 5 | SELECT * FROM employees 6 | WHERE salary BETWEEN 1000 and 3000; 7 | 8 | --------------------------------------------------------------------------------