MAX / MIN
| Since: | SQL-92(1992) |
|---|
Aggregate functions that return the maximum and minimum values from a column of numbers, strings, or dates. You can also combine them with GROUP BY to find the maximum and minimum per group.
Syntax
Returns the maximum value in a column.
SELECT MAX(column_name) FROM table_name;
Returns the minimum value in a column.
SELECT MIN(column_name) FROM table_name;
Returns the maximum and minimum values per group.
SELECT group_column, MAX(column_name), MIN(column_name) FROM table_name GROUP BY group_column;
Syntax List
| Syntax | Description |
|---|---|
| MAX(column_name) | Returns the maximum value in the specified column. Works with numbers, strings, and dates. NULL values are ignored. |
| MIN(column_name) | Returns the minimum value in the specified column. Works with numbers, strings, and dates. NULL values are ignored. |
Sample Code
The following examples use the employees table.
Returns the highest and lowest salaries.
sample_max_min.sql
SELECT MAX(salary) AS highest_salary, MIN(salary) AS lowest_salary FROM employees;
+----------------+---------------+ | highest_salary | lowest_salary | +----------------+---------------+ | 380000 | 250000 | +----------------+---------------+ 1 row in set
Returns the highest and lowest salary per department.
sample_max_min.sql
SELECT department, MAX(salary) AS highest_salary, MIN(salary) AS lowest_salary FROM employees GROUP BY department;
+--------------+----------------+---------------+ | department | highest_salary | lowest_salary | +--------------+----------------+---------------+ | Jujutsu High | 380000 | 250000 | +--------------+----------------+---------------+ 1 row in set
Returns the overall average salary.
sample_max_min.sql
SELECT AVG(salary) AS average_salary FROM employees;
+----------------+ | average_salary | +----------------+ | 307500.000 | +----------------+ 1 row in set
Returns the difference between the highest and lowest salary.
sample_max_min.sql
SELECT MAX(salary) - MIN(salary) AS salary_range FROM employees;
+--------------+ | salary_range | +--------------+ | 130000 | +--------------+ 1 row in set
Returns the alphabetically last name (string maximum).
sample_max_min.sql
SELECT MAX(name) AS max_name FROM employees;
+-----------------+ | max_name | +-----------------+ | Kugisaki Nobara | +-----------------+ 1 row in set
Returns the name of the employee with the highest salary (using a subquery).
sample_max_min.sql
SELECT name, salary FROM employees WHERE salary = (SELECT MAX(salary) FROM employees);
+-------------+--------+ | name | salary | +-------------+--------+ | Gojo Satoru | 380000 | +-------------+--------+ 1 row in set
Database-Specific Syntax
This syntax works the same across all major databases.
SELECT department, MAX(salary) AS highest_salary, MIN(salary) AS lowest_salary FROM employees GROUP BY department;
How you retrieve other columns from the row that holds the maximum or minimum value differs by database. MySQL and SQLite require a subquery, while PostgreSQL supports window functions as well.
-- Works in all databases (subquery)
SELECT name, salary FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);
-- PostgreSQL (window function, also handles ties)
SELECT name, salary FROM (
SELECT name, salary, RANK() OVER (ORDER BY salary DESC) AS rnk
FROM employees
) ranked WHERE rnk = 1;
Notes
MAX and MIN work on strings (lexicographic order) and dates (chronological order), not just numbers. For strings, the result is determined by character code order (which closely follows alphabetical order for UTF-8 encoded text).
MAX and MIN ignore NULL values. If all values in the column are NULL, the result is also NULL.
To retrieve other columns from the row that holds the maximum or minimum value, the common approach is to find the max or min with a subquery and then filter with WHERE. If multiple rows share the same value, all of them are returned. For totals and averages, see SUM / AVG.
If you find any errors or copyright issues, please contact us.