MIN and MAX: Finding Extremes
You'll learn how to use SQL's MIN and MAX functions to instantly find the smallest and largest values in any column.
What Are MIN and MAX?
MIN() and MAX() are aggregate functions that scan through an entire column and return the extreme values—the smallest and largest, respectively. Think of them as automatic scouts that examine every row and report back with just the winner at each end of the spectrum.
How They Work
These functions work across different data types:
Numeric columns: Find the lowest and highest numbers
SELECT MIN(price) AS lowest_price, MAX(price) AS highest_price
FROM products;
Date columns: Find the earliest and most recent dates
SELECT MIN(order_date) AS first_order, MAX(order_date) AS last_order
FROM orders;
Text columns: Find alphabetically first and last values (based on sorting order)
SELECT MIN(product_name) AS first_alphabetically, MAX(product_name) AS last_alphabetically
FROM products;
Practical Applications
- Business intelligence: "What's our cheapest product? Our most expensive?"
- Data quality: "What's the earliest date in this dataset? Does it make sense?"
- Range analysis: "What's the age range of our customers?"
Important Notes
MIN()andMAX()ignore NULL values (just like other aggregate functions)- They return a single value per column examined
- Can be combined with WHERE clauses to find extremes within filtered subsets
- Often used with GROUP BY to find extremes per category (you'll learn this soon)
Key Takeaway: MIN() and MAX() are your go-to functions for instantly discovering the smallest and largest values in any column, whether numeric, date, or text.