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
(
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
Post a Comment