Tom
asked

Expert
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.
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer