SQL Max

The SQL Max() function returns the largest value of an expression.

SQL Max() Function Syntax

SELECT MAX(expression)
FROM tables
WHERE predicates

SQL Max() Function Example

Table: Employees

EmployeeId FirstName LastName Department Salary
203 Mazojys Fxoj Finance 78000
204 Jozzh Lnanyo Finance 45800
205 Syllauu Dfaafk Finance 57000
206 Gecrrcc Srlkrt Finance 62000
302 Jssme Bdnaa Development 75000
303 Dnnaao Errllov Development 55000
304 Tyoysww Osk Development 49000

Example 1
We want to know the highest salary among all employees:

SELECT Max(Salary) AS "Highest salary"
FROM Employees

The result will look like:

Highest salary
78000

Example 2
If we want to select highest salary for each department, we can use GROUP BY clause:

SELECT Department, MAX(Salary) AS "Highest salary"
FROM Employees
GROUP BY Department

The result will look like:

Department Highest salary
Finance 78000
Development 75000

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>