Chapter 8 · Lesson 1 of 8

Written Order vs Logical Order

Learn the logical order SQL uses to evaluate a query, from FROM through LIMIT, and why it differs from the order you write clauses.

SQL is written one way but logically evaluated in another order. A useful mental model is:

FROM
JOIN
WHERE
GROUP BY
HAVING
WINDOW FUNCTIONS
SELECT
DISTINCT
ORDER BY
LIMIT

This is a teaching model for how to reason about queries. Real database engines optimize internally, and some dialects allow convenience shortcuts, but the logical order explains most SQL behavior.

This matters because each step can only use what earlier steps have already produced. For example, in standard SQL WHERE cannot use a SELECT alias because SELECT has not happened yet.

When a query feels confusing, try reading it in logical order instead of written order. That often explains why a clause works — or why it does not.

This lesson is theory-focused. Read it, then mark it complete in the Learn SQL panel before moving on.

Open this lesson in the SQL editor

Lessons in Execution Order

Understand the logical order in which SQL evaluates FROM, WHERE, GROUP BY, HAVING, window functions, SELECT, ORDER BY, and LIMIT.

  1. 01 · Written Order vs Logical Order
  2. 02 · FROM and JOIN Build the Rows First
  3. 03 · WHERE Filters Before GROUP BY
  4. 04 · HAVING Filters After GROUP BY
  5. 05 · Where Window Functions Fit
  6. 06 · Why Window Functions Cannot Go Directly in WHERE
  7. 07 · Why SELECT Aliases Work in ORDER BY
  8. 08 · LIMIT Happens at the End

Related guides