Learn SQL: MIN / MAX
The smallest and the largest
"Which bike has done the most riding? And when did a bike last get a service?" Lien wants to know the extremes, and you look them up.
MIN and MAX
MIN gives the smallest value in a column, MAX the largest. You can put several aggregate functions in one SELECT: you still get one row.
SELECT MIN(day_price) AS cheapest, MAX(day_price) AS dearest FROM bikes| cheapest | dearest |
|---|---|
| 6 | 40 |
For text and dates too
MIN and MAX also work on text (alphabetically) and on dates written as 'year-month-day'. The smallest date is the oldest, the largest the most recent.
They skip NULL: bikes that have never been serviced don't count here.
SELECT MIN(last_service) AS oldest_service, MAX(last_service) AS latest_service FROM bikes| oldest_service | latest_service |
|---|---|
| 2024-09-03 | 2025-06-11 |
The value, not the row
MAX(odometer_km) tells you how many kilometres, not which bike. If you want the whole row, you sort and keep just one, as you learnt at the festival.
SELECT brand, type, odometer_km FROM bikes ORDER BY odometer_km DESC LIMIT 1| brand | type | odometer_km |
|---|---|---|
| Gazelle | e-bike | 15990 |
Try it yourself
Show the lowest and the highest amount a rental brought in, in one query.
Ready for more?
The full lesson has 6 exercises, from easy to challenging, with hints, points and a streak you can build.
Start the lesson