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

Aggregate Function

This document contains 7 SQL questions and answers about querying an Order table. The questions include finding average purchase item, total purchase amount, maximum grade by purchase item, minimum total order, orders with duplicate total amounts, total purchase item by customer, and highest purchase amount by customer. Optimized queries are provided for some questions.

Uploaded by

Muhammad Faisal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Aggregate Function

This document contains 7 SQL questions and answers about querying an Order table. The questions include finding average purchase item, total purchase amount, maximum grade by purchase item, minimum total order, orders with duplicate total amounts, total purchase item by customer, and highest purchase amount by customer. Optimized queries are provided for some questions.

Uploaded by

Muhammad Faisal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Q.1 Write a SQL statement to find the average purchase item of all orders.

Ans. Select Avg(PURCHASE_ITEM) as Purchase_Item from Order_Table

Q.2 Write a SQL statement to find the total purchase amount of all orders.
Ans. Select Sum(TOTAL_AMOUNT) as TOTAL_AMOUNT from Order_Table

Q.3 Write a SQL statement which selects the max grade for each of the purchase item of the
order.
Ans. SELECT PURCHASE_ITEM,MAX(GRADE) FROM Order_Table
GROUP BY PURCHASE_ITEM

Q.4 Write a query to get the minimum total orders from order table.
Ans. SELECT MIN(TOTAL_AMOUNT) as Minimum_Total_Order from Order_Table

Q.5 Write a query to get the number of order with the same .total orders.
Ans. Select ORD_NO from Order_Table o
inner join (
SELECT TOTAL_AMOUNT, COUNT(*) AS dupeCount
FROM Order_Table
GROUP BY TOTAL_AMOUNT
HAVING COUNT(*) > 1
) oc on o.TOTAL_AMOUNT = oc.TOTAL_AMOUNT

The above query will work for the question no 5. But Here is the optimized query as well for Question no
5 belwo.

SELECT TOTAL_AMOUNT, COUNT(*) as Count, Max(ORD_NO) AS dupes_ord_no


FROM Order_Table
GROUP BY TOTAL_AMOUNT
HAVING (COUNT(*) > 1)

Q.6 Write a query to get total purchase item of same customer.


Ans. Select CUST_ID,Sum(PURCHASE_ITEM) as Total_Purchase_Item from Order_Table
Group by CUST_ID
Q.7 Write a SQL statement to find the highest purchase amount ordered by the each customer
with their ID and highest purchase amount.
Ans. Select CUST_ID,Max(TOTAL_AMOUNT) as Total_Purchase_Item from Order_Table
Group by CUST_ID
Order by CUST_ID asc, Total_Purchase_Item asc

You might also like