TechnoLearnAcademy

TECHNOLEARN

SQL HAVING Clause

THE HAVING Clause is used in conjunction with the group by clause to apply conditions on aggregate functions.
The Having clause is always used after the group by clause.

Syntax:      

                SELECT  column1 , aggregate_function(column2)                                                                                                                                                                                                       FROM tablename                                                                                                                                                                                 GROUP BY column1                                                                                                                                                                              HAVING condition(s)                                                                                                                                                                            ORDER BY column1; 
Let’s understand the having clause with an example. For demonstration purposes, we will be using the table emp.
         Table Emp

Example:

   Objective: In this example, we will count the number of employees in each department where the count is greater than                         10 .
                     SELECT Department , COUNT(Ecode) as [TotalCount] FROM Tbl_Emp
                                                        GROUP BY Department                                                                                                                                                                          HAVING COUNT(Ecode)>10
                                                                ORDER BY Department;
    Output:
Note: Where clause always comes before the group by clause and having clause always comes after the group by clause.
Scroll to Top