Second maximum salary using sub query and IN clause
Sub queries in SQL are great tool for this kind of scenario, here we first select maximum salary and then another maximum excluding result of subquery.
SELECT max(salary) FROM Employee WHERE salary NOT IN (SELECT max(salary) FROM Employee);
SELECT max(salary) FROM Employee WHERE salary < (SELECT max(salary) FROM Employee);
Second highest salary using TOP keyword of Sybase or SQL Server database
SELECT TOP 1 salary FROM ( SELECT TOP 2 salary FROM employees ORDER BY salary DESC) AS emp ORDER BY salary ASC
Sub queries in SQL are great tool for this kind of scenario, here we first select maximum salary and then another maximum excluding result of subquery.
SELECT max(salary) FROM Employee WHERE salary NOT IN (SELECT max(salary) FROM Employee);
SELECT max(salary) FROM Employee WHERE salary < (SELECT max(salary) FROM Employee);
Second highest salary using TOP keyword of Sybase or SQL Server database
SELECT TOP 1 salary FROM ( SELECT TOP 2 salary FROM employees ORDER BY salary DESC) AS emp ORDER BY salary ASC
Here is what this SQL query is doing : First find out top 2 salary from Employee table and list them in descending order, Now second highest salary of employee is at top so just take that value.==> distinct
Second maximum salary using LIMIT keyword of MYSQL database
SELECT salary FROM (SELECT salary FROM Employee ORDER BY salary DESC LIMIT 2) AS emp ORDER BY salary LIMIT 1;
Read full article from How to find second highest or maximum salary of Employee in SQL - Interview question
Second maximum salary using LIMIT keyword of MYSQL database
SELECT salary FROM (SELECT salary FROM Employee ORDER BY salary DESC LIMIT 2) AS emp ORDER BY salary LIMIT 1;
Read full article from How to find second highest or maximum salary of Employee in SQL - Interview question
No comments:
Post a Comment