SQL MIN() and MAX()
In SQL, the MIN()
and MAX()
functions are aggregate functions that allow you to find the minimum and maximum values, respectively, within a set of values. These functions are often used in conjunction with the SELECT
statement and are useful for obtaining summary information from a database.
MIN() Function:
- The
MIN()
function is used to find the minimum value in a set of values. - Syntax:
MIN(column_name)
orMIN(expression)
The basic syntax of the MIN()
the function is as follows:
1 |
SELECT MIN(column_name) FROM table_name; |
column_name: The name of the column from which you want to find the minimum value.
table_name: The name of the table containing the specified column.
- Example: If you have a table named
scores
with a column namedscore
, you can find the minimum score using the following SQL query:
1 |
SELECT MIN(score) FROM scores; |
MAX() Function:
- The
MAX()
function is used to find the maximum value in a set of values. - Syntax:
MAX(column_name)
orMAX(expression)
The basic syntax of the MAX()
function is as follows:
1 |
SELECT MAX(column_name) FROM table_name; |
column_name: The name of the column from which you want to find the maximum value.
table_name: The name of the table containing the specified column.
- Example: Using the same
scores
table, you can find the maximum score with the following query:
1 |
SELECT MAX(score) FROM scores; |