SQL Min and Max
In SQL, the MIN and MAX functions are used to find the minimum and maximum values, respectively, in a column or a set of values. Here’s how you can use these functions:
- MIN Function: The MIN function returns the smallest value in a column or a set of values.Example 1: Finding the minimum value in a columnsql
SELECT MIN(column_name)
FROM table_name;
Example 2: Finding the minimum value in a set of values
sql
SELECT MIN(value1, value2, value3, ...)
FROM table_name;
MAX Function: The MAX function returns the largest value in a column or a set of values.
Example 1: Finding the maximum value in a column
sql
SELECT MAX(column_name)
FROM table_name;
Example 2: Finding the maximum value in a set of values
sql
SELECT MAX(value1, value2, value3, ...) FROM table_name;
Note that the MIN and MAX functions can be used with various data types such as numeric, string, and date/time.
You can also combine the MIN or MAX function with other SQL clauses like WHERE, GROUP BY, or ORDER BY to perform more advanced queries.