This is one of the most common interview question for freshers and experienced candidates. We can do this in many ways.
Look at the following table?
EMPLOYEE TABLE
select max( salary) from employee
select max( salary) from employee < (select max( salary) from employee)
select max( salary) from employee <> (select max( salary) from employee)
select max( salary) from employee not in (select max( salary) from employee)
select top 1 salary from
order by salary desc)
result order by salary
select top 1 salary from
order by salary desc)
result order by salary
Look at the following table?
EMPLOYEE TABLE
Name | Salary |
---|---|
1000 | |
2000 | |
3000 | |
4000 |
If you want to find the max salary from the table then we have to use max( ) function as follows.
Then result is 4000.
If you want to find 2nd highest salary.
OR
OR
Then result is 3000.
The following is the query to find nth highest salary.
(select top N salary from employee
Based on above query if you want to find 3rd highest salary, then write 3 at the place N as follows.
(select top 3 salary from emp11
Then the result is 2000.
No comments:
Post a Comment