When executing a query, SQL Server will process the different clauses of that query in a very particular order.
Other programming languages like C++ or C# will process code in a top-down manner. SQL Server does not work that way.
Take a look at the following two tables, Products and Orders:
Using this data, I’ll construct a query that uses several clauses:
This query uses many clauses:
- SELECT
- FROM
- WHERE
- GROUP BY
- HAVING
- ORDER BY
This is the order in which we read the query, but it is not the order in which SQL Server processes the query
SQL Server will actually process the query in the following order:
- FROM (which includes the JOIN)
- WHERE
- GROUP BY
- HAVING
- SELECT
- ORDER BY
To better understand SQL Server logical processing order, let’s talk about an analogy of giving instructions to your significant other when you need them to pick up something from the store. You might say, “Darling, can you pick up sesame oil from the grocery store? It should be in the international isle.”
You’ve outlined what you need first, followed by where it can be found.
But think about if you wanted to outline these instructions in the order they should be executed. In that case, you might say “Go the the grocery store. Go down the international isle. Locate sesame oil. Bring it home. ”
You’ve outlined where to go first, followed by what you need from that location.
You and I read the query like the first example, where we outline what we want first. But SQL Server reads the query the other way, where it determines where to go first.
Knowing a thing or two about logical query processing order can help us understand a few factoids about SQL Server
The first factoid is how the only place a column alias can be referenced is in the ORDER BY clause. Column aliases are created in the SELECT clause, which is nearly last in the logical processing order. The only clause that comes after it (and therefore the only clause that knows of the existence of an alias) is the ORDER BY clause.
Another factoid is how we need to use the HAVING clause to filter the results of an aggregation. We cannot filter the results of an aggregation using the WHERE clause. If we look at the processing order, we see that the WHERE clause is only second in the processing order. It knows nothing about groupings yet. Groupings aren’t established until the GROUP BY clause, of course. After groupings have been established, then we can filter on those groups using the HAVING clause.
Next Steps:
Leave a comment if you found this tutorial helpful!
You should consider getting the book T-SQL Fundamentals by Itzik Ben-Gan. It goes over several core concepts you should know about Microsoft SQL Server, including a full discussion on logical query processing order. This book helped me out tremendously when I was first starting out with SQL Server, and I reference it all the time now. You won’t regret owning this book, trust me. Get it today!
Are you following these 7 best practices in SQL Server? Click the link to find out!
Thank you very much for reading!
Make sure you subscribe to my newsletter to receive special offers and notifications anytime a new tutorial is released!
If you have any questions, please leave a comment. Or better yet, send me an email!
>>To better understand SQL Server logical processing order, let’s talk about an analogy of giving instructions to your significant other
(and why clarity matters!)
***JOKE:***
WIFE: stop off at the store and get a loaf of bread and if they have eggs, get a dozen.
HUSBAND: returns from store witha dozen loaves of bread
WIFE: Why did you get 12 loaves?
HUSBAND: That’s what you told me to do!