Click here to Skip to main content
15,895,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
SQL
declare @table as table
(
  id int identity(1,1) 
 ,salary int 
)

insert into @table values(1000)
insert into @table values(2000)
insert into @table values(5000)
insert into @table values(4000)
insert into @table values(1000)
insert into @table values(8000)
insert into @table values(9000)
insert into @table values(6000)
insert into @table values(1000)
insert into @table values(7000)
insert into @table values(3000)

select A.salary from @table as A
where (select count(*) from @table B
            where b.salary < A.salary)>5
Posted

1 solution

Output:

salary
-------
5000
8000
9000
6000
7000

Explanation:
Below sql snippet selects single row from temp table.
select A.salary from @table as A

Here where condition is a sub query. Lets checkout this sub query.
select count(*) from @table B where b.salary < A.salar

This query gives the counts of those whose salary is greater than the salaries of A.

If this count is greater than 5 then those salaries will be printed!
 
Share this answer
 
Comments
Magic Wonder 29-Jan-16 2:28am    
Are you sure "If this count is greater than 5 then those salaries will be printed!"???
csharpbd 29-Jan-16 5:18am    
yeah pretty sure that if the value of count is greater than 5 then those rows will be in output.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900