The order the database works in
After this: Follow a query clause by clause in working order, and explain why ORDER BY can use an alias and WHERE cannot.
On this page · 5 sections · 4 checks
You now know five clauses: SELECT, FROM, WHERE, ORDER BY and LIMIT. A query writes them in that order, with SELECT first. But think about what SELECT name, year asks: name and year of which rows, from which table? The database cannot answer that until it has read FROM and WHERE. So it does not work in the order the query is written.
Written order and working order
| Written | Worked through |
|---|---|
1. SELECT | 1. FROM: start with the table |
2. FROM | 2. WHERE: keep the rows that pass the test |
3. WHERE | 3. SELECT: keep and work out the columns |
4. ORDER BY | 4. ORDER BY: sort the rows |
5. LIMIT | 5. LIMIT: keep the first few |
Only SELECT moves: it is written first and worked third.
One query, step by step
Here are all five clauses in one query: the Leeds students, highest year first, then by name, two of them.
SELECT name, yearFROM studentsWHERE city = 'Leeds'ORDER BY year DESC, nameLIMIT 2;students · 6 rows
| id | name | year | city |
|---|---|---|---|
| 1 | Ana | 2 | Leeds |
| 2 | Ben | 1 | York |
| 3 | Chloe | 2 | Leeds |
| 4 | Dev | 3 | Hull |
| 5 | Eve | 1 | York |
| 6 | Femi | 3 | Leeds |
Press Run it, then step through the query one clause at a time, in the order SQLite works.
- rows
- 0
- columns
- 0
FROM studentsstarts with all 6 rows.WHERE city = 'Leeds'keeps Ana, Chloe and Femi. The other three are struck out.SELECT name, yearkeeps two columns: (Ana, 2), (Chloe, 2), (Femi, 3).ORDER BY year DESC, nameputs Femi first (year 3), then Ana and Chloe (year 2, A to Z).LIMIT 2keeps (Femi, 3) and (Ana, 2).
What the order explains
ORDER BY can use a SELECT alias. ORDER BY runs after SELECT, so the names that AS gives already exist:
SELECT name AS nFROM studentsWHERE id <= 2ORDER BY n;| n |
|---|
| Ana |
| Ben |
WHERE cannot, in standard SQL. WHERE runs before SELECT, so an alias does not exist yet when the rows are tested. The way that works in every database is to repeat the expression:
SELECT name, year * 10 AS scoreFROM studentsWHERE year * 10 > 15;| name | score |
|---|---|
| Ana | 20 |
| Chloe | 20 |
| Dev | 30 |
| Femi | 30 |
WHERE can test a column that SELECT leaves out. In the step-by-step query, WHERE city = 'Leeds' tests the city, and the result has no city column. The test was done before the columns were chosen.
LIMIT cuts last. It cuts a list that is already sorted, so "the top two" really are the top two.
Run it
Press Run. Then move things around: change LIMIT 2 to LIMIT 1, sort by name alone, or test year >= 2 in place of the city.
Check yourself
A query has SELECT, FROM, WHERE, ORDER BY and LIMIT. In which order does the database work through them?
Students: Ana (year 2, Leeds), Ben (1, York), Chloe (2, Leeds), Dev (3, Hull), Eve (1, York), Femi (3, Leeds). Which rows are left after the WHERE step of this query?
SELECT nameFROM studentsWHERE year = 1 OR city = 'Hull'ORDER BY name DESCLIMIT 2;Now the whole query. What does it give back?
SELECT nameFROM studentsWHERE year = 1 OR city = 'Hull'ORDER BY name DESCLIMIT 2;SQLite runs this query, but PostgreSQL refuses it and says there is no column score. Why, and what is the fix that works everywhere?
SELECT name, year * 10 AS scoreFROM studentsWHERE score > 15;- Say the order the database works through a query in: FROM, WHERE, SELECT, ORDER BY, LIMIT.
- Follow a query step by step and say which rows and columns are left after each clause.
- Use a SELECT alias in ORDER BY, and repeat the expression in WHERE, where the alias does not exist yet.
- Say that WHERE can test a column the result leaves out, because it runs before SELECT.