Let’s dive into the world of extremes in data — finding minimum and maximum values. Don’t worry, SQL won’t make you learn anything radically new, but you’ll see how to grab the biggest and smallest values in your tables. These functions are everywhere: from analyzing movie ratings on IMDb to figuring out the lowest temperature on Mars.
MIN() and MAX() are aggregate functions that return the minimum and maximum values from a dataset. They work like friendly hunters for extremes: MIN() looks for the smallest item, and MAX() — the biggest one. You’re not limited to just numbers: you can hunt for the minimum and maximum strings or dates too.
-- General form for MIN and MAX functions
SELECT
MIN(column_name) AS min_value,
MAX(column_name) AS max_value
FROM table_name;
Easy and simple! Now let’s check out how this magic works in different scenarios.
Syntax and Basic Examples
Numeric Data
Let’s say we have a table called employees with a column for employee salaries (salary).
We want to find out the minimum and maximum salaries.
Table employees
| id | name | salary |
|---|---|---|
| 1 | Anna Song | 45000 |
| 2 | Otto Art | 30000 |
| 3 | Maria Chi | 120000 |
| 4 | Alex Lin | 90000 |
| 5 | Eva Tan | 60000 |
-- Example for numeric data
SELECT
MIN(salary) AS min_salary,
MAX(salary) AS max_salary
FROM employees;
Result
| min_salary | max_salary |
|---|---|
| 30000 | 120000 |
Text Data
You can use MIN() and MAX() with text data too.
They compare values alphabetically (yep, even letters can be the "smallest" or "biggest").
Suppose we have a table students where the name column stores student names.
Table students
| id | name |
|---|---|
| 1 | Anna |
| 2 | Otto |
| 3 | Maria |
| 4 | Ben |
| 5 | Zoe |
-- Example for text data
SELECT
MIN(name) AS first_name_in_order,
MAX(name) AS last_name_in_order
FROM students;
Result
| first_name_in_order | last_name_in_order |
|---|---|
| Anna | Zoe |
Dates and Time
Let’s find the earliest and latest date from the events table.
Table events
| id | event_name | event_date |
|---|---|---|
| 1 | New Year Party | 2023-01-01 |
| 2 | Summer Fest | 2023-06-15 |
| 3 | Halloween Bash | 2023-10-31 |
| 4 | Year End Gala | 2023-12-31 |
-- Example for working with dates
SELECT
MIN(event_date) AS earliest_date,
MAX(event_date) AS latest_date
FROM events;
Result
| earliest_date | latest_date |
|---|---|
| 2023-01-01 | 2023-12-31 |
Using MIN() and MAX() in Tables with NULL Values
If a column has NULL values, you can chill: SQL just ignores them when calculating.
This means MIN() and MAX() will only work with the "normal" values.
Table employees_with_null
| id | name | salary |
|---|---|---|
| 1 | Otto | 30000 |
| 2 | Maria | NULL |
| 3 | Anna | 120000 |
| 4 | John | NULL |
-- Table with NULL
SELECT
MIN(salary) AS min_salary,
MAX(salary) AS max_salary
FROM employees_with_null;
If there are NULL records in the salary column, they’re just ignored.
| min_salary | max_salary |
|---|---|
| 30000 | 120000 |
But if the WHOLE column is NULL, the result will be NULL.
Examples of Using MIN() and MAX() in Real Tasks
Finding the Oldest and Youngest Employee
Table employees
| id | name | birth_date |
|---|---|---|
| 1 | Otto | 1980-05-10 |
| 2 | Maria | 1990-11-20 |
| 3 | Anna | 1975-03-15 |
| 4 | John | 2000-08-01 |
The employees table has a birth_date field that stores employee birth dates. Let’s find the oldest and youngest.
SELECT
MIN(birth_date) AS oldest_employee,
MAX(birth_date) AS youngest_employee
FROM employees;
Result:
| oldest_employee | youngest_employee |
|---|---|
| 1975-03-15 | 2000-08-01 |
Sales Analysis
| id | amount |
|---|---|
| 1 | 150.00 |
| 2 | 75.50 |
| 3 | 200.00 |
| 4 | 120.00 |
| 5 | 500.00 |
| 6 | 90.00 |
The sales table has an amount field that stores sales amounts. Let’s find the smallest and largest sale.
SELECT
MIN(amount) AS smallest_sale,
MAX(amount) AS largest_sale
FROM sales;
Query result
| smallest_sale | largest_sale |
|---|---|
| 75.50 | 500.00 |
Finding the First and Last Orders
If you have a table orders with a order_date field, you can easily find the first and last order dates.
Original table orders
| order_id | customer_id | order_date | amount |
|---|---|---|---|
| 101 | 1 | 2023-01-15 10:23:00 | 250.00 |
| 102 | 2 | 2023-02-10 14:45:00 | 150.00 |
| 103 | 3 | 2023-01-05 09:10:00 | 300.00 |
| 104 | 1 | 2023-03-22 16:30:00 | 500.00 |
| 105 | 4 | 2023-01-25 11:00:00 | 120.00 |
SELECT
MIN(order_date) AS first_order,
MAX(order_date) AS last_order
FROM orders;
Query result:
| first_order | last_order |
|---|---|
| 2023-01-05 09:10:00 | 2023-03-22 16:30:00 |
Special Features of MIN() and MAX()
Comparing Text: when working with strings, MIN() and MAX() compare them alphabetically.
Working with NULL: NULL is ignored if there are other values in the column. But if all values are NULL, the result will be NULL.
Comparing Dates: SQL takes into account not just the year, but also the month, day, hours, and minutes.
GO TO FULL VERSION