0% found this document useful (0 votes)
36 views

Oracle Group By Having Clause Exercises Solutions

Oracle Group By Having Clause
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Oracle Group By Having Clause Exercises Solutions

Oracle Group By Having Clause
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Oracle Group By Having Clause Exercises

1. From the following table, write a SQL query to calculate total purchase
amount of all orders. Return total purchase amount
ord_no purch_amt ord_date customer_id salesman_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

Soln:

SELECT SUM (purch_amt) FROM orders;

2. From the following table, write a SQL query to calculate average purchase
amount of all orders. Return average purchase amount.

SELECT AVG (purch_amt)

FROM orders;

3. From the following table, write a SQL query to count the number of unique
salespeople. Return number of salespeople
SELECT COUNT (DISTINCT salesman_id)

FROM orders;

4. From the following table, write a SQL query to count the number of
customers. Return number of customers
SELECT COUNT(*)

FROM customer;

5. From the following table, write a SQL query to find the number of customers
who got at least a gradation for his/her activity.

SELECT COUNT (ALL grade) FROM customer;

6.From the following table, write a SQL query to find the maximum purchase

amount.

SELECT MAX (purch_amt)

FROM orders;

7. From the following table, write a SQL query to find the minimum purchase
amount.
SELECT MIN(purch_amt) FROM orders;

8. From the following table, write a SQL query to find the highest grade of the
customers for each of the city. Return city, maximum grade.

SELECT city,MAX(grade)

FROM customer

GROUP BY city;

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.

SELECT customer_id,MAX(purch_amt)

FROM orders

GROUP BY customer_id;
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.

SELECT customer_id,ord_date,MAX(purch_amt)

FROM orders

GROUP BY customer_id,ord_date;

11. From the following table, write a SQL query to find the highest purchase
amount on '2012-08-17' by each salesperson. Return salesperson ID,
purchase amount.

SELECT salesman_id,MAX(purch_amt)

FROM orders

WHERE ord_date = '2012-08-17'

GROUP BY salesman_id;

12. From the following table, write a SQL query to find highest order
(purchase) amount by each customer in a particular order date. Filter the
result by highest order (purchase) amount above 2000.00. Return customer
id, order date and maximum purchase amount.
SELECT customer_id,ord_date,MAX(purch_amt) FROM orders GROUP BY customer_id,ord_date

HAVING MAX(purch_amt)>2000.00;

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.

SELECT customer_id,ord_date,MAX(purch_amt)

FROM orders

GROUP BY customer_id,ord_date
14. From the following table, write a SQL query to find the maximum order
(purchase) amount by 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
SELECT customer_id,ord_date,MAX(purch_amt)

FROM orders

GROUP BY customer_id,ord_date

HAVING MAX(purch_amt) IN(2000 ,3000,5760, 6000);

15. From the following table, write a SQL query to find the maximum order
(purchase) amount by 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
SELECT customer_id,MAX(purch_amt)

FROM orders

WHERE customer_id BETWEEN 3002 and 3007

GROUP BY customer_id;

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
SELECT customer_id,MAX(purch_amt)

FROM orders

WHERE customer_id BETWEEN 3002 and 3007

GROUP BY customer_id

HAVING MAX(purch_amt)>1000;

17. From the following table, write a SQL query to find 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.
SELECT salesman_id,MAX(purch_amt)

FROM orders

GROUP BY salesman_id

HAVING salesman_id BETWEEN 5003 AND 5008;

18. write a SQL query to count all the orders generated on '2012-08-17'.
Return number of orders
SELECT COUNT(*)

FROM orders

WHERE ord_date='2012-08-17';

19. write a SQL query to count number of salespeople who belongs to a city.
Return number of salespeople
SELECT COUNT(*)

FROM salesman

WHERE city IS NOT NULL;

20. write a SQL query to count number of orders by the combination of each
order date and salesperson. Return order date, salesperson id
SELECT ord_date,salesman_id,COUNT(*)

FROM orders

GROUP BY ord_date,salesman_id;

21. write a SQL query to calculate the average product price. Return average
product price.

SELECT AVG(pro_price) AS "Average Price"

FROM item_mast;

22. write a SQL query to count number of products where product price is
higher than or equal to 350. Return number of products

SELECT COUNT(*) AS "Number of Products"

FROM item_mast
WHERE pro_price >= 350;

23. write a SQL query to compute the average price for unique companies.
Return average price and company id

SELECT AVG(pro_price) AS "Average Price",

pro_com AS "Company ID"

FROM item_mast

GROUP BY pro_com;

24. write a SQL query to compute the sum of the allotment amount of all
departments. Return sum of the allotment amount.

SELECT SUM(dpt_allotment)

FROM emp_department;

25. write a SQL query to find the number of employees in each department.
Return department code and number of employees
SELECT emp_dept, COUNT(*)

FROM emp_details

GROUP BY emp_dept;

You might also like