SQL HAVING clause

The HAVING clause is used with the GROUP BY clause and filter the groups created by the GROUP BY clause.

Syntax:

SELECT column1, column2,…, columnN FROM tableName WHERE[conditions] GROUP BY column1, column2 …HAVING[conditions];

Example:

SELECT EMP_NAME, SUM(SALARY) FROM EMPLOYEE 
GROUP BY EMP_NAME HAVING Count(EMP_NAME) >= 2;

Output:

EMP_NAME SUM(SALARY)
Parbhjot 81000
Parmender 90000

Position of different clauses in a SQL statement:

1. SELECT
2. FROM
3. WHERE
4. GROUP BY
5. HAVING
6. ORDER BY

No comments: