Imagine you're curious about the best and worst-selling products on Amazon. SQL's MIN and MAX functions are gonna help you find extremes in your data, whether it's sales figures, dates, or even product names.
They work with numbers, dates, and even text.
Say we have an Amazon-like product_sales table with columns product_id, product_name, sales, and release_date.
| product_id | product_name | sales | release_date |
|---|---|---|---|
| 1 | Echo Dot | 500 | 2021-01-01 |
| 2 | Kindle Reader | 300 | 2021-02-01 |
| 3 | Fire Stick | 700 | 2021-03-01 |
| ... | ... | ... | ... |
To find the best and worst-selling products, you'd use:
SELECT
MIN(sales) AS min_sales,
MAX(sales) AS max_sales
FROM product_sales;
This tells you the lowest and highest sales numbers. But what if you want to know the earliest and latest product release dates? You'd use:
SELECT
MIN(release_date) AS earliest_release,
MAX(release_date) AS latest_release
FROM product_sales;
MIN and MAX are super handy for these kinds of insights - from figuring out your top and bottom performers to tracking product trends over time. You don't need to order all your data in ascending order and then find that value. Imagine if you had to deal with millions of rows (your Excel won't be able to manage it). This is why MIN and MAX are so powerful as they simplify digging into your data to find those key high and low points.
Now let's practice!