Sorting and the top 3
Find the top 3 books: the longest, or the oldest.
ORDER BY sorts (add DESC for largest first) and LIMIT keeps the first few rows.
Show the title and year of the 3 oldest books, oldest first.
| book_id | title | author | genre | year | pages |
|---|---|---|---|---|---|
| 1 | Pride and Prejudice | Jane Austen | Romance | 1813 | 432 |
| 2 | The Great Gatsby | F. Scott Fitzgerald | Fiction | 1925 | 180 |
| 3 | Dracula | Bram Stoker | Horror | 1897 | 418 |
| 4 | Mockingjay | Suzanne Collins | Science Fiction | 2010 | 390 |
| 5 | The Time Machine | H.G. Wells | Science Fiction | 1895 | 118 |
| 6 | Angels & Demons | Dan Brown | Thriller | 2000 | 616 |
| 7 | Charlotte's Web | E.B. White | Children | 1952 | 184 |
| 8 | The Fault in Our Stars | John Green | Romance | 2012 | 313 |
| 9 | The Fellowship of the Ring | J.R.R. Tolkien | Fantasy | 1954 | 423 |
Run your query to see the result table.
Press the green Run button or Ctrl+Enter.
How ORDER BY, DESC, LIMIT works
YOU WRITE
- SELECT
- FROM
- WHERE
- GROUP BY
- HAVING
- ORDER BY
- LIMIT
SQL WORKS THROUGH IT
- 1. FROM take the table
- 2. WHERE keep some rows
- 3. GROUP BY make piles
- 4. HAVING keep some piles
- 5. SELECT pick and work out columns
- 6. ORDER BY sort
- 7. LIMIT keep the first few
ORDER BY and LIMIT come last, after SELECT. That is why ORDER BY can use an AS name everywhere, while WHERE can't in most databases.
How does
ORDER BYsort?ORDER BY pagessorts the result by pages, smallest first. Text sorts A to Z. AddDESC(descending) for largest first, or Z to A.What does
LIMITdo?LIMIT 3keeps the first 3 rows of the result. Together withORDER BYit answers "top 3" and "bottom 3" questions.Does the order of the parts matter?
Yes. The parts always go in this order:
SELECT...FROM...WHERE...ORDER BY...LIMIT.Is the order of rows fixed without
ORDER BY?No. Without
ORDER BYa database can return rows in any order, and it may change. If the order matters, always sort.How is this like Excel?
=TAKE(SORT(B2:F11, 5, -1), 3)does the same: sort by the fifth column, largest first, and keep 3 rows.
How other databases do it
The lessons run on SQLite. Most of SQL is the same everywhere; these are the places where this lesson's SQL differs.
| Database | How | Note |
|---|---|---|
| SQLite (these lessons) | ORDER BY pages DESC LIMIT 3 | |
| PostgreSQL | ORDER BY pages DESC LIMIT 3 | The standard FETCH FIRST 3 ROWS ONLY works too. |
| MySQL / MariaDB | ORDER BY pages DESC LIMIT 3 | |
| SQL Server | SELECT TOP 3 title, pages FROM ... ORDER BY pages DESC | Or OFFSET 0 ROWS FETCH NEXT 3 ROWS ONLY after ORDER BY (SQL Server 2012 and later). |
| Oracle | ORDER BY pages DESC FETCH FIRST 3 ROWS ONLY | Oracle 12c and later. Older versions filter on ROWNUM around a sorted subquery. |
| Sybase ASE | SELECT TOP 3 title, pages FROM ... ORDER BY pages DESC | Or SET ROWCOUNT 3 before the query. |
| Database | How | Note |
|---|---|---|
| SQLite (these lessons) | First | Write NULLS LAST to move them. |
| PostgreSQL | Last | NULL counts as larger than any value. NULLS FIRST / NULLS LAST change it. |
| MySQL / MariaDB | First | No NULLS LAST: sort on pages IS NULL first, then pages. |
| SQL Server | First | No NULLS LAST: sort on CASE WHEN pages IS NULL THEN 1 ELSE 0 END first. |
| Oracle | Last | NULL counts as larger than any value. NULLS FIRST / NULLS LAST change it. |
| Sybase ASE | First | No NULLS LAST: sort on a CASE expression first, as in SQL Server. |
Know someone fighting with a spreadsheet? Send them this page.