Skip to content

SQL MIN & MAX Functions πŸ†ΒΆ

Prerequisites: Aggregate Functions Overview

Mentor's Note: MIN and MAX are 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'.

-- Scenario: Find the alphabetically first student
SELECT MIN(name) FROM students;

🎨 Visual Logic: The Extremes¢

Dataset: [10, 50, 5, 100, 25]
MIN: [5] <------------ (The Floor)
MAX: [100] <---------- (The Ceiling)

πŸ“ˆ Learning PathΒΆ