SQL Query Logical Processing Order

Last modified date:

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

My name is Jan Zedníček and I work as a BI Developer at Kentico Software in Brno. Mostly you can see me there working in the office, but I also work partly as a freelancer. I have been working as a freelancer for many companies for more than 5 years, but Kentico is the matter of my heart. I used to work as a financial controller at companies like Aero Vodochody or Sberbank and I also used to be a bond program manager in Unicapital Investment group. When I am not at work, I like playing volleyball, chess, doing a workout in the gym and I enjoy tasting of best quality rums. I am trying to summarize all my knowledge on this website not to forget them (because of the rum effect, you know =) and to put them forward to anyone. Don´t worry about asking for help or writing some comments.

Category: SQL Commands SQL Server

About Ing. Jan Zedníček - BI Developer, Finance controller

My name is Jan Zedníček and I work as a BI Developer at Kentico Software in Brno. Mostly you can see me there working in the office, but I also work partly as a freelancer. I have been working as a freelancer for many companies for more than 5 years, but Kentico is the matter of my heart. I used to work as a financial controller at companies like Aero Vodochody or Sberbank and I also used to be a bond program manager in Unicapital Investment group. When I am not at work, I like playing volleyball, chess, doing a workout in the gym and I enjoy tasting of best quality rums. I am trying to summarize all my knowledge on this website not to forget them (because of the rum effect, you know =) and to put them forward to anyone. Don´t worry about asking for help or writing some comments.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.