SQL MIN and MAX Functions

The SQL MIN and MAX functions are used to find the minimum and maximum values in a column of a table, respectively. These functions are frequently used in SQL queries to retrieve the smallest or largest value from a set of records.

SQL MIN Function

The MIN function is used to find the smallest value in a column. The syntax of the MIN function is as follows:

SELECT MIN(column_name)
FROM table_name;

Where “column_name” is the name of the column for which you want to find the minimum value, and “table_name” is the name of the table that contains the column.

For example, consider the following “employees” table:

+----+----------+--------+
| ID | Name     | Salary |
+----+----------+--------+
|  1 | John Doe |  50000 |
|  2 | Jane Doe |  55000 |
|  3 | Bob Smith|  45000 |
+----+----------+--------+

To find the minimum salary in the “employees” table, you would run the following query:

SELECT MIN(Salary)
FROM employees;

This would return the following result:

+-----------+
| MIN(Salary)|
+-----------+
|      45000|
+-----------+

SQL MAX Function

The MAX function is used to find the largest value in a column. The syntax of the MAX function is as follows:

SELECT MAX(column_name)
FROM table_name;

Where “column_name” is the name of the column for which you want to find the maximum value, and “table_name” is the name of the table that contains the column.

For example, consider the following “employees” table:

+----+----------+--------+
| ID | Name     | Salary |
+----+----------+--------+
|  1 | John Doe |  50000 |
|  2 | Jane Doe |  55000 |
|  3 | Bob Smith|  45000 |
+----+----------+--------+

To find the maximum salary in the “employees” table, you would run the following query:

SELECT MAX(Salary)
FROM employees;

This would return the following result:

+-----------+
| MAX(Salary)|
+-----------+
|      55000|
+-----------+

Combining MIN and MAX Functions in a Query

You can combine the MIN and MAX functions in a single query to find both the minimum and maximum values in a column. The syntax for combining the MIN and MAX functions in a query is as follows:

SELECT MIN(column_name1), MAX(column_name2)
FROM table_name;

Where “column_name1” and “column_name2” are the names of the columns for which you want to find the minimum and maximum values, respectively, and “table_name” is the name of the table that contains the columns.

For example, consider the following “employees” table:

+----+----------+--------+
| ID | Name     | Salary |
+----+----------+--------+
|  1 | John Doe |  50000 |
|  2 | Jane Doe |  55000 |
|  3 | Bob Smith|  45000 |
+----+----------+--------+

To find the minimum and maximum salaries in the “employees” table, you would run the following query:

SELECT MIN(Salary), MAX(Salary)
FROM employees;

This would return the following result:

+-----------+-----------+
| MIN(Salary)| MAX(Salary)|
+-----------+-----------+
|      45000|      55000|
+-----------+-----------+

In this example, the MIN function returns the minimum salary of 45000 and the MAX function returns the maximum salary of 55000.

Conclusion

The SQL MIN and MAX functions are useful for finding the smallest and largest values in a column of a table, respectively. These functions can be used in a single query to find both the minimum and maximum values in a column.

By using the MIN and MAX functions, you can easily retrieve information about the range of values in a column, which can be useful for various purposes, such as analyzing data or generating reports.

Leave a Reply

Your email address will not be published. Required fields are marked *