Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Mr. Sandeep M Shinde www.eTutorInfotech.

com

eTutorInfotech@gmail.com 8308800428/9930975472

Aggregate Functions
1. Write a SQL statement to find the total purchase amount of all orders
Sample table: orders
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

SELECT SUM (purch_amt)


FROM orders;

2. Write a SQL statement to find the average purchase amount of all orders
Sample table: orders

SELECT AVG (purch_amt)


FROM orders;

3. Write a SQL statement to find the number of salesmen currently listing for all of
their customers. (Sample table: orders)

SELECT COUNT (DISTINCT salesman_id) FROM orders;

4. Write a SQL statement know how many customer have listed their names.
Sample table: customer

customer_id cust_name city grade salesman_id


----------- ------------ ---------- ---------- -----------
3002 Nick Rimando New York 100 5001
3005 Graham Zusi California 200 5002
3001 Brad Guzan London 5005
3004 Fabian Johns Paris 300 5006
3007 Brad Davis New York 200 5001
3009 Geoff Camero Berlin 100 5003
Mr. Sandeep M Shinde www.eTutorInfotech.com

eTutorInfotech@gmail.com 8308800428/9930975472

3008 Julian Green London 300 5002


3003 Jozy Altidor Moscow 200 5007

SELECT COUNT(*)
FROM customer;

5. Write a SQL statement find the number of customers who gets at least a gradation
for his/her performance.

Sample table: customer


SELECT COUNT (ALL grade)
FROM customer;

6. Write a SQL statement to get the maximum purchase amount of all the
orders. Sample table: orders
SELECT MAX (purch_amt)
FROM orders;

7. Write a SQL statement to get the minimum purchase amount of all the orders.

Sample table: orders


SELECT MIN (purch_amt)
FROM orders;

8. Write a SQL statement which selects the highest grade for each of the cities of the
customers. Sample table: customer

SEL SELECT city,MAX(grade)


FROM customer
GROUP BY city;

9. Write a SQL statement to find the highest purchase amount ordered by the each
customer with their ID and highest purchase amount.

Sample table: orders

SELECT customer_id,MAX(purch_amt)
FROM orders
GROUP BY customer_id;
Mr. Sandeep M Shinde www.eTutorInfotech.com

eTutorInfotech@gmail.com 8308800428/9930975472

10. Write a SQL statement to find the highest purchase amount ordered by the each
customer on a particular date with their ID, order date and highest purchase
amount.

Sample table: orders

SELECT customer_id,ord_date,MAX(purch_amt)
FROM orders
GROUP BY customer_id,ord_date;

11. Write a SQL statement to find the highest purchase amount on a date '2012-08-
17' for each salesman with their ID

Sample table: orders

SELECT salesman_id,MAX(purch_amt)
FROM orders
WHERE ord_date = '2012-08-17'
GROUP BY salesman_id;

12. Write a SQL statement to find the highest purchase amount with their ID and
order date, for only those customers who have highest purchase amount in a day is
more than 2000.

Sample table: orders

SELECT customer_id,ord_date,MAX(purch_amt)
FROM orders
GROUP BY customer_id,ord_date
HAVING MAX(purch_amt)>2000.00;

NOTE: Aggregate function can NOT be used in WHERE clause over here, you need to use
HAVING clause instead.

SELECT customer_id,ord_date,MAX(purch_amt)
FROM orders
WHERE MAX(purch_amt)>2000.00-------------THIS IS WRONG

GROUP BY customer_id,ord_date ;
13. Write a SQL statement to find the highest purchase amount with their ID and
order date, for those customers who have a higher purchase amount in a day is
within the range 2000 and 6000.
Mr. Sandeep M Shinde www.eTutorInfotech.com

eTutorInfotech@gmail.com 8308800428/9930975472

Sample table: orders

SELECT customer_id,ord_date,MAX(purch_amt)
FROM orders
GROUP BY customer_id,ord_date
HAVING MAX(purch_amt) BETWEEN 2000 AND 6000;

14. Write a SQL statement to find the highest purchase amount with their ID and
order date, for only those customers who have a higher purchase amount in a day is
within the list 2000, 3000, 5760 and 6000.

Sample table: orders

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. Write a SQL statement to find the highest purchase amount with their ID, for
only those customers whose ID is within the range 3002 and 3007.

Sample table: orders

SELECT customer_id,MAX(purch_amt)
FROM orders
WHERE customer_id BETWEEN 3002 and 3007 ------ No aggregate function
GROUP BY customer_id;

16. Write a SQL statement to display customer details (ID and purchase amount)
whose IDs are within the range 3002 and 3007 and highest purchase amount is
more than 1000.

Sample table: orders

SELECT customer_id,MAX(purch_amt)
FROM orders
WHERE customer_id BETWEEN 3002 and 3007
GROUP BY customer_id
HAVING MAX(purch_amt)>1000;
Mr. Sandeep M Shinde www.eTutorInfotech.com

eTutorInfotech@gmail.com 8308800428/9930975472

17. Write a SQL statement to find the highest purchase amount with their ID, for
only those salesmen whose ID is within the range 5003 and 5008.

Sample table: orders

SELECT salesman_id,MAX(purch_amt)
FROM orders
GROUP BY salesman_id
HAVING salesman_id BETWEEN 5003 AND 5008;

18. Write a SQL statement that counts all orders for a date August 17th, 2012

Sample table: orders

SELECT COUNT(*)
FROM orders
WHERE ord_date='2012-08-17';

19. Write a SQL statement that count the number of salesmen for whom a city is
specified. Note that there may be spaces or no spaces in the city column if no city is
specified

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 0.12
5007 Paul Adam Rome 0.13

SELECT COUNT(*)
FROM salesman
WHERE city IS NOT NULL;

20. Write a query that counts the number of salesmen with their order date and ID
registering orders for each day.

Sample table: orders

SELECT ord_date,salesman_id,COUNT(*)
FROM orders
Mr. Sandeep M Shinde www.eTutorInfotech.com

eTutorInfotech@gmail.com 8308800428/9930975472

GROUP BY ord_date,salesman_id;

21. Write a SQL query to calculate the average price of all the products.

Sample table: item_mast


PRO_ID PRO_NAME PRO_PRICE PRO_COM
------- ------------------------- ---------- ----------
101 Mother Board 3200 15
102 Key Board 450 16
103 ZIP drive 250 14
104 Speaker 550 16
105 Monitor 5000 11
106 DVD drive 900 12
107 CD drive 800 12
108 Printer 2600 13
109 Refill cartridge 350 13
110 Mouse 250 12

SELECT AVG(pro_price) AS "Average Price"


FROM item_mast;

22. Write a SQL query to find the number of products with a price more than or
equal to Rs.350.

Sample table: item_mast

SELECT COUNT(*) AS "Number of Products"


FROM item_mast
WHERE pro_price >= 350;

23. Write a SQL query to display the average price of each company's products,
along with their code.

Sample table: item_mast

SELECT AVG(pro_price) AS "Average Price",


pro_com AS "Company ID"
FROM item_mast
GROUP BY pro_com;
Mr. Sandeep M Shinde www.eTutorInfotech.com

eTutorInfotech@gmail.com 8308800428/9930975472

24. Write a query in SQL to find the sum of the allotment amount of all departments.

Sample table: emp_department


DPT_CODE DPT_NAME DPT_ALLOTMENT
-------- --------------- -------------
57 IT 65000
63 Finance 15000
47 HR 240000
27 RD 55000
89 QC 75000

SELECT SUM(dpt_allotment)
FROM emp_department;

25. Write a query in SQL to find the number of employees in each department along
with the department code.

Sample table: emp_details


EMP_IDNO EMP_FNAME EMP_LNAME EMP_DEPT
--------- --------------- --------------- ----------
127323 Michale Robbin 57
526689 Carlos Snares 63
843795 Enric Dosio 57
328717 Jhon Snares 63
444527 Joseph Dosni 47
659831 Zanifer Emily 47
847674 Kuleswar Sitaraman 57
748681 Henrey Gabriel 47
555935 Alex Manuel 57
539569 George Mardy 27

SELECT emp_dept, COUNT(*)


FROM emp_details
GROUP BY emp_dept;

You might also like