Sample Data
students
| id | name | age | grade |
|---|---|---|---|
| 1 | Alice | 20 | A |
| 2 | Bob | 22 | B |
| 3 | Charlie | 21 | A |
| 4 | David | 23 | B |
4 rows
We'll use this students table for all examples:
What is MIN?
students
| id | name | age | grade |
|---|---|---|---|
| 1 | Alice | 20 | A |
| 2 | Bob | 22 | B |
| 3 | Charlie | 21 | A |
| 4 | David | 23 | B |
4 rows
MIN finds the smallest value. Like finding the youngest person in a room.
SELECT MIN(age) FROM students;Result: 20 (youngest student)
What is MAX?
students
| id | name | age | grade |
|---|---|---|---|
| 1 | Alice | 20 | A |
| 2 | Bob | 22 | B |
| 3 | Charlie | 21 | A |
| 4 | David | 23 | B |
4 rows
MAX finds the largest value. Like finding the oldest person in a room.
SELECT MAX(age) FROM students;Result: 23 (oldest student)
How They Work
students
| id | name | age | grade |
|---|---|---|---|
| 1 | Alice | 20 | A |
| 2 | Bob | 22 | B |
| 3 | Charlie | 21 | A |
| 4 | David | 23 | B |
4 rows
Basic Syntax
students
| id | name | age | grade |
|---|---|---|---|
| 1 | Alice | 20 | A |
| 2 | Bob | 22 | B |
| 3 | Charlie | 21 | A |
| 4 | David | 23 | B |
4 rows
SELECT MIN(column_name) FROM table_name;
SELECT MAX(column_name) FROM table_name;Example 1: Using Aliases
Result
| youngest | oldest |
|---|---|
| 20 | 23 |
1 row
Give your results nice names:
SELECT MIN(age) AS youngest, MAX(age) AS oldest FROM students;Example 2: MIN and MAX with WHERE
Result
| youngest_a |
|---|
| 21 |
1 row
Find values for specific rows:
-- Youngest grade A student
SELECT MIN(age) AS youngest_a FROM students WHERE grade = 'A';
-- Oldest grade B student
SELECT MAX(age) AS oldest_b FROM students WHERE grade = 'B';Example 3: Combine All Aggregate Functions
Result
| total | youngest | oldest | average |
|---|---|---|---|
| 4 | 20 | 23 | 21.5 |
1 row
Use COUNT, SUM, AVG, MIN, MAX together:
SELECT
COUNT(*) AS total,
MIN(age) AS youngest,
MAX(age) AS oldest,
AVG(age) AS average
FROM students;Try It Below
students
| id | name | age | grade |
|---|---|---|---|
| 1 | Alice | 20 | A |
| 2 | Bob | 22 | B |
| 3 | Charlie | 21 | A |
| 4 | David | 23 | B |
4 rows
Use the playground to practice:
SELECT MIN(age) FROM students;SELECT MAX(age) FROM students;SELECT MIN(age) AS youngest, MAX(age) AS oldest FROM students;
What Comes Next
students
| id | name | age | grade |
|---|---|---|---|
| 1 | Alice | 20 | A |
| 2 | Bob | 22 | B |
| 3 | Charlie | 21 | A |
| 4 | David | 23 | B |
4 rows
Next: Learn GROUP BY to calculate aggregates for different groups.
Try MIN & MAX
Find smallest and largest values. Try: SELECT MAX(age) FROM students;
Source Table: students
| id | name | age | grade |
|---|---|---|---|
| 1 | Alice | 20 | A |
| 2 | Bob | 22 | B |
| 3 | Charlie | 21 | A |
| 4 | David | 23 | B |
4 rows
SQL Editor
Loading...
MIN & MAX with WHERE
Find youngest grade A student. Try changing the grade!
Source Table: students
| id | name | age | grade |
|---|---|---|---|
| 1 | Alice | 20 | A |
| 2 | Bob | 22 | B |
| 3 | Charlie | 21 | A |
| 4 | David | 23 | B |
4 rows
SQL Editor
Loading...
All Aggregate Functions
Use COUNT, MIN, MAX, AVG together!
Source Table: students
| id | name | age | grade |
|---|---|---|---|
| 1 | Alice | 20 | A |
| 2 | Bob | 22 | B |
| 3 | Charlie | 21 | A |
| 4 | David | 23 | B |
4 rows
SQL Editor
Loading...