0 votes
in Sql by
Write the SQL query to get the third maximum salary of an employee from a table named employees.

1 Answer

0 votes
by
The following query is the simplest way to get the third maximum salary of an employee:

SELECT * FROM `employees` ORDER BY `salary` DESC LIMIT 1 OFFSET 2  

Here is the demo example that shows how to get the third maximum salary of an employee.

SQL Interview Questions and Answers

The following are the alternative way to get the third-highest salary of an employee:

A. Using LIMIT Keyword

SELECT salary FROM employees   

ORDER BY salary DESC   

LIMIT 2, 1;  

B. Using Subquery

SELECT salary    

FROM   

    (SELECT salary   

     FROM employees   

     ORDER BY salary DESC   

     LIMIT 3) AS Temp   

ORDER BY salary LIMIT 1;  

C. Using TOP Keyword

SELECT TOP 1 salary   

FROM   

    (SELECT DISTINCT TOP 3 salary   

     FROM employees   

     ORDER BY salary DESC) AS Temp   

ORDER BY salary ASC;
...