Sunday, 17 July 2016

1. Write a query to find the nth highest salary in SQL, where n can be any number.

select MAX(Salary) from Employee;

2. Write a SQL query to get the second highest salary from the table?

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 )

SELECT TOP 1 salary FROM ( SELECT TOP 2 salary FROM employees ORDER BY salary DESC) AS emp ORDER BY salary ASC