General Note:

Here we have pulled in the data from a sample database. You can download it from the following link and use it for practicing Click Here For Sample Database

In this tutorial, you will learn the use of HAVING keyword in SQLite.

HAVING is used to limit the result returned by the group by keyword. Say for example, in the below query we grouping brands and their os type to get the available quantity in store


Select brand_name,os_name,count(*) 
from 
mobile_sales_details 
group by brand_name,os_name

brand_name  os_name                    count(*)  
----------  -------------------------  ----------
Alcatel     Android v4.2 (Jelly Bean)  1         
Apple       iOS 6                      2         
Apple       iOS 7                      3         
Asus        Android v4.3 OS            4         
BlackBerry  BlackBerry 10 OS           2         
BlackBerry  BlackBerry 10.2.1 OS       1         
BlackBerry  BlackBerry 7.1 OS          1         
BlackBerry  BlackBerry OS              1         
Karbonn     Android v4.2 (Jelly Bean)  3         
Karbonn     Android v4.4 (KitKat) OS   2         
Lava                                   1         
Lava        Android v4.2 (Jelly Bean)  1         
Lava        Android v4.3 OS            1         
Lava        Android v4.4 (KitKat) OS   2         

In the above query it displayed result as brand name and their OS name with available OS count. However, it displayed counts as 1 ,2,3, but I need to know only brand and their OS count must be greater than 3.

We cannot use where to restrict group result. So here we using having to restrict the result of group function


Select sum(price),min(price),max(price),brand_name,count(*) 

from mobile_sales_details group by  brand_name having count(*) > 3

sum(price)  min(price)  max(price)  brand_name  count(*)  
----------  ----------  ----------  ----------  ----------
221811      21712       62500       Apple       5         
45996       5999        16999       Asus        4         
102595      9125        43490       BlackBerry  5         
26295       2699        11999       Karbonn     5         
40456       1210        12749       Lava        5         
46950       3490        15990       Lenovo      5         
35385       882         9074        Micromax    5         
74694       1249        17190       Nokia       8         
256522      1299        54300       Samsung     16        
84119       6150        28999       Sony        5