AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL MAX Function |
SQL > SQL Functions >
MAX Function
The MAX function is used to find the maximum value in an expression. SyntaxThe syntax for the MAX function is, SELECT MAX (<expression>)
FROM "table_name"; <expression> can be a column name or an arithmetic operation. An arithmetic operation can include more than one column, such as ("column1" + "column2"). It is also possible to have one or more columns in addition to the MAX function in the SELECT statement. In those cases, these columns need to be part of the GROUP BY clause as well: SELECT "column_name1", "column_name2", ... "column_nameN", MAX (<expression>)
FROM "table_name"; GROUP BY "column_name1", "column_name2", ... "column_nameN"; ExamplesWe use the following table for our examples. Table Store_Information
Example 1: MAX function on a columnTo find the maximum sales amount, we type in, SELECT MAX(Sales) FROM Store_Information;
Result:
1500 represents the maximum value of all Sales entries: 1500, 250, 300, and 700. Example 2: MAX function on an arithmetic operationAssume that sales tax is 10% of the sales amount, we use the following SQL statement to get the maximum sales tax amount: SELECT MAX(Sales*0.1) FROM Store_Information;
Result:
SQL will first calculate "Sales*0.1" and then apply the MAX function to the result for the final answer. Example 3: MAX function with a GROUP BY clauseTo get the maximum sales amount for each store, we type in, SELECT Store_Name, MAX(Sales) FROM Store_Information GROUP BY Store_Name;
Result:
|
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.