#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
Step 6
3 min read

MIN & MAX

Learn to find smallest and largest values in SQL.

Sample Data

students
idnameagegrade
1Alice20A
2Bob22B
3Charlie21A
4David23B
4 rows

We'll use this students table for all examples:

What is MIN?

students
idnameagegrade
1Alice20A
2Bob22B
3Charlie21A
4David23B
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
idnameagegrade
1Alice20A
2Bob22B
3Charlie21A
4David23B
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
idnameagegrade
1Alice20A
2Bob22B
3Charlie21A
4David23B
4 rows

Basic Syntax

students
idnameagegrade
1Alice20A
2Bob22B
3Charlie21A
4David23B
4 rows
SELECT MIN(column_name) FROM table_name; SELECT MAX(column_name) FROM table_name;

Example 1: Using Aliases

Result
youngestoldest
2023
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
totalyoungestoldestaverage
4202321.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
idnameagegrade
1Alice20A
2Bob22B
3Charlie21A
4David23B
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
idnameagegrade
1Alice20A
2Bob22B
3Charlie21A
4David23B
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
idnameagegrade
1Alice20A
2Bob22B
3Charlie21A
4David23B
4 rows
SQL Editor
Loading...

MIN & MAX with WHERE

Find youngest grade A student. Try changing the grade!

Source Table: students
idnameagegrade
1Alice20A
2Bob22B
3Charlie21A
4David23B
4 rows
SQL Editor
Loading...

All Aggregate Functions

Use COUNT, MIN, MAX, AVG together!

Source Table: students
idnameagegrade
1Alice20A
2Bob22B
3Charlie21A
4David23B
4 rows
SQL Editor
Loading...

Finished this topic?

Mark it complete to track your progress and maintain your streak!