Lesson 4 of 30 · Querying Basics
SQL ORDER BY and LIMIT: Sort Results and Page Through Rows
Without instructions, a database returns rows in whatever order is convenient for it — often the order they were added, but that is not guaranteed. Leaderboards, "newest first" lists and "cheapest first" shops all need a fixed order, and SQL gives you one with ORDER BY. LIMIT then keeps just the first few rows.
Sorting with ORDER BY
ORDER BY goes after FROM (and after WHERE, if there is one):
SELECT name, gold FROM heroes ORDER BY gold DESC;
| name | gold |
|---|---|
| Hugo Stonebeard | 1200 |
| Cyra Moonweaver | 910 |
| Nox Grimsby | 880 |
| … | … |
ASCsorts from low to high. It is the default, soORDER BY goldmeansORDER BY gold ASC.DESCsorts from high to low.
Numbers sort by value and text sorts alphabetically. Dates written as YYYY-MM-DD text, like the joined column, sort from oldest to newest.
Sorting by several columns
List several columns, separated by commas. The database sorts by the first one; the second only breaks ties:
SELECT name, class, level FROM heroes ORDER BY class, level DESC;
| name | class | level |
|---|---|---|
| Oona Fernwhistle | Healer | 20 |
| Elara Brightwell | Healer | 12 |
| Iris Dawnpetal | Healer | 7 |
| Cyra Moonweaver | Mage | 21 |
| … | … | … |
Classes are sorted A to Z, and inside each class the highest level comes first. Every column has its own direction: DESC belongs only to the column right before it.
LIMIT and OFFSET
LIMIT n returns at most n rows. Together with ORDER BY it answers "top N" questions — here, the three best-paying quests:
SELECT title, reward FROM quests ORDER BY reward DESC LIMIT 3;
This returns Dragon Sighting (800), The Bandit King (500) and The Clockwork Golem (450). OFFSET n skips the first n rows before the limit is applied, which is how websites show results page by page:
-- page 2, three quests per page
SELECT title, reward FROM quests ORDER BY reward DESC LIMIT 3 OFFSET 3;
For page p with k rows per page, use LIMIT k OFFSET (p - 1) * k.
The clauses always come in this order: SELECT … FROM … WHERE … ORDER BY … LIMIT … OFFSET ….
Common mistakes
LIMITwithoutORDER BY. You get some three rows, not the top three. Always sort first.- Clauses in the wrong order.
LIMIT 3 ORDER BY goldis a syntax error —ORDER BYcomes beforeLIMIT. - Expecting
DESCto apply to every column.ORDER BY class, level DESCsorts the classes A to Z; onlylevelis descending. OFFSETon its own. SQLite only acceptsOFFSETafter aLIMIT.
Assignment
The market's catalogue lists the items table sorted by type (A to Z) and, within the same type, from the most expensive to the cheapest. It shows 5 items per page.
| id | name | type | price | rarity |
|---|---|---|---|---|
| 5 | Leather Vest | armor | 80 | common |
| 9 | Mana Potion | potion | 30 | common |
| 11 | Scroll of Fireball | scroll | 90 | rare |
| … | … | … | … | … |
Show page 2 of the catalogue: skip the first 5 items and return the next 5. Return the columns type, name and price, in this order, sorted exactly like the catalogue.
Keep learning
Need a quick reminder? The SQL cheat sheet has every topic of Learn SQL on one page.
Lesson last updated on .