Please get the 4 th maximum salary from a table without
using any sql keyword (TOP,MAX are mot allowed)

Answer Posted / pradeep

DECLARE @test TABLE(Empnm VARCHAR(10), Salary VARCHAR(10))

INSERT INTO @test (Empnm, Salary )
SELECT 'A', '200'
UNION ALL
SELECT 'B', '300'
UNION ALL
SELECT 'A', '200'
UNION ALL
SELECT 'B', '300'
UNION ALL
SELECT 'C', '400'
UNION ALL
SELECT 'C', '400'
UNION ALL
SELECT 'E', '100'
UNION ALL
SELECT 'D', '500'

SELECT * FROM @test


SELECT Empnm, Salary, (SELECT COUNT(DISTINCT(SALARY)) FROM
@test AS B WHERE A.Salary <= B.SALARY)
FROM @test AS A
GROUP BY Empnm, Salary
HAVING 4 = (SELECT COUNT(DISTINCT(SALARY)) FROM @test AS B
WHERE A.Salary <= B.SALARY)

Is This Answer Correct ?    9 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is transact-sql ddl trigger?

722


Explain the usage of floor function in sql server.

570


what are the different types of SSRS reports?

116


how would you improve etl (extract, transform, load) throughput?

630


What is shared lock?

574






How to define and use table alias names in ms sql server?

655


What is transaction server implicit?

604


What is the architecture of ms sql reporting service?

603


You want to check the syntax of a complicated update sql statement without executing it. What command should you use?

566


How to turn on the mssql api module in php?

664


What is the difference between DataRow.Delete() and DataRow.Remove()?

835


As a general practice, it is recommended to have dbo be the owner of all database objects however, in your database you find number of tables owned by a user other than dbo, how could you fix this?

715


When should you use an instead of trigger?

624


Explain what stored procedure sp_replcounters is used for? : sql server replication

1124


What is recompile sql server?

656