Aggregate Function
Aggregate Function
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.