Add up and count

INPUT · Slides

The biggest and the smallest

01 / 04

You want the top figure

"What did the dearest thing cost?" was something ORDER BY and LIMIT 1 could already answer in chapter 1. But that was a way of fetching a row.

If the value itself is what you want, an aggregate function gets it directly. MAX and MIN.

02 / 04

MAX and MIN

MAX(column) gives the largest, MIN(column) the smallest. No sorting needed, and the handy part is that you can write both at once.

One row comes back, holding just the values.

SELECT MAX(price), MIN(price)  FROM sales;

Result

2400 | 400

03 / 04

They work on text

MAX and MIN are not only for numbers. They work on text columns too, and then you get the first and last value in character-code order.

Capitals sit before lower case in that order, so a mix of the two will not line up the way a dictionary does. Even so, it is a good way to see where the range ends.

SELECT MIN(item), MAX(item)  FROM sales;

Result

ceramic vase | wooden spoon

04 / 04

They work on dates

Store a date in the shape 2026-05-02 and character order is date order. So MIN(day) is the first day and MAX(day) the last.

It lets you check "what period is this data from?" in one go — a shape you use constantly in real work. Combining it with WHERE or an expression works as before. Let us write some.

SELECT MIN(day), MAX(day)  FROM sales;

Result

2026-05-02 | 2026-05-03