Today we will understand how to find the Nth highest salary in an optimized way and the Nth highest salary in each department using the SQL server.
CREATE TABLE [dbo].[Department]
(
DeptId INT IDENTITY(1,1) PRIMARY KEY,
DeptName VARCHAR(100)
);
GO
INSERT INTO [dbo].[Department] (DeptName) VALUES ('Computer');
INSERT INTO [dbo].[Department] (DeptName) VALUES ('Research');
INSERT INTO [dbo].[Department] (DeptName) VALUES ('Sales');
INSERT INTO [dbo].[Department] (DeptName) VALUES ('Accouting');
CREATE TABLE [dbo].[Employee]
(
EmpId INT IDENTITY(1,1) PRIMARY KEY,
EmpName VARCHAR(20),
ManagerId INT NULL,
Salary MONEY,
DeptId INT FOREIGN KEY REFERENCES [dbo].[Department](DeptId)
);
GO
INSERT INTO [dbo].[Employee] (EmpName,ManagerId,Salary,DeptId) VALUES ('Rajesh',NULL, 60000,1);
INSERT INTO [dbo].[Employee] (EmpName,ManagerId,Salary,DeptId) VALUES ('Rohan',1, 50000,1);
INSERT INTO [dbo].[Employee] (EmpName,ManagerId,Salary,DeptId) VALUES ('Janak',NULL, 70000,2);
INSERT INTO [dbo].[Employee] (EmpName,ManagerId,Salary,DeptId) VALUES ('Mohan',3, 50000,2);
INSERT INTO [dbo].[Employee] (EmpName,ManagerId,Salary,DeptId) VALUES ('Rahul',NULL, 40000,3);
INSERT INTO [dbo].[Employee] (EmpName,ManagerId,Salary,DeptId) VALUES ('Pragnesh',NULL, 40000,4);
Hope you can successfully able to create both the above tables in your database now we go for example.
Actual employee data in the employee table.
SELECT * FROM Employee ORDER BY Salary DESC
1) Find the 2nd highest salary or the Nth highest salary using CTE (Common Table Expression).
Example of 2Nd highest salary.
DECLARE @Rank INT=2;
WITH EmployeeCte AS
(
SELECT DENSE_RANK() OVER (ORDER BY Salary DESC) As SalaryRank,* FROM Employee
)
SELECT * FROM EmployeeCte WHERE SalaryRank=@Rank
Result
Example of Nth highest salary with CTE.
DECLARE @Rank INT=N;
WITH EmployeeCte AS
(
SELECT DENSE_RANK() OVER (ORDER BY Salary DESC) As SalaryRank,* FROM Employee
)
SELECT * FROM EmployeeCte WHERE SalaryRank=@Rank
DECLARE @Rank INT=2;
SELECT * FROM Employee Emp1 WHERE (@Rank-1)=
(
SELECT COUNT(DISTINCT(Salary)) FROM Employee Emp2 WHERE Emp2.Salary>Emp1.Salary
)
Result
DECLARE @Rank INT=2;
WITH EmployeeCte AS
(
SELECT DENSE_RANK() OVER (PARTITION BY DeptId ORDER BY Salary DESC) As SalaryRank,* FROM Employee
)
SELECT
E.EmpId
,E.EmpName
,E.Salary
,D.DeptName
FROM
EmployeeCte E
INNER JOIN Department D ON E.DeptId=D.DeptId
WHERE
SalaryRank=@Rank
Result
Using this query you can find 3rd, 4th,5th and Nth highest salary from the employee table and also you can find the department wise highest salary in an optimized way. Also, you can find the Nth lowest salary by changing the order with ASC.
I hope you have liked this article to the understanding of Find the Nth highest salary or 2nd highest salary and Department wise Nth highest salary.
Please feel free to comment if you have found anything incorrect, or you want to share more information about the topic discussed above.
Please Share this article so it will help to another one because “Helping millions grow better”.
Follow my blog https://mydotnetguru.blogspot.com to read the more interesting technical articles.
Next Recommended Article:
- Different ways to remove or delete duplicate rows from a SQL Table.
- When to use abstract class and interface in C#?
- Async and Await in C#
- Top 100+ popular ASP.Net MVC Interview Questions and Answers.
- Top 100+ popular SQL Interview Questions and Answers Blog.
- Top 100+ popular C# Interview Questions and Answers.
- @Input, @Output decorator and EventEmitter class in Angular.
- Dependency Injection and types of dependency injection.
No comments:
Post a Comment