Back to Developer Roadmap

Where Vs Having

src/data/question-groups/sql-queries/content/where-vs-having.md

4.01.1 KB
Original Source

You use WHERE for filtering rows before applying any grouping or aggregation. The code snippet below illustrates the use of WHERE. It filters the Users table for rows where the Age is greater than 18.

sql
SELECT * FROM Users
WHERE Age > 18;

The result of the query is similar to the table below.

userIdfirstNamelastNameage
1JohnDoe30
2JaneDon31
3WillLiam25
4WadeGreat32
5PeterSmith27

On the other hand, you use HAVING to filter groups after performing grouping and aggregation. You apply it to the result of aggregate functions, and it is mostly used with the GROUP BY clause.

sql
SELECT FirstName, Age FROM Users
GROUP BY FirstName, Age
HAVING Age > 30;

The code above selects the FirstName and Age columns, then groups by the FirstName and Age, and finally gets entries with age greater than 30. The result of the query looks like this:

firstNameage
Wade32
Jane31