SQL MIN & MAX Functions πΒΆ
Prerequisites: Aggregate Functions Overview
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)