Sort and limit results
A raw query returns rows in whatever order the database feels like - which is
rarely the order you actually want. ORDER BY fixes that: it sorts the result
by one or more columns, ascending by default (ASC) or descending if you add
DESC.
LIMIT trims the result down to a fixed number of rows. The two work together
constantly - "top 5", "oldest first", "most recent 10" are all ORDER BY plus
LIMIT. Order the rows the way you want, then cut off everything past the
number you need.
Same users table as before: id, name, country, age.
Your task: return the name and age of the two oldest users, oldest first.
You'll practice:
- Sorting results with
ORDER BY column DESC
- Capping the number of rows with
LIMIT
Related reading: Filtering & Sorting: WHERE, ORDER BY, LIMIT →