Learn SQL
-- aggregations.min_max

MIN & MAX in SQL

Smallest and largest — numbers, dates, even text

--concept

MIN and MAX return the extreme value of a column across matching rows. They work beyond numbers: MIN(hire_date) is the earliest hire, MAX(name) the alphabetically last name. One caveat: aggregates collapse rows, so “the value” is easy but “WHO has it” needs an ORDER BY … LIMIT 1 follow-up (or a window function).

--how it works
1

Extremes of a column

MIN(salary), MAX(salary) — one scan, both answers.

2

They work beyond numbers

Dates: earliest/latest; text: alphabetically first/last — comparisons follow the type.

3

Scope with WHERE

Extremes within a slice: MAX(salary) WHERE department = 'Sales'.

4

Group for per-bucket extremes

GROUP BY department + MAX(salary) = the top salary in each team.

5

The owner needs a follow-up

Aggregates collapse rows, so "WHO has the max" needs ORDER BY … DESC LIMIT 1 — or a window function.

--visualization
WHERE department =
Alice85,000Bob52,000Cynthia91,000David47,000Elena58,000Farid73,000
maximum minimum others
Stage 1 / 8
MIN(salary) and MAX(salary) over the whole table.
--example
query.sql
1SELECT MIN(salary) AS lowest,
2 MAX(salary) AS highest
3FROM employees;
Result
lowesthighest
4700091000

David holds the floor (47k), Cynthia the ceiling (91k) — spread of 44k. Crown and flag the bars in the visualization.

--variants

MIN & MAX together

The full spread in one query.

query.sql
1SELECT MIN(salary) AS lowest, MAX(salary) AS highest
2FROM employees;
Result
lowesthighest
4700091000
1David holds the floor at 47000.
2Cynthia holds the ceiling at 91000.
3Spread: 44000 — the range your salary bands must cover.

Scoped to a department

Extremes within a slice.

query.sql
1SELECT MIN(salary) AS floor_eng, MAX(salary) AS ceil_eng
2FROM employees
3WHERE department = 'Engineering';
Result
floor_engceil_eng
7300091000
1Engineering only: 85000, 91000, 73000.
2Floor 73000 (Farid), ceiling 91000 (Cynthia).
3The WHERE narrowed the population before the extremes.

Who has the max?

Pair the extreme with its row via ORDER BY + LIMIT.

query.sql
1SELECT name, salary
2FROM employees
3ORDER BY salary DESC
4LIMIT 1;
Result
namesalary
Cynthia91000
1Aggregate MAX cannot tell you the person — rows collapsed.
2Sort descending instead, keep one row.
3The LIMIT 1 window-function alternative: RANK() OVER (ORDER BY salary DESC).

Text extremes

MIN/MAX compare alphabetically on strings.

query.sql
1SELECT MIN(name) AS first_name, MAX(name) AS last_name
2FROM employees;
Result
first_namelast_name
AliceFarid
1Alphabetically: Alice first, Farid last among {Alice, Bob, Cynthia, David, Elena, Farid}.
2Comparison follows the column's collation — case rules are engine-specific.
3Dates behave the same way: MIN(hire_date) = first hire.
--key facts
Beyond numbersdates → earliest/latest · text → alphabetical extremes
Who has it?aggregates hide the row — pair with LIMIT 1 to surface it
Fast pathssorted indexes let engines answer MIN/MAX instantly
--use cases
Price ranges on filters: "€47000 – €91000" under the slider
First/last event dates per user or per product
Record holders: fastest lap, highest score — with the owner via LIMIT 1
Capacity planning: MIN/MAX memory or disk usage over time
--under the hood
On a sorted index, MIN and MAX are O(1): walk to the first (or last) entry — no scan at all.
MIN/MAX on expressions defeat that shortcut; bare indexed columns get the fast path.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.