SQL Query Abd Interview Question
SQL Query Abd Interview Question
Q8. what primary key and foreing key and its differences.
Q21. what is used for group by ,having, and order by and like and where
Q24. what is function and what is diff between function and procedure
Q28. what is diff between on delete cascade and on delete set null and on delete update
Q29. what is cascading and why use it
ord_nopurch_amtord_datecustomer_idsalesman_id
---------- ---------- ---------- ----------- -----------
70001 150.5 2012-10-05 3005 5002
70009 270.65 2012-09-10 3001 5005
70002 65.26 2012-10-05 3002 5001
70004 110.5 2012-08-17 3009 5003
70007 948.5 2012-09-10 3005 5002
70005 2400.6 2012-07-27 3007 5001
70008 5760 2012-09-10 3002 5001
70010 1983.43 2012-10-10 3004 5006
70003 2480.4 2012-10-10 3009 5003
70012 250.45 2012-06-27 3008 5002
70011 75.29 2012-08-17 3003 5007
70013 3045.6 2012-04-25 3002 5001
Sample table : salesman
1. find the details of those salespeople who come from the 'Paris' City or 'Rome' City. Return salesman_id, name,
city, commission.
2. find the details of the salespeople who come from either 'Paris' or 'Rome'. Return salesman_id, name, city,
commission.
3. find the details of those salespeople who live in cities other than Paris and Rome. Return salesman_id, name, city,
commission.
4. retrieve the details of all customers whose ID belongs to any of the values 3007, 3008 or 3009. Return
customer_id, cust_name, city, grade, and salesman_id.
5. to find salespeople who receive commissions between 0.12 and 0.14 (begin and end values are included). Return
salesman_id, name, city, and commission.
6. select orders between 500 and 4000 (begin and end values are included). Exclude orders amount 948.50 and
1983.43. Return ord_no, purch_amt, ord_date, customer_id, and salesman_id.
7. to retrieve the details of the salespeople whose names begin with any letter between 'A' and 'L' (not inclusive).
Return salesman_id, name, city, commission.
8. find the details of all salespeople except those whose names begin with any letter between 'A' and 'M'. Return
salesman_id, name, city, commission.
9. retrieve the details of the customers whose names begins with the letter 'B'. Return customer_id, cust_name,
city, grade, salesman_id..
10. to find the details of the customers whose names end with the letter 'n'. Return customer_id, cust_name, city,
grade, salesman_id.
11. find the details of those salespeople whose names begin with ‘N’ and the fourth character is 'l'. Rests may be any
character. Return salesman_id, name, city, commission.
1. From the following table, write a SQL query to calculate total purchase amount of all orders. Return total
purchase amount.
2. From the following table, write a SQL query to calculate the average purchase
amount of all orders. Return average purchase amount.
3. From the following table, write a SQL query that counts the number of unique salespeople. Return number of
salespeople.
4. From the following table, write a SQL query to count the number of customers. Return number of customers.
5. From the following table, write a SQL query to determine the number of customers who received at least one
grade for their activity.
6. From the following table, write a SQL query to find the maximum purchase amount.
7. From the following table, write a SQL query to find the minimum purchase amount.
8. From the following table, write a SQL query to find the highest grade of the customers in each city. Return city,
maximum grade.
9. From the following table, write a SQL query to find the highest purchase amount ordered by each customer.
Return customer ID, maximum purchase amount.
10. From the following table, write a SQL query to find the highest purchase amount ordered by each customer on a
particular date. Return, order date and highest purchase amount.
11. From the following table, write a SQL query to determine the highest purchase amount made by each
salesperson on '2012-08-17'. Return salesperson ID, purchase amount
12. From the following table, write a SQL query to find the highest order (purchase) amount by each customer on a
particular order date. Filter the result by highest order (purchase) amount above 2000.00. Return customer id,
order date and maximum purchase amount.
13. From the following table, write a SQL query to find the maximum order (purchase) amount in the range 2000 -
6000 (Begin and end values are included.) by combination of each customer and order date. Return customer id,
order date and maximum purchase amount.
14. From the following table, write a SQL query to find the maximum order (purchase) amount based on the
combination of each customer and order date. Filter the rows for maximum order (purchase) amount is either
2000, 3000, 5760, 6000. Return customer id, order date and maximum purchase amount.
15. From the following table, write a SQL query to determine the maximum order amount for each customer. The
customer ID should be in the range 3002 and 3007(Begin and end values are included.). Return customer id and
maximum purchase amount.
16. From the following table, write a SQL query to find the maximum order (purchase) amount for each customer.
The customer ID should be in the range 3002 and 3007(Begin and end values are included.). Filter the rows for
maximum order (purchase) amount is higher than 1000. Return customer id and maximum purchase amount.
17. From the following table, write a SQL query to determine the maximum order (purchase) amount generated by
each salesperson. Filter the rows for the salesperson ID is in the range 5003 and 5008 (Begin and end values are
included.). Return salesperson id and maximum purchase amount.
18. From the following table, write a SQL query to count all the orders generated on '2012-08-17'. Return number of
orders.
19. From the following table, write a SQL query to count the number of salespeople in a city. Return number of
salespeople.
20. From the following table, write a SQL query to count the number of orders based on the combination of each
order date and salesperson. Return order date, salesperson id.
21. From the following table, write a SQL query to calculate the average product price. Return average product price.
22. From the following table, write a SQL query to count the number of products whose price are higher than or
equal to 350. Return number of products.
23. From the following table, write a SQL query to compute the average price for unique companies. Return average
price and company id.
24. From the following table, write a SQL query to compute the sum of the allotment amount of all departments.
Return sum of the allotment amount.
25. From the following table, write a SQL query to count the number of employees in each department. Return
department code and number of employees.
Formatting Output
Sample table: salesman
salesman_id | name | city | commission
-------------+------------+----------+------------
5001 | James Hoog | New York | 0.15
5002 | Nail Knite | Paris | 0.13
5005 | Pit Alex | London | 0.11
5006 | Mc Lyon | Paris | 0.14
5007 | Paul Adam | Rome | 0.13
5003 | Lauson Hen | San Jose | 0.12
1. From the following table, write a SQL query to select all the salespeople. Return
salesman_id, name, city, commission with the percent sign (%).
2. From the following table, write a SQL query to find the number of orders booked for each
day. Return the result in a format like "For 2001-10-10 there are 15 orders".".
3. From the following table, write a SQL query to find all the orders. Sort the result-set in
ascending order by ord_no. Return all fields.
4. From the following table, write a SQL query to find all the orders. Sort the result-set in
descending order by ord_date. Return all fields.
5. From the following table, write a SQL query to find all the orders. Sort the result-set in
descending order by ord_date and purch_amt. Return all fields.
6. From the following table, write a SQL query to find all the customers. Sort the result-set by
customer_id. Return cust_name, city, grade.
7. From the following table, write a SQL query that calculates the maximum purchase
amount generated by each salesperson for each order date. Sort the result-set by
salesperson id and order date in ascending order. Return salesperson id, order date and
maximum purchase amount.
8. From the following table, write a SQL query to find all the customers. Sort the result-set in
descending order on 3rd field. Return customer name, city and grade.
9. From the following table, write a SQL query that counts the unique orders and the highest
purchase amount for each customer. Sort the result-set in descending order on 2nd field.
Return customer ID, number of distinct orders and highest purchase amount by each
customer.
10. From the following table, write a SQL query to calculate the summation of purchase
amount, total commission (15% for all salespeople) by each order date. Sort the result-set
on order date. Return order date, summation of purchase amount and commission.
JOINS
Sample table : salesman
salesman_id | name | city | commission
-------------+------------+----------+------------
5001 | James Hoog | New York | 0.15
5002 | Nail Knite | Paris | 0.13
5005 | Pit Alex | London | 0.11
5006 | Mc Lyon | Paris | 0.14
5007 | Paul Adam | Rome | 0.13
5003 | Lauson Hen | San Jose | 0.12
Sample table: customer
customer_id | cust_name | city | grade | salesman_id
-------------+----------------+------------+-------+-------------
3002 | Nick Rimando | New York | 100 | 5001
3007 | Brad Davis | New York | 200 | 5001
3005 | Graham Zusi | California | 200 | 5002
3008 | Julian Green | London | 300 | 5002
3004 | Fabian Johnson | Paris | 300 | 5006
3009 | Geoff Cameron | Berlin | 100 | 5003
3003 | Jozy Altidor | Moscow | 200 | 5007
3001 | Brad Guzan | London | | 5005
1. From the following tables write a SQL query to find the salesperson and customer who
reside in the same city. Return Salesman, cust_name and city.
2. From the following tables write a SQL query to find those orders where the order amount
exists between 500 and 2000. Return ord_no, purch_amt, cust_name, city.
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.
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
5. From the following tables write a SQL query to locate those salespeople who do not live in
the same city where their customers live and have received a commission of more than
12% from the company. Return Customer Name, customer city, Salesman, salesman city,
commission.
6. From the following tables write a SQL query to find the details of an order. Return ord_no,
ord_date, purch_amt, Customer Name, grade, Salesman, commission.
7. Write a SQL statement to join the tables salesman, customer and orders so that the same
column of each table appears once and only the relational rows are returned.
8. From the following tables write a SQL query to display the customer name, customer city,
grade, salesman, salesman city. The results should be sorted by ascending customer_id.
9. From the following tables write a SQL query to find those customers with a grade less than
300. Return cust_name, customer city, grade, Salesman, salesmancity. The result should
be ordered by ascending customer_id.
10. Write a SQL statement to make a report with customer name, city, order number, order
date, and order amount in ascending order according to the order date to determine
whether any of the existing customers have placed an order or not.
11. SQL statement to generate a report with customer name, city, order number, order date,
order amount, salesperson name, and commission to determine if any of the existing
customers have not placed orders or if they have placed orders through their salesman or
by themselves.
12. Write a SQL statement to generate a list in ascending order of salespersons who work
either for one or more customers or have not yet joined any of the customers.
13. From the following tables write a SQL query to list all salespersons along with customer
name, city, grade, order number, date, and amount. Condition for selecting list of salesmen
: 1. Salesmen who works for one or more customer or, 2. Salesmen who not yet join under
any customer, Condition for selecting list of customer : 3. placed one or more orders, or 4.
no order placed to their salesman.
14. Write a SQL statement to make a list for the salesmen who either work for one or more
customers or yet to join any of the customer. The customer may have placed, either one or
more orders on or above order amount 2000 and must have a grade, or he may not have
placed any order to the associated supplier.
15. For those customers from the existing list who put one or more orders, or which orders
have been placed by the customer who is not on the list, create a report containing the
customer name, city, order number, order date, and purchase amount
16. Write a SQL statement to generate a report with the customer name, city, order no. order
date, purchase amount for only those customers on the list who must have a grade and
placed one or more orders or which order(s) have been placed by the customer who
neither is on the list nor has a grade.
17. Write a SQL query to combine each row of the salesman table with each row of the
customer table.
18. Write a SQL statement to create a Cartesian product between salesperson and customer,
i.e. each salesperson will appear for all customers and vice versa for that salesperson who
belongs to that city.
19. Write a SQL statement to create a Cartesian product between salesperson and customer,
i.e. each salesperson will appear for every customer and vice versa for those salesmen who
belong to a city and customers who require a grade.
20. Write a SQL statement to make a Cartesian product between salesman and customer i.e.
each salesman will appear for all customers and vice versa for those salesmen who must
belong to a city which is not the same as his customer and the customers should have their
own grade.
21. From the following tables write a SQL query to select all rows from both participating tables
as long as there is a match between pro_com and com_id.
22. Write a SQL query to display the item name, price, and company name of all the products.
23. From the following tables write a SQL query to calculate the average price of items of each
company. Return average value and company name.
24. From the following tables write a SQL query to calculate and find the average price of
items of each company higher than or equal to Rs. 350. Return average value and
company name.
25. From the following tables write a SQL query to find the most expensive product of each
company. Return pro_name, pro_price and com_name.
26. From the following tables write a SQL query to display all the data of employees including
their department.
27. From the following tables write a SQL query to display the first and last names of each
employee, as well as the department name and sanction amount.
28. From the following tables write a SQL query to find the departments with budgets more
than Rs. 50000 and display the first name and last name of employees.
29. From the following tables write a SQL query to find the names of departments where more
than two employees are employed. Return dpt_name.
SUBQUERIES
Sample table : Customer
customer_idcust_name city grade salesman_id
----------- ------------ ---------- ---------- ----------
3002 Nick Rimando New York 100 5001
3005 Graham Zusi California 200 5002
3001 Brad Guzan London 100 5005
3004 Fabian Johns Paris 300 5006
3007 Brad Davis New York 200 5001
3009 Geoff Camero Berlin 100 5003
3008 Julian Green London 300 5002
3003 Jozy AltidorMoncow 200 5007
Sample table: Salesman
salesman_id name city commission
----------- ---------- ---------- ----------
5001 James Hoog New York 0.15
5002 Nail Knite Paris 0.13
5005 Pit Alex London 0.11
5006 Mc Lyon Paris 0.14
5003 Lauson Hen San Jose 0.12
5007 Paul Adam Rome 0.13
Sample table: Orders
ord_nopurch_amtord_datecustomer_idsalesman_id
---------- ---------- ---------- ----------- -----------
70001 150.5 2012-10-05 3005 5002
70009 270.65 2012-09-10 3001 5005
70002 65.26 2012-10-05 3002 5001
70004 110.5 2012-08-17 3009 5003
70007 948.5 2012-09-10 3005 5002
70005 2400.6 2012-07-27 3007 5001
70008 5760 2012-09-10 3002 5001
70010 1983.43 2012-10-10 3004 5006
70003 2480.4 2012-10-10 3009 5003
70012 250.45 2012-06-27 3008 5002
70011 75.29 2012-08-17 3003 5007
70013 3045.6 2012-04-25 3002 5001
1. From the following tables, write a SQL query to find all the orders issued by the salesman 'Paul
Adam'. Return ord_no, purch_amt, ord_date, customer_id and salesman_id.
2. From the following tables write a SQL query to find all orders generated by London-based
salespeople. Return ord_no, purch_amt, ord_date, customer_id, salesman_id.
3. From the following tables write a SQL query to find all orders generated by the salespeople
who may work for customers whose id is 3007. Return ord_no, purch_amt, ord_date,
customer_id, salesman_id.
4. From the following tables write a SQL query to find the order values greater than the average
order value of 10th October 2012. Return ord_no, purch_amt, ord_date, customer_id,
salesman_id.
5. From the following tables, write a SQL query to find all the orders generated in New York city.
Return ord_no, purch_amt, ord_date, customer_id and salesman_id.
6. From the following tables write a SQL querytodetermine the commission of the salespeople in
Paris. Return commission.
7. Write a query to display all the customers whose ID is 2001 below the salesperson ID of Mc Lyon.
8. From the following tables write a SQL query to count the number of customers with grades
above the average in New York City. Return grade and count.
9. From the following tables, write a SQL query to find those salespeople who earned the
maximum commission. Return ord_no, purch_amt, ord_date, and salesman_id.
10. From the following tables write SQL query to find the customers who placed orders on 17th
August 2012. Return ord_no, purch_amt, ord_date, customer_id, salesman_id and cust_name.
11. From the following tables write a SQL query to find salespeople who had more than one
customer. Return salesman_id and name.
12. From the following tables write a SQL query to find those orders, which are higher than the
average amount of the orders. Return ord_no, purch_amt, ord_date, customer_id and
salesman_id.
13. From the following tables write a SQL query to find those orders that are equal or higher than
the average amount of the orders. Return ord_no, purch_amt, ord_date, customer_id and
salesman_id.
14. Write a query to find the sums of the amounts from the orders table, grouped by date, and
eliminate all dates where the sum was not at least 1000.00 above the maximum order amount
for that date.
15. Write a query to extract all data from the customer table if and only if one or more of the
customers in the customer table are located in London. Sample table : Customer
16. From the following tables write a SQL query to find salespeople who deal with multiple
customers. Return salesman_id, name, city and commission.
17. From the following tables write a SQL query to find salespeople who deal with a single
customer. Return salesman_id, name, city and commission.
18. From the following tables, write a SQL query to find the salespeople who deal the customers
with more than one order. Return salesman_id, name, city and commission.
19. From the following tables write a SQL query to find all salespeople who are located in any city
where there is at least one customer. Return salesman_id, name, city and commission.
20. From the following tables write a SQL query to find salespeople whose place of residence
matches any city where customers live. Return salesman_id, name, city and commission.
21. From the following tables write a SQL query to find all those salespeople whose names appear
alphabetically lower than the customer’s name. Return salesman_id, name, city, commission.
22. From the following table write a SQL query to find all those customers with a higher grade than
all the customers alphabetically below the city of New York. Return customer_id, cust_name, city,
grade, salesman_id.
23. From the following table write a SQL query to find all those orders whose order amount
exceeds at least one of the orders placed on September 10th 2012. Return ord_no, purch_amt,
ord_date, customer_id and salesman_id.
24. From the following tables write a SQL query to find orders where the order amount is less than
the order amount of a customer residing in London City. Return ord_no, purch_amt, ord_date,
customer_id and salesman_id.
25. From the following tables write a SQL query to find those orders where every order amount is
less than the maximum order amount of a customer who lives in London City. Return ord_no,
purch_amt, ord_date, customer_id and salesman_id.
26. From the following tables write a SQL query to find those customers whose grades are higher
than those living in New York City. Return customer_id, cust_name, city, grade and salesman_id.
27. From the following tables write a SQL query to calculate the total order amount generated by a
salesperson. Salespersons should be from the cities where the customers reside. Return
salesperson name, city and total order amount.
28. From the following tables write a SQL query to find those customers whose grades are not the
same as those who live in London City. Return customer_id, cust_name, city, grade and
salesman_id.
29. From the following tables write a SQL quer
30. From the following tables write a SQL query to find all those customers who have different
grades than any customer who lives in Dallas City. Return customer_id, cust_name,city, grade
and salesman_id.
UNION
Sample table: Salesman
salesman_id | name | city | commission
-------------+------------+----------+------------
5001 | James Hoog | New York | 0.15
5002 | Nail Knite | Paris | 0.13
5005 | Pit Alex | London | 0.11
5006 | Mc Lyon | Paris | 0.14
5007 | Paul Adam | Rome | 0.13
5003 | Lauson Hen | San Jose | 0.12
Sample table: Customer
customer_id | cust_name | city | grade | salesman_id
-------------+----------------+------------+-------+-------------
3002 | Nick Rimando | New York | 100 | 5001
3007 | Brad Davis | New York | 200 | 5001
3005 | Graham Zusi | California | 200 | 5002
3008 | Julian Green | London | 300 | 5002
3004 | Fabian Johnson | Paris | 300 | 5006
3009 | Geoff Cameron | Berlin | 100 | 5003
3003 | Jozy Altidor | Moscow | 200 | 5007
3001 | Brad Guzan | London | | 5005
Sample table:Order
ord_nopurch_amtord_datecustomer_idsalesman_id
---------- ---------- ---------- ----------- -----------
70001 150.5 2012-10-05 3005 5002
70009 270.65 2012-09-10 3001 5005
70002 65.26 2012-10-05 3002 5001
70004 110.5 2012-08-17 3009 5003
70007 948.5 2012-09-10 3005 5002
70005 2400.6 2012-07-27 3007 5001
70008 5760 2012-09-10 3002 5001
70010 1983.43 2012-10-10 3004 5006
70003 2480.4 2012-10-10 3009 5003
70012 250.45 2012-06-27 3008 5002
70011 75.29 2012-08-17 3003 5007
70013 3045.6 2012-04-25 3002 5001
1. From the following tables, write a SQL query to find all salespeople and customers located in
the city of London.
2. From the following tables, write a SQL query to find distinct salespeople and their cities. Return
salesperson ID and city.
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.
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.
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.
6. 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.
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.
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'.
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.
VIEW
Sample table:Salesman
salesman_id | name | city | commission
-------------+------------+----------+------------
5001 | James Hoog | New York | 0.15
5002 | Nail Knite | Paris | 0.13
5005 | Pit Alex | London | 0.11
5006 | Mc Lyon | Paris | 0.14
5007 | Paul Adam | Rome | 0.13
5003 | Lauson Hen | San Jose | 0.12