A couple of useful and simple functions in SQL are the MAX() and MIN() functions. These functions act exactly as they seem intuitively.
This lesson provides definitions and examples of SQL MAX() and SQL MIN().
Note: This lesson uses the payments table from the MySQL Sakila demo database. If you don't yet have this database installed, follow these steps to install the Sakila DB, then head back here.
SQL Max Function
SQL MAX() returns the maximum value of a numeric column.
For example, using the payment table from the Sakila Database, you can query for the largest payment you've received:
SELECT MAX(amount) AS largest_payment
FROM payment;
SQL Min Function
SQL MIN() returns the minimum value of a numeric column.
For example, suppose you need to know the lowest payment in the payment table. Since the amount column contains the payment value, you want the amount column's "MIN" (for "minimum").
SELECT MIN(amount) AS smallest_payment
FROM payment;
Summary: SQL Max & SQL Min
The SQL MAX and MIN functions, like many other SQL functions, are self-explanatory.
- The SQL
MAX()function stands for maximum and takes the maximum value of a column - The
MIN()function stands for minimum and takes the minimum value of a column - Both
MINandMAXwork only on numeric and date/time (DATETIME) columns
SQL Max & SQL Min Syntax
Here's the syntax for the commands, where you can substitute the variables in < > with your values.
--MIN function
SELECT MIN(<YOUR_COLUMN>)
FROM <YOUR_TABLE>
--MAX function
SELECT MAX(<YOUR_COLUMN>)
FROM <YOUR_TABLE>
Functions are useful, aren't they? Next, review your new SQL functions with a video tutorial.