In this tutorial you will learn how to use SQL Having Clause with Group by
When you need having clause in SQL Statement? Just assume you have a large number of records in a table, and there are many duplicate email ids (or any other column value), you want to find out which are those records, in such scenario you can use having clause.
HAVING clause in SQL was added because the WHERE keyword could not be used with aggregate functions.
SELECT columnName(s) FROM table_name WHERE condition GROUP BY columnName(s) HAVING condition ORDER BY columnName(s);
In below screenshot you can see how having clause is been used to find the duplicate email ids
Here is simple example of using having clause with group by in sql query.
SELECT COUNT(Email),Email FROM tbStudent GROUP BY Email HAVING COUNT(Email) > 1
Now you can also add order by clause in above query, see how to use "having count" with group by query.
SELECT COUNT(Email),Email, Name FROM tbStudent GROUP BY Email HAVING COUNT(Email) > 1 Order By Email