SQL MIN and MAX

The MAX and MIN functions in SQL are used to retrieve the highest and lowest values in a column, respectively. These functions are often used to find the largest or smallest value in a set of data, such as the maximum salary in a table of employees or the minimum date in a table of sales.

Syntax of min and max

1SELECT MAX(column_name) 2FROM table_name; 3 4SELECT MIN(column_name) 5FROM table_name;

For example, consider a table named employees with the following columns: id, first_name, last_name, salary. If you want to find the highest salary in the table, you could use the following query:

1SELECT MAX(salary) 2FROM employees;

This query would return the highest salary in the salary column of the employees table.

Similarly, if you wanted to find the lowest salary in the table, you could use the following query:

1SELECT MIN(salary) 2FROM employees;