In SQL, determining the minimum and maximum values within a dataset is a fundamental requirement in many applications. Whether you are calculating the smallest or largest values in a column, such as the lowest salary in a company or the highest price of products, SQL provides two simple yet powerful aggregate functions: MIN() and MAX().
In this chapter, we will explore these functions, their syntax, use cases, and advanced scenarios. We will also dive into how MIN()
and MAX()
can be used alongside other SQL features such as GROUP BY
, HAVING
, and subqueries.
The MIN()
and MAX()
functions return the smallest and largest values, respectively, from a column of data. Both functions are aggregate functions, which means they operate on a set of values to return a single result.
MIN()
: Returns the smallest value.MAX()
: Returns the largest value.The basic syntax is:
SELECT MIN(column_name), MAX(column_name)
FROM table_name
WHERE condition;
Consider an employees
table:
ID | Name | Department | Salary |
---|---|---|---|
1 | John Doe | HR | 5000 |
2 | Jane Smith | IT | 6000 |
3 | David Green | Sales | 7000 |
4 | Sarah Brown | IT | 8000 |
5 | James White | Sales | 5500 |
To find the minimum and maximum salary in the employees
table:
SELECT MIN(salary) AS MinSalary, MAX(salary) AS MaxSalary
FROM employees;
MinSalary | MaxSalary |
---|---|
5000 | 8000 |
You can combine MIN()
and MAX()
with the WHERE
clause to filter the data before calculating the minimum or maximum values.
To find the minimum and maximum salary of employees in the IT
department:
SELECT MIN(salary) AS MinITSalary, MAX(salary) AS MaxITSalary
FROM employees
WHERE department = 'IT';
;
MinITSalary | MaxITSalary |
---|---|
6000 | 8000 |
The query filters employees from the IT
department and returns the smallest and largest salary values from that filtered set.
The GROUP BY
clause is often used alongside MIN()
and MAX()
to calculate the minimum or maximum values for each group of data, such as each department or category.
If you want to find the minimum and maximum salary for each department:
SELECT department, MIN(salary) AS MinSalary, MAX(salary) AS MaxSalary
FROM employees
GROUP BY department;
Department | MinSalary | MaxSalary |
---|---|---|
HR | 5000 | 5000 |
IT | 6000 | 8000 |
Sales | 5500 | 7000 |
The query groups the employees by department and returns the minimum and maximum salary for each department.
The HAVING
clause is used to filter the result of aggregate functions, such as MIN()
and MAX()
, after they are applied. This is especially useful when you want to find groups that meet certain criteria.
To find departments where the maximum salary exceeds 6000:
SELECT department, MAX(salary) AS MaxSalary
FROM employees
GROUP BY department
HAVING MAX(salary) > 6000;
Department | MaxSalary |
---|---|
IT | 8000 |
Sales | 7000 |
The query first calculates the maximum salary for each department and then filters the results to show only those departments where the maximum salary is greater than 6000.
You can use MIN()
and MAX()
on more than one column in a single query. This is useful for analyzing multiple aspects of your data simultaneously.
To find the minimum salary and the minimum id
value from the employees
table:
SELECT MIN(salary) AS MinSalary, MIN(id) AS MinID, MAX(salary) AS MaxSalary, MAX(id) AS MaxID
FROM employees;
MinSalary | MinID | MaxSalary | MaxID |
---|---|---|---|
5000 | 1 | 8000 | 5 |
The query returns both the minimum and maximum salary, as well as the minimum and maximum id
values from the employees
table.
In more complex scenarios, you may need to use MIN()
and MAX()
within subqueries to filter records or perform calculations.
To find the name of the employee with the highest salary:
SELECT name, salary
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);
Name | Salary |
---|---|
Sarah Brown | 8000 |
The subquery retrieves the maximum salary, and the outer query fetches the employee(s) who earn this salary.
MIN()
and MAX()
can also be used with date and time columns, which is useful for finding the earliest or latest events in a dataset.
Consider a hire_date
column in the employees
table:
ID | Name | Hire Date |
---|---|---|
1 | John Doe | 2020-05-01 |
2 | Jane Smith | 2019-08-15 |
3 | David Green | 2021-01-10 |
4 | Sarah Brown | 2019-12-22 |
To find the earliest and latest hire dates:
SELECT MIN(hire_date) AS EarliestHireDate, MAX(hire_date) AS LatestHireDate
FROM employees;
EarliestHireDate | LatestHireDate |
---|---|
2019-08-15 | 2021-01-10 |
The query returns the earliest and latest hire dates from the employees
table.
You can combine MIN()
and MAX()
with other aggregate functions like SUM()
, COUNT()
, and AVG()
to perform more comprehensive data analysis.
To find the minimum, maximum, and average salary in the employees
table:
SELECT MIN(salary) AS MinSalary, MAX(salary) AS MaxSalary, AVG(salary) AS AvgSalary
FROM employees;
MinSalary | MaxSalary | AvgSalary |
---|---|---|
5000 | 8000 | 6300 |
This query returns the minimum, maximum, and average salary values in one go, providing a broader picture of the salary distribution.
When using MIN()
and MAX()
on large datasets, performance can become a concern, especially if the columns being analyzed are not indexed. Here are some tips to optimize queries involving MIN()
and MAX()
:
GROUP BY
as it adds extra overhead to the query execution.The MIN() and MAX() functions are essential tools for extracting the smallest and largest values from datasets in SQL. From simple queries to more complex scenarios involving GROUP BY, HAVING, and subqueries, these functions provide flexibility in data analysis.Understanding how to use MIN() and MAX() effectively, along with considering performance optimizations, ensures that you can retrieve the most critical information from your data efficiently. Happy coding !❤️