Table :emp
eid Name salary
1 ravikb 2000
2 gorav 3000
3 sorav 2000
4 rohit 2500
5 hemat 4500
6 sachin 6230
7 suraj 4054
8 sttt 8000
13 sttt 1000
14 fr 2010
1.find second highest salary
select min(salary) salary from emp where salary in(select top 2 salary from emp order by salary desc)
OUTPUT :
salary
6230
2. Find TOP 3 highest salary Without Top Function
CASE 1.
Select * from emp e1 where 2>=(select count(salary) from emp e2 where e1.salary<e2.salary)
order by salary asc
CASE 2.
With CTE as
(
Select eid,Name,salary ,ROW_NUMBER()over(order by salary desc)r from emp
)
select Salary from CTE where r<=3
OUTPUT :
8 sttt 8000
6 sachin 6230
5 hemat 4500
3. Find TOP 2 Lowest salary Without Top Function
With CTE as
(
Select eid,Name,salary ,ROW_NUMBER()over(order by salary asc)r from emp
)
select Distinct Salary from CTE where r<=2
OUTPUT :
1000
2000
No comments:
Post a Comment