How to Use MAX and MIN in SQL
Use MAX() and MIN() when you need the highest or lowest value in a column. These functions are perfect for price ranges, earliest dates, latest activity, top scores, and threshold checks.
What you’ll build or solve
You’ll learn how to use MAX and MIN in SQL for numbers, dates, and grouped extremes. You’ll also know how NULL values affect the result.
Learn SQL on Mimo
When this approach works best
This approach is the right choice when only the highest or lowest matching value matters.
Common real-world scenarios include:
- Highest price
- Lowest inventory
- Latest login
- Earliest order
- Best exam score
This is a bad idea when you need the full top row details. In that case, ORDER BY ... LIMIT 1 may be better.
Prerequisites
You only need:
- A SQL table with sortable numeric or date columns
- Basic
SELECT,WHERE, andGROUP BYknowledge
Step-by-step instructions
Step 1: Wrap the column in MAX() or MIN()
Use MAX() for the largest value.
SQL
SELECT MAX(price)
FROM products;
Use MIN() for the smallest value.
SQL
SELECT MIN(price)
FROM products;
This works especially well with dates.
SQL
SELECT MAX(last_login)
FROM users;
For grouped extremes, combine with GROUP BY.
SQL
SELECT country, MAX(total)
FROM orders
GROUP BY country;
What to look for:
MAX()returns the highest valueMIN()returns the lowest value- Great for numbers and dates
GROUP BYcreates per-group extremesNULLvalues are ignored
Examples you can copy
Highest order
SQL
SELECT MAX(total)
FROM orders;
Earliest signup
SQL
SELECT MIN(created_at)
FROM users;
Highest score by class
SQL
SELECT class_id, MAX(score)
FROM exams
GROUP BY class_id;
Common mistakes and how to fix them
Mistake 1: Expecting the full row
What the reader might do:
SQL
SELECT MAX(total)
FROM orders;
Why it breaks: this returns only the highest value, not the entire order row.
Corrected approach:
Use ORDER BY total DESC LIMIT 1 when the full row is needed.
Mistake 2: Forgetting GROUP BY
What the reader might do:
SQL
SELECT country, MAX(total)
FROM orders;
Why it breaks: SQL needs grouping for non-aggregated columns.
Corrected approach:
SQL
GROUP BY country
Mistake 3: Misreading ignored NULL values
What the reader might do:
SQL
SELECT MIN(discount)
FROM orders;
Why it breaks: NULL discounts are skipped.
Corrected approach:
Use COALESCE() if missing values should count as zero.
Troubleshooting
If the result is only one number, that is expected aggregate behavior.
If grouped extremes error, add GROUP BY.
If the result is NULL, confirm matching rows exist.
If the full row is needed, switch to ORDER BY.
Quick recap
- Use
MAX()for the highest value - Use
MIN()for the lowest value - Great for dates and numbers
- Add
GROUP BYfor grouped extremes - Use
ORDER BYfor full top rows
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot