Sorting and Limiting
Here is an uncomfortable truth about every result you've seen so far: the rows came back in whatever order the engine found convenient — which is to say, in no promised order at all. The films happened to appear in id order because the table is small and young. Nothing guarantees they will tomorrow.
This page adds the two clauses that turn "some rows" into a proper answer. ORDER BY imposes the order you actually mean, and LIMIT keeps only the top of it. Between them they answer half of Lora's daily questions — longest films first, the five newest customers — and they teach a rule worth engraving: "top five" means nothing until something defines top.
No ORDER BY, No Order
Chapter 2 promised that tables have no built-in row order, and this is that promise seen from the query side. Without an ORDER BY, the database is free to return rows in any sequence it likes: whatever it found first on disk, whatever a recent change shuffled around. If the same query has returned the same order a hundred times, that is luck, not contract — and luck runs out precisely when the table grows big enough for you to care.
So treat it as a habit rather than an option: any result a human will read in order gets an ORDER BY. It is one line, and it converts a coincidence into a guarantee.
ORDER BY: Ascending, Descending, Tie-Breaking
Lora wants the schedule board to show the longest films first. In English: show each title and runtime, from the films table, ordered by runtime, biggest first. The keyword DESC — descending — says "biggest first"; its mirror ASC, ascending, is the default and rarely written out:
SELECT title, runtime_min FROM films ORDER BY runtime_min DESC;
| title | runtime_min |
|---|---|
| The Long Rain | 112 |
| Night Bus | 98 |
| Paper Lanterns | 96 |
You can sort by more than one column, and the second column is not a second sort — it is a tie-breaker. ORDER BY rating, title reads out loud as: by rating first, and within each rating, alphabetically by title. The title ordering only ever applies among rows whose ratings are equal; it never reshuffles the rating order. Multi-column sorting is a sequence of tie-breaks, not a blend.
LIMIT Is a Knife, Not a Filter
The Marquee now has 212 customers, and Lora wants to greet the newest ones by name. She needs the five most recent joiners — which is a sort plus a cut. First order everyone by joining date, newest first; then LIMIT 5 slices off the top five rows and discards the rest:
SELECT name, joined_on FROM customers ORDER BY joined_on DESC LIMIT 5;
| name | joined_on |
|---|---|
| Sofia | 2026-07-06 |
| Dev | 2026-07-04 |
| Mira | 2026-07-01 |
| Ben | 2026-06-27 |
| Chloe | 2026-06-24 |
The order of operations is the whole lesson. LIMIT is a knife that cuts the result after sorting — it doesn't search for the newest anything. Run LIMIT 5 without an ORDER BY and you get five rows, certainly, but they are merely some five rows, in the engine's convenience order. "Top 5" only means something because ORDER BY defined top first. Think of race results: the finishing order exists only after the race is scored, and "the top three" without scoring is just three runners standing around. Same here — sort defines top, then the knife cuts. That's the picture; back to the clauses.
One honest engine note: LIMIT is the word in PostgreSQL, MySQL, and SQLite, but SQL Server spells the same idea SELECT TOP 5 …, and the SQL standard's official spelling is a FETCH FIRST 5 ROWS ONLY clause — same knife, three labels.
Where Do NULLs Sort?
One loose end from the missing-data story. Suppose some customers have no joining date on file — where do those NULL rows land in a sorted result? The honest answer: engines disagree; some put NULLs first, some last, and the default can flip between ascending and descending sorts. Because unknown has no natural place in an ordering, the standard lets you say what you mean explicitly: ORDER BY joined_on DESC NULLS LAST pins the unknowns to the bottom, and NULLS FIRST does the opposite. You won't need it often — but the first time a report puts a row of blanks proudly at the top, you'll know both why it happened and the two words that fix it.
- "Tables have a natural order I can rely on." They don't. The same query can return rows in a different order tomorrow, after a change or a restart. If a human will read the result in order — ORDER BY, or it didn't happen.
- "LIMIT 5 gives the smallest or biggest 5." LIMIT alone gives some 5, in the engine's convenience order. Pair it with ORDER BY and it gives the 5 you meant — the sort defines top, the knife just cuts.
- "Sorting by two columns sorts by both at once." The second column only breaks ties in the first. It's a sequence of tie-breaks, not a blend — the first column's order is never disturbed.
- "DESC applies to the whole ORDER BY list." Each column carries its own direction:
ORDER BY rating DESC, titlesorts ratings descending but titles ascending within each rating.
- "Top N by X" is the single most common real-world question shape — best sellers, slowest pages, newest signups. Reports, dashboards, and debugging sessions all live on the sort-then-cut pattern you just learned.
- Ordering is never free: someone has to arrange those rows, and on a big table that costs real work. Chapter 8 puts a price tag on it — and shows the trick that makes it cheap.
Knowledge Check
A query without ORDER BY has returned rows in the same order every day for a month. What can Lora rely on?
- The order is now established and will continue
- Rows always come back in the order they were added
- Nothing — without ORDER BY, any order is luck
- The primary key silently sorts every result
Lora runs SELECT name FROM customers LIMIT 5; with no ORDER BY. What does she get?
- The five newest customers
- The first five customer names in plain alphabetical order
- An error — LIMIT requires ORDER BY
- Some five customers, in the engine's convenience order
What does ORDER BY rating, title do?
- Sorts by rating; within equal ratings, sorts by title
- Blends both columns into one combined sort value
- Sorts by title first because it's alphabetically useful
- Returns two results, one sorted each way
Why does "give me the top five" require ORDER BY to mean anything?
- Because LIMIT refuses to run without a sort clause
- Because "top" has no meaning until a sort defines it; LIMIT only cuts
- Because unsorted LIMITs run too slowly on big tables
- Because only SQL Server actually supports using LIMIT without any ORDER BY
You got correct