Thursday, 26 September 2013

Second Max Salary ( nth max salary)

Second Max Salary ( nth max salary)

In interviews related to SQL a questions is almost always asked. You must have heard or being asked in your career that is " Find Second Max Salary from the table". A beginner of SQL will start thinking about the System function/ Aggregate Function MAX() and will get confused that MAX() will return the Maximum value, how is it possible to find the Second max.

I will say, why just second max as we can find 5th max, 10th max that is nth max.

Here is the solution 

lets practice and start with the beginning.

Step 1

Use/ Create a database

Query
CREATE DATABASE SQLisAwesome

USE SQLisAwesome

Step 2

Create a Table

Query
 CREATE TABLE SalaryDummy (
    [Id] [INT] NOT NULL IDENTITY(1,1),
    [Name] [VARCHAR](100) NOT NULL,
    [Designation] [VARCHAR](100) NOT NULL,
    [Salary] [INT] NOT NULL,
 CONSTRAINT [PK_SalaryDummy] PRIMARY KEY CLUSTERED
    ( [Id] ASC )
)

Step 3

Insert dummy values

Query
INSERT INTO [dbo].[SalaryDummy]([Name],[Designation],[Salary]) VALUES('Ram','SE',25000)
INSERT INTO [dbo].[SalaryDummy]([Name],[Designation],[Salary]) VALUES('Sham','SA',26000)
INSERT INTO [dbo].[SalaryDummy]([Name],[Designation],[Salary]) VALUES('Suresh','OE',15000)
INSERT INTO [dbo].[SalaryDummy]([Name],[Designation],[Salary]) VALUES('Ramesh','TL',30000)

 Step 4

 Get second Max salary

Using Subquery (Replace 2 at right hand side of '=' in the where condition with your desired no of Max salary)

Query
SELECT  a.Salary
FROM   SalaryDummy a
WHERE   Salary IS NOT NULL
AND (SELECT COUNT(Salary) FROM SalaryDummy b WHERE b.Salary>a.Salary)+1=2

now suppose you have two different persons having same salary

Query 
INSERT INTO [dbo].[SalaryDummy]([Name],[Designation],[Salary]) VALUES('Aman','TL',30000)

In this case we will use DISTINCT key word in the condition

Query 
SELECT  a.Salary
FROM   SalaryDummy a
WHERE   Salary IS NOT NULL
AND (SELECT COUNT( DISTINCT Salary) FROM SalaryDummy b WHERE b.Salary>a.Salary)+1=2
You can also find up to nth salaries in a single query by modifying the where condition a little

Query
SELECT  a.Salary
FROM   SalaryDummy a
WHERE   Salary IS NOT NULL
AND (SELECT COUNT( DISTINCT Salary) FROM SalaryDummy b WHERE b.Salary>a.Salary)+1<=2
ORDER BY a.Salary DESC 

You can also use the logic in the where condition for displaying the position order of salary

Query
SELECT  a.Salary, (SELECT COUNT( DISTINCT Salary) FROM SalaryDummy b WHERE b.Salary>a.Salary)+1 AS [Position Order]
FROM   SalaryDummy a
WHERE   Salary IS NOT NULL
AND (SELECT COUNT( DISTINCT Salary) FROM SalaryDummy b WHERE b.Salary>a.Salary)+1<=2
ORDER BY a.Salary DESC

All the above can also be achieved using JOINs and CTE (Common table expression)

Query
;WITH abc AS (       
    SELECT  a.Salary, COUNT ( DISTINCT a.Salary) AS Position
    FROM    SalaryDummy a INNER JOIN SalaryDummy b ON b.Salary>=a.Salary
    WHERE   a.Salary IS NOT NULL
    GROUP BY a.Salary
) SELECT * FROM abc WHERE abc.Position=2

and for up to nth salary

Query
;WITH abc AS (       
    SELECT  a.Salary, COUNT ( DISTINCT a.Salary) AS Position
    FROM    SalaryDummy a INNER JOIN SalaryDummy b ON b.Salary>=a.Salary
    WHERE   a.Salary IS NOT NULL
    GROUP BY a.Salary
) SELECT abc.Salary, abc.Position FROM abc WHERE abc.Position<=2
ORDER BY abc.Salary DESC