SQL MIN and MAX
MIN(column) returns the smallest value of a column and MAX(column) the largest. They work on numbers, text (alphabetical order) and ISO dates.
SELECT MIN(column) FROM table_name WHERE condition;
SELECT MAX(column) FROM table_name WHERE condition;Smallest and largest
SELECT MIN(price) AS lowest_price,
MAX(price) AS highest_price,
MIN(name) AS first_alphabetically,
MAX(name) AS last_alphabetically
FROM products;lowest_price | highest_price | first_alphabetically | last_alphabetically
-------------+---------------+----------------------+--------------------
1.45 | 149 | Apples | Wireless Mouse
(1 row)
For ISO dates, MIN is the earliest and MAX the latest date:
SELECT MIN(hire_date) AS first_hire, MAX(hire_date) AS latest_hire
FROM employees;
SELECT MIN(order_date) AS first_order, MAX(order_date) AS last_order
FROM orders;first_hire | latest_hire
-----------+------------
2019-03-01 | 2024-05-13
(1 row)
first_order | last_order
------------+-----------
2025-01-02 | 2025-10-20
(1 row)
MIN/MAX per group
Combine with GROUP BY to get the range in each group — the cheapest and most expensive product of every category:
SELECT category_id, MIN(price) AS min_price, MAX(price) AS max_price
FROM products
GROUP BY category_id
ORDER BY category_id;category_id | min_price | max_price
------------+-----------+----------
1 | 3.2 | 24.5
2 | 4.9 | 22
3 | 1.45 | 7.8
4 | 1.89 | 4.5
5 | 2.49 | 5.99
6 | 9.99 | 149
7 | 38 | 59.9
8 | 29 | 39
(8 rows)
Which row has the maximum?
MAX(price) tells you the price, not which product has it. A common beginner mistake is SELECT name, MAX(price) FROM products — standard SQL rejects it. Two portable ways to get the whole row:
-- 1. Compare with a subquery
SELECT name, price
FROM products
WHERE price = (SELECT MAX(price) FROM products);
-- 2. Sort and take the first row
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 1;name | price
----------------------------+------
Noise-Cancelling Headphones | 149
(1 row)
name | price
----------------------------+------
Noise-Cancelling Headphones | 149
(1 row)
The subquery version returns all products that share the top price; the LIMIT 1 version returns just one.
Note: SQLite has a special rule: in
SELECT name, MAX(price) FROM products, the other columns come from the row that holds the maximum. It is convenient, but no other major database guarantees it.
MIN and MAX of text
Text compares character by character, so 'Zara Ali' > 'Amara Costa'. In SQLite upper-case letters sort before lower-case ones, so mixed-case data may surprise you.
Scalar MIN/MAX with several arguments
In SQLite, MIN(a, b, …) and MAX(a, b, …) with two or more arguments compare those values within one row (other databases call these LEAST and GREATEST):
SELECT name, stock, MIN(stock, 20) AS shelf_display
FROM products
WHERE category_id = 1
ORDER BY product_id;name | stock | shelf_display
----------------+-------+--------------
Espresso Beans | 40 | 20
Green Tea | 120 | 20
Orange Juice | 75 | 20
Sparkling Water | 0 | 0
Chai Latte Mix | 32 | 20
(5 rows)
Test yourself
What does MAX(order_date) return for ISO date strings?
- The earliest date
- The latest date
- An error
- The number of dates
Which query reliably returns the name of every product with the highest price?
- SELECT name, MAX(price) FROM products
- SELECT name FROM products WHERE price = (SELECT MAX(price) FROM products)
- SELECT MAX(name) FROM products
- SELECT name FROM products WHERE price = MAX(price)