SQL – Having Clause

Spread the love

Having clause – it helps to filter the values basis on aggregated value’s comparisons, within the queries and generally located in the end.
It supports arithmetic functions and compares the results with the help of operator.

Functions –    sum, average, count, min, max etc.
Compare like –     > , < , <> , = etc.

Syntax –
Select column1 , function(column2)
from mytable
Group by column1
Having function(column any ) ; use operator

 

Brand Table

S.NOPRODUCTMONTHYEARQTYSALES
1NIKEDEC2016107599
2REEBOKJAN2014181200
3FILAFEB201786000
4PUMAJAN2015165000
5REEBOKFEB2016209544
6FILAMAR201566252

 

Select Product,
Sum(qty) as Total_Qty_Sold
from Brand
Group by Product
Having Sum(qty) > 15 ;

Result

ProductTotal_Qty_Sold
PUMA16
REEBOK38