When working with databases, there are times when you need to find the largest or smallest value within a column. Whether you're analyzing sales figures, employee salaries, or product prices, the SQL MAX()
and MIN()
functions provide a quick and efficient way to retrieve these values.
These functions are part of SQL’s aggregate functions and are typically used with the SELECT
statement to return the maximum and minimum values in a dataset. In this blog post, we’ll explore how the MAX()
and MIN()
functions work, with examples to demonstrate their practical usage.
The MAX()
function in SQL is used to return the largest value from a specified column. It is commonly used for numeric, date, or string data types, depending on what you are analyzing.
SELECT MAX(column_name)
FROM table_name;
column_name
: The column from which you want to retrieve the maximum value.Suppose you have a sales
table that contains the amount
column representing the amount of each sale, and you want to find the largest sale made.
SELECT MAX(amount) AS largest_sale
FROM sales;
Result:
largest_sale |
---|
5000 |
This query returns the largest sale amount from the sales
table.
MAX()
works with numeric, date, and string data types.NULL
values, they are ignored in the calculation.The MIN()
function in SQL is used to return the smallest value from a specified column. Like the MAX()
function, MIN()
can be used with numeric, date, or string data types.
SELECT MIN(column_name)
FROM table_name;
column_name
: The column from which you want to retrieve the minimum value.Suppose you want to find the smallest sale amount in the sales
table.
SELECT MIN(amount) AS smallest_sale
FROM sales;
Result:
smallest_sale |
---|
100 |
This query returns the smallest sale amount from the sales
table.
MIN()
also works with numeric, date, and string data types.MAX()
, MIN()
ignores NULL
values when calculating the result.While MAX()
and MIN()
are often used to find the highest or lowest value in a column, you can also use them with other columns in combination with GROUP BY
to find the maximum or minimum value for each group.
Suppose you have an employees
table with columns department
and salary
, and you want to find the highest and lowest salary in each department.
SELECT department, MAX(salary) AS highest_salary, MIN(salary) AS lowest_salary
FROM employees
GROUP BY department;
Result:
department | highest_salary | lowest_salary |
---|---|---|
Sales | 80000 | 45000 |
Marketing | 75000 | 50000 |
Engineering | 95000 | 60000 |
This query groups employees by their department and returns the highest and lowest salary for each department.
The MAX()
and MIN()
functions are also useful for working with date columns. You can use them to find the most recent or oldest record in a table.
Suppose you have an orders
table with an order_date
column, and you want to find the most recent and the oldest order dates.
SELECT MAX(order_date) AS latest_order, MIN(order_date) AS earliest_order
FROM orders;
Result:
latest_order | earliest_order |
---|---|
2024-12-20 | 2023-01-01 |
This query returns the latest and earliest order dates in the orders
table.
You can combine the MAX()
and MIN()
functions with other aggregate functions for more complex queries. For example, you might want to find the maximum and minimum order values along with the total sum of sales.
Suppose you want to find the total sales, highest sale, and lowest sale in the sales
table.
SELECT SUM(amount) AS total_sales,
MAX(amount) AS largest_sale,
MIN(amount) AS smallest_sale
FROM sales;
Result:
total_sales | largest_sale | smallest_sale |
---|---|---|
150000 | 5000 | 100 |
This query returns the total sales, the largest sale, and the smallest sale in the sales
table.
As mentioned earlier, the MAX()
and MIN()
functions ignore NULL
values. This means that if a column contains NULL
values, they will not affect the result.
Suppose the amount
column in the sales
table contains some NULL
values. If you use MAX()
or MIN()
, the NULL
values are automatically excluded.
SELECT MAX(amount) AS largest_sale
FROM sales;
If the amount
column has NULL
values, they will not be considered in the calculation of the largest value.
You can also use the MAX()
and MIN()
functions in subqueries to filter records based on these values.
Suppose you want to find all sales that are equal to the highest sale in the sales
table.
SELECT sale_id, amount
FROM sales
WHERE amount = (SELECT MAX(amount) FROM sales);
Result:
sale_id | amount |
---|---|
1001 | 5000 |
This query uses a subquery to find the largest sale and retrieves the sale_id
and amount
for that sale.