Smallest and Largest
MIN and MAX.
What's the cheapest book? What's the most expensive? When did the first customer join? These are all questions about the smallest, or the largest value in a column.

- Cheapest and dearest book
- First and newest customer
- Smallest and largest
MIN and MAX
Min gives the smallest value in a column, and max gives the largest. You can ask for both in the same query. Run it. The cheapest book is eight ninety nine, and the most expensive is thirty four ninety nine.
SELECT MIN(price) AS cheapest,
MAX(price) AS dearest
FROM books;โโโโโโโโโโโโฌโโโโโโโโโโ โ cheapest โ dearest โ โโโโโโโโโโโโผโโโโโโโโโโค โ 8.99 โ 34.99 โ โโโโโโโโโโโโดโโโโโโโโโโ
They work on dates too
Min and max also work on dates, and even on text. On dates, min means the earliest, and max means the latest. Run it. The first customer joined in January twenty twenty three, and the newest in June twenty twenty five.
SELECT MIN(joined) AS first_customer,
MAX(joined) AS newest_customer
FROM customers;โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโ โ first_customer โ newest_customer โ โโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค โ 2023-01-04 โ 2025-06-19 โ โโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโ
Aggregates with WHERE
Aggregates work happily with where. The where runs first, keeping only fantasy books. Then the count and sum work on what's left. Run it. Three fantasy titles, with eighty five copies on the shelves.
SELECT COUNT(*) AS books,
SUM(stock) AS copies
FROM books
WHERE genre = 'Fantasy';โโโโโโโโโฌโโโโโโโโโ โ books โ copies โ โโโโโโโโโผโโโโโโโโโค โ 3 โ 85 โ โโโโโโโโโดโโโโโโโโโ
Summary
Min, max, count, sum, and average. Those five aggregate functions answer a huge number of business questions. Let's practise them.
Key takeaways- MIN() smallest
- MAX() largest
- WHERE runs first