Logical Query Processing Phase


I think this is one the most import concept to master when learning SQL.

Keyed-in order: the syntax order
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY

Logical Query Processing (with easy to remember phrase, credit to my co-worker): the order logically query is processed
FROM           -- For
WHERE        -- Wives
GROUP BY   -- Good
HAVING        -- Husbands
SELECT        -- Sing
ORDER BY   -- Often

Logical Query Processing detail
FROM
JOINS    
WHERE    
GROUP BY  
HAVING      
SELECT
Column, and Expressions
Window Functions
Prepare Aliases for final output
DISTINCT  
ORDER BY  
TOP/LIMIT

Each phase output is served as input for the next phase
For optimization, SQL Server may process the query in any order.

Different between WHERE and HAVING clause:
WHERE evaluated before rows are grouped.
HAVING evaluated after rows are grouped.

SELECT statement:
All-at-once manner, no left to right
That mean this query will fail
SELECT 2*4 AS x, 2*x AS [2x]; --error invalid column x



Comments

Popular posts from this blog

XPath Expression Part I

Fun with NULL

Transforms Rows Into XML Elements: FOR XML AUTO