SQL Basics: the order of logical query processing

Syntax vs Processing order

An important aspect is that the order of keywords in the syntax is different from the order in which it is processed.

The Syntax order is as follows:

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

However, the processing order is different

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

This means that the processing engine starts executing from 1 to 6, and ┬áit can only refer to elements in previous steps. For example, if the engine is at “stage 2 WHERE”, it cannot refer to fields created by the SELECT. The following would throw an error Invalid column name ”Fullname”.


Select age, name + surname as FullName
from dbo.tblPeople
where FullName = 'Joseph Caruana'

Although Fullname is a valid column, the WHERE clause does not know about it yet and that it is why the error is “Invalid column name”.

This basic ordering concept explains several other things. For example, it describes why you cannot filter a GROUP by value (such as SUM) based on the WHERE clause.

Posted in Uncategorized | Leave a comment