Learn SQL Lesson

Written Order vs Logical Order

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.

Open interactive editor