Tony Bater (9/8/2010)
In a recent blog, Kalen Delaney said she is frequently asked why is there a need to understand the internals of SQL Server. I think this question illustrates why. The key to getting the right answer is an understanding of the sequence of events during the query processing - the HAVING clause is processed after the GROUP BY and acts on the result set from the earlier phases.
I learned this from Itzik Ben-Gan's book 'Inside Microsoft SQL Server 2008: T-SQL Querying'. Chapter 1, Logical Query Processing, has a great section on the sequence of events that occurs when the query processor gets to work. I really recommend this book to anyone who is struggling to understand the explanation for this QotD.
Strictly speaking, this is not about the internals of SQL Server, but about the logical stucture of T-SQL. First and foremost, you need to understand the logical schema, because that's the only way you can write code that fits the schema, as well as tell if a given coding problem is your error or Microsoft's :-P
The primary reason to understand the internals of SQL Server comes after that -- so you can know how to write queries that are not only logically correct, but that will perform well within the constraints of SQL Server and your environment.
In other words, you learn the logical process to write code that works, and then learn the actual internal process so you can write code that works well.
(Disclaimer: I got the question wrong, because I have much to learn about both subjects