# Logical Order of a Query

> SQL is written in one order and run in another. Learn the run order and it explains most confusing errors, such as aliases in WHERE.

Subject: SQL | Lesson 22 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/sql/logical-order-of-a-query
Previous: [HAVING vs WHERE](https://codewithanimation.in/sql/having-vs-where.md) | Next: [Subqueries](https://codewithanimation.in/sql/subqueries.md)

## Written order is not run order

You write `SELECT` first, but the database does not run it first. It runs the clauses in a fixed **logical order**: `FROM`, then `WHERE`, then `GROUP BY`, then `HAVING`, then `SELECT`, then `DISTINCT`, then `ORDER BY`, and finally `LIMIT` and `OFFSET`.

Each step takes the table the previous step produced and passes a new table to the next one.

## It explains the rules you just learned

Why can `WHERE` not use `count(*)`? Because groups appear two steps later. Why can `HAVING` use it? Because it runs right after grouping. Almost every "why can I not write this?" in SQL has its answer in this order.

## Follow one query through the steps

```sql
SELECT team, count(*) AS n
FROM players
WHERE points IS NOT NULL
GROUP BY team
HAVING count(*) >= 2
ORDER BY n DESC;
```

`FROM` gives 8 rows. `WHERE` removes Chen and Eli, leaving 6. `GROUP BY` makes 3 groups of 2. `HAVING` keeps groups with at least 2 rows, so all 3 stay. `SELECT` builds the rows (team, n). `ORDER BY` sorts them.

The name `n` is created at the `SELECT` step. That is why `ORDER BY n` works: it runs afterwards. And it is why `WHERE n >= 0` does not work.

## Common mistake: Aliases in WHERE and HAVING

PostgreSQL says: `column "n" does not exist`. At the moment `WHERE` runs, `SELECT` has not run, so no output name exists yet. The manual states it directly: an output name can be used in `ORDER BY` and `GROUP BY`, but not in `WHERE` or `HAVING`, where you must write the whole expression again.

The order shown here is the logical order: the database is free to run things in a different physical order, as long as the answer is the same as if it had followed these steps.

## Tip: When a query surprises you, walk the steps

Write down the table after each step, like the lab does. The step where the rows or groups stop matching your expectation is where the mistake is.

## Try it: Step through a query

Press the steps one at a time and watch the table change. Then switch on Use n in WHERE and step past WHERE to see the error.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/logical-order-of-a-query

## Check yourself

### Question 1

Which list shows the order in which a grouped query is run?

- A. FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY
- B. SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY
- C. FROM, SELECT, WHERE, GROUP BY, ORDER BY, HAVING

Correct answer: A

The logical order is FROM, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, then LIMIT.

- Why not B: That is the order the clauses are written. SELECT is run after HAVING.
- Why not C: SELECT does not run second, and HAVING runs right after GROUP BY, before SELECT and ORDER BY.

### Question 2

`SELECT count(*) AS n FROM players WHERE n > 1;` fails. Why?

- A. WHERE runs before SELECT, so the name n does not exist yet
- B. n is a reserved word
- C. count(*) cannot be given a name

Correct answer: A

Output names are created by SELECT, which runs after WHERE. At the time WHERE runs, there is no n.

- Why not B: n is an ordinary name. The problem is when it is created, not what it is called.
- Why not C: AS n is fine on an aggregate. It can be used in ORDER BY.

### Question 3

Can `ORDER BY n` use the output name n from `SELECT count(*) AS n`?

- A. Yes, because ORDER BY runs after SELECT
- B. No, ORDER BY runs before SELECT
- C. No, only WHERE can use output names

Correct answer: A

By the time ORDER BY runs, SELECT has already created the output name, so ORDER BY can use it.

- Why not B: ORDER BY is one of the last steps. It sorts the rows SELECT has produced.
- Why not C: It is the other way round: WHERE and HAVING cannot use output names, but ORDER BY and GROUP BY can.

## Remember this

- A query runs in this order: FROM, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, LIMIT.
- WHERE sees single rows, so it cannot use aggregates. HAVING runs after grouping, so it can.
- A name created in SELECT exists for ORDER BY, but not for WHERE or HAVING.
- When a query surprises you, check the table after each step to find where it went wrong.
