VCF

Unit 7 · Data that lasts

SQL clause order: SELECT, FROM, WHERE, ORDER BY

SQL clauses go in a fixed order: SELECT the columns, FROM a table, WHERE to filter rows, then ORDER BY to sort. Put them in another order and the query fails.

The mix-up

What people often think

“The parts of a query can go in any order.”

What the tutor says

SQL has a fixed order: SELECT, then FROM, then WHERE, then ORDER BY. Move the WHERE before the ORDER BY.

How it works

Without ORDER BY, the database promises no order at all. Rows might come back newest first today and in a different order tomorrow, so ask for the order you need.

Side by side

The mix-upSQL
SELECT name FROM customers ORDER BY name WHERE city = 'Toronto';
ORDER BY before WHERE: syntax error.
What worksSQL
SELECT name FROM customers
WHERE city = 'Toronto'
ORDER BY name;
Select, from, where, order by.

Close cousins of this mix-up

The tutor watches for these in the same lesson.

  • “Rows come back newest first on their own.”

    Without ORDER BY, the order is not promised. Add ORDER BY created_at DESC.

Practise it with a tutor watching

Tables and rows

Read a database table and write SELECT, INSERT and a safe UPDATE in SQL.

20 minutes See it Build it Break it Say it
Start the lesson

Free, no account needed.