Skip to main content

Highest salary query in sql server

DECLARE @dtDepartment TABLE
(
Id int,
Name varchar(100)
)
DECLARE @dtEmployee TABLE
(
Id int,
Name varchar(100),
DeptId int
)
DECLARE @dtEmpSalary TABLE
(
Id int,
EmpId int,
Salary int
)

INSERT INTO @dtDepartment VALUES(1,'.NET')
INSERT INTO @dtDepartment VALUES(2,'Java')

INSERT INTO @dtEmployee VALUES(1,'Brijesh',1)
INSERT INTO @dtEmployee VALUES(2,'Parth',1)
INSERT INTO @dtEmployee VALUES(3,'Ruchir 1',1)
INSERT INTO @dtEmployee VALUES(4,'Jainik 1',1)
INSERT INTO @dtEmployee VALUES(5,'Ruchir',2)
INSERT INTO @dtEmployee VALUES(6,'Jainik',2)
INSERT INTO @dtEmployee VALUES(7,'Jainik',2)
INSERT INTO @dtEmployee VALUES(8,'Jainik',2)

INSERT INTO @dtEmpSalary VALUES(1,1,10000)
INSERT INTO @dtEmpSalary VALUES(2,2,15000)
INSERT INTO @dtEmpSalary VALUES(3,3,12000)
INSERT INTO @dtEmpSalary VALUES(4,4,12000)
INSERT INTO @dtEmpSalary VALUES(5,5,12000)
INSERT INTO @dtEmpSalary VALUES(6,6,15000)
INSERT INTO @dtEmpSalary VALUES(7,7,255555)
INSERT INTO @dtEmpSalary VALUES(8,8,1121313)

SELECT * FROM @dtDepartment
SELECT * FROM @dtEmployee
SELECT * FROM @dtEmpSalary3

;WITH temp as
(
SELECT emp.Id as EmpId,emp.Name as EmpName,
dept.Id as DeptId,dept.Name as DeptName,emps.Salary,
DENSE_RANK() OVER(PARTITION BY dept.Id ORDER BY Salary DESC) AS 'SalaryRank'
FROM @dtDepartment dept
JOIN @dtEmployee emp on emp.DeptId = dept.Id
JOIN @dtEmpSalary emps on emps.EmpId = emp.Id
)
SELECT * FROM temp
WHERE SalaryRank <= 10

SELECT * FROM
@dtDepartment dept
JOIN @dtEmployee emp on emp.DeptId = dept.Id
JOIN @dtEmpSalary emps on emps.EmpId = emp.Id


;WITH Salaries AS
(
    SELECT Id,Salary,
       DENSE_RANK() OVER(ORDER BY Salary DESC) AS 'SalaryRank'
    FROM
        @dtEmpSalary
)
SELECT distinct Id,Salary
FROM Salaries
WHERE SalaryRank = 1

SELECT TOP 1 * FROM
(
SELECT distinct top 1 es.Salary FROM @dtEmpSalary es order by es.Salary desc
) t order by Salary


Comments

Popular posts from this blog

Tata Punch: A Compact SUV That Packs a Punch

Message Helper for MVC Application

Message Helper for MVC Application public static class MessageExtensions { public enum MessageType { Success = 0, Info = 1, Warning = 2, Error = 3 } public static void ShowMessage( this Controller controller, MessageType messageType, string message, bool showAfterRedirect = false ) { string messageTypeKey = messageType.ToString(); if (showAfterRedirect) { controller.TempData[messageTypeKey] = message; } else { controller.ViewData[messageTypeKey] = message; } } public static HtmlString RenderMessages( this HtmlHelper htmlHelper, bool clearMessage = true ) { string messages = String .Empty; foreach ( string messageType in Enum .GetNames( t...

Postback Page using Javascript in ASP.net

Postback Page using Javascript in ASP.net For Calling Function: __doPostBack('__Page', ''); Function: function __doPostBack(eventTarget, eventArgument) {         if (!theForm.onsubmit || (theForm.onsubmit() != false)) {             theForm.__EVENTTARGET.value = eventTarget;             theForm.__EVENTARGUMENT.value = eventArgument;             theForm.submit();         }     }