The order in which are the SQL clauses entered into the query are quite known. However, the order of logical query processing is different than in which we enter it into the script.

SQL clauses and their order in the script:

  1. SELECT
  2. FROM
  3. WHERE
  4. GROUP BY
  5. HAVING
  6. ORDER BY

but SQL query is logically processed in different order:

  1. FROM
  2. WHERE
  3. GROUP BY
  4. HAVING
  5. SELECT
  6. ORDER BY

Take clause SELECT as an example. We can see it is processed as penultimate even though it must be on the first place in the script. That means that all the preceding clauses FROM (1), WHERE (2), GROUP BY (3), HAVING (4) do not see what happens in SELECT (5) clause. Only clause ORDER BY (6) sees that. What impact does it have is shown in the example.

Logical SQL Query Processing Example

Let us take a look at the script which is wrong on purpose:

SELECT
  [CustomerKey] AS Customer_ID
  ,[FirstName] AS First_NAme
  ,[LastName] AS Last_Name
  ,[BirthDate] AS Alias_Birth_Date
FROM [AdventureWorksDW2014].[dbo].[DimCustomer]
WHERE Alias_Birth_Date > '1940-01-01'

This query will result in an error because we defined alias “Alias_Birth_Date” in SELECT (5) and then we use this alias in clause WHERE (2). WHERE clause was already processed by the time the alias was created in the SELECT clause (so it doesn’t know it).

logical query processing error

The situation will be different if we apply alias in ORDER BY. SELECT (5) is processed earlier than ORDER BY (6) therefore alias will be known and the query will be successful.

logical query processing - alias in order by clause

Rate this post

Ing. Jan Zedníček - Data Engineer & Controlling

My name is Jan Zedníček and I have been working as a freelancer for many companies for more than 10 years. I used to work as a financial controller, analyst and manager at many different companies in field of banking and manufacturing. When I am not at work, I like playing volleyball, chess, doing a workout in the gym.

🔥 If you found this article helpful, please share it or mention me on your website

Leave a Reply

Your email address will not be published. Required fields are marked *