MIN & MAX
Find the smallest and largest values in a column
Finding Extremes
MIN returns the smallest value in a column; MAX returns the largest. Both ignore NULLs and work across numbers, text, and dates. Here's what to know:
MAX(total) - MIN(total) AS price_range computes the full spread of values in a single expression without needing two separate queries or subqueries to find each extreme.MAX(name) returns the last name alphabetically, MIN(order_date) returns the very first order.How MIN/MAX Work
Find boundary values
- MIN returns the smallest non-NULL value
- MAX returns the largest non-NULL value
- Works on numbers, dates, and strings (alphabetical)
- Both skip NULLs — all NULLs → returns NULL
MIN/MAX Patterns
Beyond simple boundaries
- Date ranges:
MIN(order_date), MAX(order_date) - Price range:
MIN(price) || ' – ' || MAX(price) - Latest record:
WHERE date = (SELECT MAX(date) FROM ...) - With GROUP BY: per-group extremes
NULL & Type Behavior
Edge cases to know
- NULLs are ignored by both MIN and MAX
- All NULLs → NULL result
- String MIN/MAX follows collation order
- MIN('apple','Banana') depends on case sensitivity
MIN/MAX vs Other Approaches
Choosing the right tool
MIN(column)Smallest non-NULL valueMAX(column)Largest non-NULL value| name | price |
|---|---|
| Wireless Mouse | 24.99 |
| Office Desk | 189 |
| LED Desk Lamp | 45.5 |
| USB-C Cable | 12.99 |
| Bluetooth Speaker | 79 |
| Office Chair | 95 |
| Coffee Maker | 89.99 |
| Notebook Set | 8.5 |
| Wireless Headphones | 159 |
| Standing Desk | 425 |
| order_date | total |
|---|---|
| 2024-01-15 | 59.98 |
| 2024-02-08 | 449 |
| 2024-02-20 | 12.5 |
| 2024-03-05 | 89.99 |
| 2024-03-15 | 245 |
| 2024-03-22 | 159 |
| 2024-04-02 | 45.5 |
| 2024-04-12 | 79 |
| 2024-04-22 | 425 |
| 2024-05-05 | 18.99 |
| 2024-05-18 | 134 |
| 2024-06-08 | 65.5 |
Worked Example
Find the cheapest price, most expensive price, and the price range from the products table.
| cheapest | most_expensive | price_spread |
|---|---|---|
| 8.5 | 425 | 416.5 |
Key Concepts
Pro Tip
Extremes reveal data boundaries — cheapest product, latest order, highest spend. Essential for range checks and anomaly detection.
When to Use
Cheapest/most expensive product, earliest/latest order, highest/lowest ratings, date range of a dataset.
Find the earliest and latest order dates, and the smallest and largest order totals from the orders table. Use aliases: earliest_order, latest_order, min_total, max_total.