codesnout.com

SQL MAX()

Google Ads

SQL MAX() Function


SQL Sample Code - SQL MAX() Function


SELECT MAX(column_name)
FROM table_name;

See the SQL samples section below for an explanation of the Max function.


What does the MAX() Function do?

The SQL MAX() function simply retrieves the largest value that exists in a specified column.

Note: The MAX() function can only be used on database columns with numeric based data-types. See our data-types section for more information.


Sample 1 – SQL MAX() Function

In this SQL sample we will use the database table ‘tblCompany’.

Company_ID CompanyName Address Town Sales
1 SQL Sample 1 Sample St Hamburg 10000
2 SQL Code Land 2 Code Rd Hamburg 20000
3 Sample Code World 66 SQL St Curry 18000
4 SQL Reference Ltd 34 Reference St Pisa 17000

In this SQL sample we simply want to know the largest Sales figure in the tblCompany table.

Use the SELECT statement below:

SELECT MAX(Sales) AS Sales_Max FROM tblCompany;

The result will look like this:

Sales_Max
20000

Note: the AS operator is used to name the result column as ‘Sales_Max’. See the Alias section of this website to learn more about renaming database columns and tables.


Sample 2 – SQL MAX() Function using GROUP BY.

In this SQL sample we will use the database table ‘tblCompany’.

Company_ID CompanyName Address Town Sales
1 SQL Sample 1 Sample St Hamburg 10000
2 SQL Code Land 2 Code Rd Hamburg 20000
3 Sample Code World 66 SQL St Curry 18000
4 SQL Reference Ltd 34 Reference St Pisa 17000

In this SQL sample we want to know the largest Sales records for each town in the tblCompany table.

Use the SELECT statement below:

SELECT Town, MAX(Sales) FROM tblCompany
GROUP BY Town;

The result will look like this:

Town COUNT(Sales)
Hamburg 20000
Curry 18000
Pisa 17000

 


Related SQL Sample Code:


About Us | Privacy Policy | Contact Us | ©2010 Thunderousity Information Management Solutions | www.autosnout.com - Car Performance Statistics Website Utilising SQL | SQL Blog |

SQL Home | SQL Intro | SQL SELECT Statement | DISTINCT | WHERE | SQL Wildcards | AND OR | IN | BETWEEN | LIKE | ORDER BY | GROUP BY | TOP | ALIAS | DELETE | INSERT | UPDATE | CREATE DATABASE | CREATE TABLE | ALTER | INNER JOIN | FULL JOIN | LEFT JOIN | RIGHT JOIN | UNION | CONSTRAINTS | NOT NULL| Aggregate Functions | SUM | AVG | COUNT | MAX | MIN | FIRST | LAST | GROUP BY | HAVING | LIMIT | Microsoft Access Data Types | MySQL Data Types | SQL Server Data Types | ASCII HTML Reference Sheet |