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_id | first_name | last_name | age | salary |
---|---|---|---|---|
1 | John | Doe | 30 | 50000 |
2 | Jane | Smith | 25 | 60000 |
3 | Michael | O'Connor | 35 | 55000 |
4 | Sarah | O'Brien | 28 | 45000 |
5 | Laura | Wilson | 32 | 70000 |
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;
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;
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.