Sorting and cutting
After this: Sort by one or two columns with ORDER BY and DESC, keep the first rows with LIMIT, and drop repeats with DISTINCT.
On this page · 6 sections · 3 checks
You can now choose columns and rows. The office has two more requests. It wants the class list in alphabetical order, and it wants to give a prize for the three best marks in the school. The first needs the rows sorted. The second needs them sorted and then cut short.
Rows in order
So far the rows have come back in id order: Ana (1), Ben (2), Chloe (3). That is what SQLite usually does, but nothing in those queries asked for it. The clause that asks for an order is ORDER BY:
SELECT nameFROM studentsORDER BY name;| name |
|---|
| Ana |
| Ben |
| Chloe |
| Dev |
| Eve |
| Femi |
Names sort from A to Z, and numbers from smallest to largest. That direction is called , and it is what you get unless you say otherwise. Write DESC after the column for order: largest first, or Z to A.
Two sort columns
Sort the students by year, largest first, by hand:
- Dev and Femi are in year 3, so they come first.
- Ana and Chloe (year 2) come next, then Ben and Eve (year 1).
But which of Dev and Femi comes first? They tie: they have the same year, and ORDER BY year DESC says nothing about the order inside a tie. A second sort column decides it. The rows are sorted by the first column, and only rows that tie on it are sorted by the second:
SELECT name, yearFROM studentsORDER BY year DESC, name;| name | year |
|---|---|
| Dev | 3 |
| Femi | 3 |
| Ana | 2 |
| Chloe | 2 |
| Ben | 1 |
| Eve | 1 |
DESC belongs to the column it follows, so here the years go from largest to smallest and the names inside each year go from A to Z.
Keeping the first few rows
LIMIT 3 keeps the first three rows of the result and drops the rest. For the prize, sort the marks from largest to smallest and keep three:
SELECT student_id, course_id, markFROM enrolmentsORDER BY mark DESCLIMIT 3;enrolments · 10 rows
| student_id | course_id | mark |
|---|---|---|
| 1 | 1 | 72 |
| 1 | 2 | 65 |
| 1 | 3 | 80 |
| 2 | 1 | 58 |
| 2 | 4 | 90 |
| 3 | 1 | 85 |
| 3 | 3 | NULL |
| 4 | 2 | 47 |
| 4 | 4 | 76 |
| 6 | 1 | 91 |
Press Run it, then step through the query one clause at a time, in the order SQLite works.
- rows
- 0
- columns
- 0
ORDER BY works first, so LIMIT cuts a sorted list: the three best marks are 91 (student 6, course 1), 90 (student 2, course 4) and 85 (student 3, course 1). At the ORDER BY step, the last row shows NULL where a mark should be; lesson 4 is about that row.
The order matters. LIMIT 3 without ORDER BY still gives three rows, but which three is not promised.
One copy of each row
The office also wants a list of the cities its students come from. Asking for the city column gives one row per student, with repeats:
SELECT cityFROM studentsORDER BY city;| city |
|---|
| Hull |
| Leeds |
| Leeds |
| Leeds |
| York |
| York |
DISTINCT, written directly after SELECT, keeps one copy of each different row:
SELECT DISTINCT cityFROM studentsORDER BY city;| city |
|---|
| Hull |
| Leeds |
| York |
Run it
Press Run. Then change the sort: put the names from Z to A, sort by city and then name, or add LIMIT 2 at the end.
Check yourself
The six names are Ana, Ben, Chloe, Dev, Eve and Femi. What does this query give back?
SELECT nameFROM studentsORDER BY name DESCLIMIT 2;Two students are in year 1, two in year 2 and two in year 3. How many rows does this query give back?
SELECT DISTINCT yearFROM studentsORDER BY year;Ben and Eve are both in year 1. A query ends ORDER BY year; with no other sort column. In which order do Ben and Eve come back?
- Sort a result with ORDER BY: ascending (A to Z, smallest first) unless you write DESC.
- Break ties with a second sort column, as in ORDER BY year DESC, name.
- Keep the first rows with LIMIT, after the sort, so the top three really are the top three.
- Drop repeated rows with DISTINCT, written directly after SELECT.
- Say that without ORDER BY the order of the rows is not promised.