MIN and MAX function in SQL

The MIN and MAX functions in SQL are used to find the minimum and maximum values in a specified column, respectively.

Example: Let's use a table called employees with the following data:

employee_idfirst_namelast_nameagesalary
1JohnDoe3050000
2JaneSmith2560000
3MichaelO'Connor3555000
4SarahO'Brien2845000
5LauraWilson3270000

1) MIN Function

The MIN function returns the minimum value, ignoring null values.

SQL Syntax

Select MIN(column_name) from table_name;

Find the Minimum Age To find the youngest employee (minimum age):

SQL

SELECT MIN(age) AS min_age
FROM employees;

The result of the above query returns the minimum value in the age column.

min_age
25

2) MAX Function

The MAX function returns maximum value from selected column of the table.

SQL Syntax

SELECT MAX(column_name) from table-name;

Find the Maximum Age To find the oldest employee (maximum age):

SQL

SELECT MAX(age) AS max_age
FROM employees;

The above query returns the maximum age from the column age.

max_age
35

Note: Similarly, we can find the minimum and maximum value from the salary column.