0d: 00h: 00m: 00s

🎁 Get 3 extra months of learning — completely FREE

That's 90 extra days to master new skills, build more projects, and land your dream job.

Become a PRO
Background Image

🎁 Get 3 extra months of learning completely FREE

Become a PRO
Tom Savino
PRO
last month
Tomcountry asked

Is the WHERE clause always used before GROUP BY in SQL?

Abhay Jajodia
Expert
2 days ago
Abhay Jajodia answered

Hi Tom,

Yes, exactly — WHERE always comes before GROUP BY in SQL.

Here’s why: the WHERE clause filters the rows before any grouping happens. So only the rows that match your condition are included in the groups.

Let’s say you have this query:

SELECT department, MIN(age) AS min_age, MAX(age) AS max_age
FROM Employees
WHERE department <> 'Marketing'
GROUP BY department;

First, SQL filters out all rows where the department is 'Marketing'. Then it groups the remaining rows by department and calculates the min and max age for each group.

If you applied the WHERE after GROUP BY, it wouldn’t work — because WHERE doesn’t operate on groups. That’s what HAVING is for, if you ever need to filter after grouping.

So yes, WHERE always comes first.

If you have more questions, I am here to help.

SQL
This question was asked as part of the Learn SQL Basics course.