Execution sequence of Group By, Having and Where clause in SQL Server?
I am just confused with the execution sequence of a SQL query when we use GROUP BY and HAVING with a WHERE clause. Which one gets executed first? What is the sequence?
WHERE is first, then you GROUP the result of the query, and last but not least HAVING-clause is taken to filter the grouped result. This is the "logical" order, I don't know how this is technically implemented in the engine.
I think it is implemented in the engine as Matthias said: WHERE, GROUP BY, HAVING
Was trying to find a reference online that lists the entire sequence (i.e. "SELECT" comes right down at the bottom), but I can't find it. It was detailed in a "Inside Microsoft SQL Server 2005" book I read not that long ago, by Solid Quality Learning
FROM & JOINs determine & filter rows WHERE more filters on the rows GROUP BY combines those rows into groups HAVING filters groups ORDER BY arranges the remaining rows/groups LIMIT filters on the remaining rows/groups
You can check order of execution with examples from this article.
For you question below lines might be helpful and directly got from this article.
GROUP BY --> The remaining rows after the WHERE constraints are applied are then grouped based on common values in the column specified in the GROUP BY clause. As a result of the grouping, there will only be as many rows as there are unique values in that column. Implicitly, this means that you should only need to use this when you have aggregate functions in your query.
HAVING --> If the query has a GROUP BY clause, then the constraints in the HAVING clause are then applied to the grouped rows, discard the grouped rows that don't satisfy the constraint. Like the WHERE clause, aliases are also not accessible from this step in most databases.