Tags
InterviewQuestions, InterviewQuestions for SQL, Queries for SQL Interview, SQLSERVER, T SQL Puzzles, TSQL, TSQL Queries
– Generic Logic To Find Highest Salary
– Query can be used to find out n’th salary, set @rowNum = n where n is an integer
– Create a table CREATE TABLE Employee ( Name varchar(50) NOT NULL, Salary int NOT NULL ) – Insert the values INSERT INTO Employee(Name, Salary) VALUES (‘e5′, 45000) INSERT INTO Employee(Name, Salary) VALUES (‘e3′, 30000) INSERT INTO Employee( Name, Salary ) VALUES (‘e2′, 49000) INSERT INTO Employee( Name, Salary ) VALUES (‘e4′, 36600) INSERT INTO Employee( Name, Salary ) VALUES (‘e1′ , 58000) SELECT * FROM Employee – Select second highest salary DECLARE @rowNum int SET @rowNum = 2 Select top 1 * from Employee WHERE salary in (SELECT TOP (@rowNum) salary FROM Employee ORDER BY salary DESC) ORDER BY salary
–Gauri Deshpande and Pawan Kumar
pawankkmr@hotmail.com