SQL MIN & MAX Functions 🏆¶
Mentor's Note:
MINandMAXare like looking for the shortest and tallest people in a room. They help you find the boundaries of your data. 💡
🌟 The Scenario: The Sports Day 🏃♂️¶
Imagine you are recording race times and scores. - MIN: "Who finished the race in the Least amount of time?" (The Winner). ⏱️ - MAX: "Who jumped the Highest in the high jump?" 🥇
💻 1. The MIN() Function¶
Returns the smallest value in a column.
-- Scenario: Find the cheapest product in the catalog
SELECT MIN(price) FROM products;
-- Scenario: Find the oldest join date (The first employee)
SELECT MIN(join_date) FROM employees;
💻 2. The MAX() Function¶
Returns the largest value in a column.
-- Scenario: Find the most expensive laptop
SELECT MAX(price) FROM products WHERE category = 'Laptop';
-- Scenario: Find the latest message received
SELECT MAX(sent_at) FROM chat_history;
💻 3. Using with Strings (A-Z) 🔤¶
Did you know MIN and MAX work on text too?
- MIN: Returns the value closest to 'A'.
- MAX: Returns the value closest to 'Z'.
🎨 Visual Logic: The Extremes¶
Dataset: [10, 50, 5, 100, 25]
MIN: [5] <------------ (The Floor)
MAX: [100] <---------- (The Ceiling)