The Smallest and the Largest
MIN(column) finds the smallest value of a column, MAX(column) the largest one. The cheapest and the most expensive game:
SELECT MIN(price) AS cheapest,
MAX(price) AS most_expensive
FROM games;| cheapest | most_expensive |
|---|---|
| 5 | 60 |
They work on text and dates too. For text, the smallest value is the first one in alphabetical order and the largest the last one, in the same order in which ORDER BY sorts. Dates written as 'YYYY-MM-DD' sort as dates do, so MIN finds the earliest one and MAX the latest:
SELECT MIN(city) AS first_city,
MAX(city) AS last_city,
MIN(joined) AS first_joined,
MAX(joined) AS last_joined
FROM customers;| first_city | last_city | first_joined | last_joined |
|---|---|---|---|
| Bristol | Manchester | 2023-01-15 | 2025-10-03 |
Notice that the first city isn't NULL, although ORDER BY city puts NULL first. Like the other aggregate functions, MIN and MAX skip NULL.
As you can see, one query can have several aggregate functions, and different ones. They all work on the same rows, those that WHERE has left. A short summary of the action games:
SELECT COUNT(*) AS game_count,
MIN(price) AS cheapest,
MAX(price) AS most_expensive,
ROUND(AVG(price), 1) AS average_price
FROM games
WHERE genre = 'Action';| game_count | cheapest | most_expensive | average_price |
|---|---|---|---|
| 4 | 25 | 40 | 32.5 |
Typical mistake: you want the title of the cheapest game and write SELECT title, MIN(price) FROM games;. In SQLite, this happens to work: with MIN and MAX, it takes the other columns from the row with the smallest or the largest value, here Cube Logic. But other databases report an error, and when two games share the lowest price, SQLite shows just one of them. Use what you know from lesson 2 instead, ORDER BY price LIMIT 1, and to see whether another game costs the same, show a few more rows.
Instructions
Show the date of the first and of the last order, in columns first_order and last_order:
| first_order | last_order |
|---|---|
| 2024-01-20 | 2025-12-22 |
Below it, summarize the reviews of Dragon Forge (game_id 1): their number, the fewest and the most stars, and the average stars rounded to 2 decimal places, in columns review_count, fewest_stars, most_stars and average_stars:
| review_count | fewest_stars | most_stars | average_stars |
|---|---|---|---|
| 4 | 3 | 5 | 4.25 |
And from games, show the title that comes first in alphabetical order and the one that comes last, in columns first_title and last_title:
| first_title | last_title |
|---|---|
| Bakery Story | Zombie Town |
Start programming for free
4/10