×

SQL SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may be a string expression.

The syntax for the select max function:

SELECT MAX(column_name) FROM table_name WHERE conditions;

Let’s take deep dive into the SQL SELECT MAX.

Consider the already existing tables, which have the following data:

Table Name: Employees

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAC#5
1003NIKHILVANI50500JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAIC#5
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3
4003RUCHIKAAGARWAL60000DELHIORACLE1
5001ARCHITSHARMA55500DELHITESTING4

Example 1: Execute a query that searches the max salary of employees from the employees' table.

SELECT MAX (SALARY) AS 'MAXSALARY' FROM EMPLOYEES;

 In the above query, we find the maximum salary from the entire employees' table. We have named the Max (salary) function as max salary. The salary will be displayed as the column name when the output is returned.

Output:

MAX SALARY
65500
SQL SELECT MAX

As you can see, the maximum salary is 65500 from the employees' table

Example 2: Execute a query to search the max salary of employees whose residing city is Mumbai from the employees' table.

SELECT CITY, MAX(SALARY) AS 'SALARY'  FROM EMPLOYEES WHERE CITY = 'MUMBAI';

We have named max (salary) as salary in the above query, displayed as column name when output is returned. We have found the max salary of an employee in Mumbai and displayed the city name.

Output:

CITYSALARY
MUMBAI60500
SQL SELECT MAX

As you can see, the max salary of an employee who resides in the Mumbai city is 60500

Example 3: Write a query to find the employees' maximum salary from the employees' table of employees whose Department is Java.

SELECT DEPARTMENT, MAX(SALARY) AS 'MAXIMUM_SALARY'  FROM EMPLOYEES WHERE DEPARTMENT = 'JAVA';

In the above query, we have aliased max (salary) as maximum_salary, displayed as column name when output is returned. We have found the maximum salary of an employee whose Department is Java and displayed the department name.

Output:

DEPARTMENTMAXIMUM_SALARY
JAVA58500
SQL SELECT MAX

As you can see, the maximum salary of an employee from the Java department is 58500

Example 4: Write a query to find the maximum salary of employees whose Department includes one of the lists is Oracle and FMW from the employees' table.

SELECT MAX(SALARY) AS 'SALARY'  FROM EMPLOYEES WHERE DEPARTMENT IN ('ORACLE', 'FMW');

We have aliased max (salary) in the above query, displayed as column name when output is returned. We have found the maximum salary of an employee whose Department is Oracle and FMW.

Output:

SALARY
65500
SQL SELECT MAX

Example 5: Execute a query to search the max salary of employees whose salary is greater than 55000 and city includes Noida, Delhi from employees’ table group by city.

SELECT CITY, MAX(SALARY) AS EMPLOYEE_SALARY FROM EMPLOYEES WHERE SALARY > 55000 AND CITY IN ('NOIDA', 'DELHI') GROUP BY CITY;

We have named Max (salary) as EMPLOYEE_SALARY in the above query, displayed as a column name when output is returned. We have found the maximum salary of an employee whose salary is greater than 55000. Also, the employee city must be one of the names included in the city IN parameter. We also used the GROUP BY clause followed by the city column. The Noida city will be grouped into one city of all the employees whose city in Noida and found the employees' salary, and the same approach is used for Delhi city.

Output:

CITYEMPLOYEE_SALARY
DELHI60000
NOIDA60000
SQL SELECT MAX

Employee resides in Delhi city, the max salary is 60000, and Employee resides in Noida city, max salary is 60000.

Example 6: Execute a query to search max employee salary from Employee's table cities are unique of employees group by Department.

SELECT CITY, MAX(DISTINCT SALARY) AS 'EMPLOYEE_SALARY' FROM EMPLOYEES GROUP BY DEPARTMENT;

We have named max (DISTINCT salary) as Employee_salary in the above query, displayed as column name when output is returned. We have found the maximum employee salary from the employees' table considering unique cities in a group by department column.

Output:

DEPARTMENTEMPLOYEE_SALARY
NOIDA60000
JAIPUR65500
PUNE58500
PUNE65500
MUMBAI60500
SQL SELECT MAX

Example 7:  Write a query to find the maximum employee salary from the Employee's table unique department of employees group by the Department.

SELECT DEPARTMENT, MAX(DISTINCT SALARY) AS 'EMPLOYEE_SALARY' FROM EMPLOYEES GROUP BY DEPARTMENT;

In the above query, we have aliased max (DISTINCT salary) as Employee_salary, displayed as column name when output is returned. We have found the maximum employee salary from the employees' table considering unique departments grouped by department column.

Output:

DEPARTMENTEMPLOYEE_SALARY
C#60000
FMW65500
JAVA58500
ORACLE65500
TESTING60500
SQL SELECT MAX

Example 8: Execute a query to search the maximum salary of the Employee from the Employee's table group by the Department where max() salary is greater than 55000.

SELECT CITY, MAX(SALARY) AS SALARY FROM EMPLOYEES GROUP BY DEPARTMENT HAVING MAX(SALARY) > 55000;

We have named max (salary) as salary in the above query, displayed as column name when output is returned. We have found maximum employee salary from the employees' table followed group by department name and used having a condition where max() is greater than 55000.

Output:

DEPARTMENTEMPLOYEE_SALARY
NOIDA60000
JAIPUR65500
PUNE58500
PUNE65500
MUMBAI60500
SQL SELECT MAX

As we can see, only cities with aggregate salaries are greater than 55000. Having a clause is like where clause. Having a clause is used when we want to apply some condition to the aggregate function. We used having clause only when we used aggregate function in the query.

Example 9: Write a query to find an employee's maximum salary with a laptop group by the Department.

SELECT DEPARTMENT, MAX(SALARY) AS SALARY FROM EMPLOYEES WHERE EMPLOYEEID IN (SELECT EMPLOYEEID FROM LAPTOP) GROUP BY DEPARTMENT;

We have named max (salary) as salary in the above query, displayed as column name when output is returned. Explanation of the above query, First sub-query will get executed (SELECT EMPLOYEEID FROM LAPTOP); as a result, we will get the employee ids, including the null value. After the sub-query is executed, the main query will get executed SELECT DEPARTMENT, MAX(SALARY) AS SALARY FROM EMPLOYEES WHERE EMPLOYEEID IN (output of sub-query). In the IN operator of the main query, there is an employee's id, which is output from the sub-query according to the IN operator. We will get our final result which is the group by department name.

Output:

DEPARTMENTSALARY
C#60000
JAVA58500
ORACLE60000
TESTING55500
SQL SELECT MAX

Related Topics

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

SQL ORDER BY

SQL ORDER BY The SQL ORDER BY clause is used to sort the data stored in tables in the database. The sorting can be done in an ascending way, descending way,...

5 minutes read.

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

SQL LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

SQL Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

4 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL INSERT Statement

In this tutorial, we will help you to understand and learn how to insert records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

SQL Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.