SQL MAX() function
MAX() function
The aggregate function SQL MAX() is used to find the maximum value or highest value of a certain column or expression. This function is useful to determine the largest of all selected values of a column.
Syntax:
MAX ([ALL | DISTINCT] expression )
DB2 and Oracle Syntax:
MAX ([ALL | DISTINCT] expression ) OVER (window_clause)
Parameters:
| Name | Description |
|---|---|
| ALL | Applies to all values. |
| DISTINCT | Consider each unique value. DISTINCT is not meaningful with MAX function. |
| expression | Expression made up of a single constant, variable, scalar function, or column name or any combination of arithmetic, bitwise, and string operators. MAX can be used with numeric, character, and datetime columns, but not with bit columns. Aggregate functions and subqueries are not permitted. |
DBMS Support: COUNT() function
| DBMS | Command |
| MySQL | Supported |
| PostgreSQL | Supported |
| SQL Server | Supported |
| Oracle | Supported |
Syntax diagram - MAX() function
Example:
To get maximum 'ord_amout' from the 'orders' table, the following SQL statement can be used :
Sample table: orders
ORD_NUM ORD_AMOUNT ADVANCE_AMOUNT ORD_DATE CUST_CODE AGENT_CODE ORD_DESCRIPTION
---------- ---------- -------------- --------- --------------- --------------- -----------------
200114 3500 2000 15-AUG-08 C00002 A008
200122 2500 400 16-SEP-08 C00003 A004
200118 500 100 20-JUL-08 C00023 A006
200119 4000 700 16-SEP-08 C00007 A010
200121 1500 600 23-SEP-08 C00008 A004
200130 2500 400 30-JUL-08 C00025 A011
200134 4200 1800 25-SEP-08 C00004 A005
200108 4000 600 15-FEB-08 C00008 A004
200103 1500 700 15-MAY-08 C00021 A005
200105 2500 500 18-JUL-08 C00025 A011
.........
200102 2000 300 25-MAY-08 C00012 A012
SELECT MAX(ord_amount) -- Selects the maximum value of the 'ord_amount' column
FROM orders; -- Specifies the 'orders' table as the source of data
Explanation:
- SELECT MAX(ord_amount): This is the main part of the SQL query. It uses the MAX() function to calculate the maximum value of the 'ord_amount' column in the 'orders' table. The result will be a single value representing the maximum value found in the 'ord_amount' column.
- FROM orders: This specifies the source of the data for the query, which is the 'orders' table. The FROM keyword is used to indicate the table from which the data will be selected. In this case, it selects data from the 'orders' table.
Relational Algebra Expression:

Relational Algebra Tree:

Output:
MAX(ORD_AMOUNT)
---------------
4500
Visual Presentation:
SQL MAX() with addition of two columns
To get the maximum value of (opening_amt+receive_amt) from the customer table, the following SQL statement can be used :
Sample table: customer
+-----------+-------------+-------------+--------------+--------------+-------+-------------+-------------+-------------+---------------+--------------+------------+ |CUST_CODE | CUST_NAME | CUST_CITY | WORKING_AREA | CUST_COUNTRY | GRADE | OPENING_AMT | RECEIVE_AMT | PAYMENT_AMT |OUTSTANDING_AMT| PHONE_NO | AGENT_CODE | +-----------+-------------+-------------+--------------+--------------+-------+-------------+-------------+-------------+---------------+--------------+------------+ | C00013 | Holmes | London | London | UK | 2 | 6000.00 | 5000.00 | 7000.00 | 4000.00 | BBBBBBB | A003 | | C00001 | Micheal | New York | New York | USA | 2 | 3000.00 | 5000.00 | 2000.00 | 6000.00 | CCCCCCC | A008 | | C00020 | Albert | New York | New York | USA | 3 | 5000.00 | 7000.00 | 6000.00 | 6000.00 | BBBBSBB | A008 | | C00025 | Ravindran | Bangalore | Bangalore | India | 2 | 5000.00 | 7000.00 | 4000.00 | 8000.00 | AVAVAVA | A011 | | C00024 | Cook | London | London | UK | 2 | 4000.00 | 9000.00 | 7000.00 | 6000.00 | FSDDSDF | A006 | | C00015 | Stuart | London | London | UK | 1 | 6000.00 | 8000.00 | 3000.00 | 11000.00 | GFSGERS | A003 | | C00002 | Bolt | New York | New York | USA | 3 | 5000.00 | 7000.00 | 9000.00 | 3000.00 | DDNRDRH | A008 | | C00018 | Fleming | Brisban | Brisban | Australia | 2 | 7000.00 | 7000.00 | 9000.00 | 5000.00 | NHBGVFC | A005 | | C00021 | Jacks | Brisban | Brisban | Australia | 1 | 7000.00 | 7000.00 | 7000.00 | 7000.00 | WERTGDF | A005 | ........ | C00011 | Sundariya | Chennai | Chennai | India | 3 | 7000.00 | 11000.00 | 7000.00 | 11000.00 | PPHGRTS | A010 | +-----------+-------------+-------------+--------------+--------------+-------+-------------+-------------+-------------+---------------+--------------+------------+
SELECT MAX(opening_amt + receive_amt) -- Selects the maximum value of the sum of 'opening_amt' and 'receive_amt'
FROM customer; -- Specifies the 'customer' table as the source of data
Explanation:
- SELECT MAX(opening_amt + receive_amt): This is the main part of the SQL query. It calculates the sum of the 'opening_amt' and 'receive_amt' columns for each row in the 'customer' table and then selects the maximum value among these sums. The MAX() function is used to find the maximum value.
- FROM customer: This specifies the source of the data for the query, which is the 'customer' table. The FROM keyword is used to indicate the table from which the data will be selected. In this case, it selects data from the 'customer' table.
Relational Algebra Expression:

Relational Algebra Tree:

Output:
MAX(OPENING_AMT+RECEIVE_AMT)
----------------------------
19000
Note: Outputs of the said SQL statement shown here is taken by using Oracle Database 10g Express Edition
Here is a slide presentation of all aggregate functions.
Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.
PREV : Avg with round, group by
NEXT : Max with Group by, Order by
