Monday 8 August 2016

Find out nth order salary in MySQL

To find out the nth highest salary from employee table :
SYNTAX:

SELECT * from employee emp WHERE (n) = (SELECT COUNT(emp2.salary) FROM employee emp2 WHERE emp2.salary >= emp.salary)
where n is the number of salary which you want.

OR

To Find the 2nd Highest Salary in following ways



SELECT name, salary FROM employee where salary NOT IN (SELECT MAX(salary) from employee)

SELECT MAX(salary) FROM employee where salary NOT IN (SELECT MAX(salary) from employee)
SELECT DISTINCT salary FROM employee ORDER BY salary DESC LIMIT 1,1;

No comments:

Post a Comment