SQL – GROUP BY

Spread the love

Group by is used for calculate the values , it works with aggregate Functions like sum ,count ,min , max , avg etc.
Non aggregated columns specification is necessarary in SELECT as well as GROUP BY clause

Syntax –
Select column1,
Aggregate function (column2)
From table
Group by column1

Brand Table

S.NOPRODUCTMONTHYEARQTYSALES
1NIKEDEC2016157599
2REEBOKJAN2014191200
3FILAFEB201786000
4PUMAJAN2015165000
5REEBOKFEB2016199544
6FILAMAR201566252

 

Select Product,
Sum(Qty) as Total_Qty_Sold,
Sum(Sales) as Revenue,
Count(Product) as CountofProduct,
Avg(Qty) as Avg_Qty,
Max(Qty) as Max_Sales,
Min(Qty) as Min_Qty_Sold
From Brand
Group By Product

 
Result

PRODUCTTotal_Qty_SoldRevenueCountofProductAvg_QtyMax of SalesMin_Qty_Sold
FILA14122522762526
NIKE157599115759915
PUMA165000116500016
REEBOK3810744219954419