How to get the highest paid employee row - sql-server

A table consists of employee name, address, phone, department, salary:
How to get the highest paid employee row from each department?
I tried with
select dept, max(salary) from employee group by dept
but it gives only two columns. But I want to select an entire row. How to do it?
Alternatively how to add more columns to the result?
(I am using SQL Server 2008)

You simply need to join the query you currently have back to the employee table to get the full employee information.
select e.*
from employee e
inner join (select dept, max(salary) ms from employee group by dept) m
on e.dept = m.dept and e.salary = m.ms

SELECT name,
address,
phone,
department,
salary,
dept
FROM (SELECT name,
address,
phone,
department,
salary,
dept,
row_number() OVER(PARTITION BY dept ORDER BY salary DESC) AS rn
FROM employee) AS e
WHERE e.rn = 1
Using row_number() will give you one row if there is a tie for the highest salary. If you want all the highest salaries for each department you should use rank() instead.
SELECT name,
address,
phone,
department,
salary,
dept
FROM (SELECT name,
address,
phone,
department,
salary,
dept,
rank() OVER(PARTITION BY dept ORDER BY salary DESC) AS rn
FROM employee) AS e
WHERE e.rn = 1

Something like this?
select * from employee where salary = (select max(salary) from employee)

with cte as (
select *, rank() over (partition by dept order by salary desc) as [r]
from employees
)
select * from cte where [r] = 1;

select * from employee
where salary in
(select max(salary) from employee group by dept);

If situation like you have both employee details and department in same table and you have to find the highest paid employee from each department like given below
EmployeeId EmployeeName Department Salary
1 Neeraj Dot Net 45000
2 Ankit Java 5000
3 Akshay Java 6000
4 Ramesh Dot Net 7600
5 Vikas Java 4000
7 Neha Php 8500
8 Shivika Php 4500
9 Tarun Dot Net 9500
Then you can solve it by using below solutions
Solution - 1
SELECT t.EmployeeName,t.Department,t.Salary
FROM(SELECT MAX(Salary) AS TotalSalary,Department FROM Employee GROUP BY Department)
AS TempNew Inner Join Employee t ON TempNew.Department=t.Department
and TempNew.TotalSalary=t.Salary
ORDER BY t.Department ASC
Solution -2
;WITH EmployeeDetails AS (
SELECT EmployeeName, Department, DENSE_RANK() OVER(PARTITION BY Department
ORDER BY Salary DESC) AS SalaryRank, Salary FROM Employee )
SELECT EmployeeName, Department, Salary FROM EmployeeDetails WHERE SalaryRank=1
OUTPUT
EmployeeName Department Salary
Neeraj Dot Net 45000
Akshay Java 6000
Neha Php 8500

Related

Top 5 Employees from each dept with lowest attendance

How do I find top 5 employees from each department with lowest attendance?
---Employee Table
--- Department Table
--- Attendance Table
I tried in this way to find top 5 salary from each department but how to apply in this specially for attendance table with employee and department table how to find top 5 employees with lowest attendance please assist me in this regard with cte.
Thanks
With ctetemp As
(
Select E.EmpId, E.Ename, E.Salary,D.Dname,
DENSE_RANK() OVER(PARTITION BY D.DeptId ORDER BY Salary desc) as rnw
from EmployeeTbl AS E JOIN Departmet AS D
ON D.DeptId = e.Dept_Id_Emp
)
Select * from ctetemp
where rnw IN (5)
You will need to reference the attendance table.
I'm assuming 'a' means 'absent' in this context.
SELECT
AbsenceSummary.DeptID
,AbsenceSummary.EmpId
,AbsenceSummary.AbsentDays
,AbsenceSummary.AbsentRank
FROM
(
SELECT
dept.DeptId
,att.EmpId
,COUNT(*) AS AbsentDays
,DENSE_RANK() OVER (PARTITION BY dept.DeptId ORDER BY COUNT(*) DESC) AS AbsentRank
FROM
attendance att
INNER JOIN
employee emp
ON emp.EmpID = att.EmpId
INNER JOIN
department dept
ON dpt.DeptId = emp.DeptId
WHERE
dept.DeptId IN (<the departments you care about>)
AND att.atdate >= <your minimum date>
AND att.atdate <= <your maximum date>
AND att.attendance = 'a' --absent
GROUP BY
dept.DeptId
,att.EmpId
) AbsenceSummary
WHERE
AbsenceSummary.AbsentRank <= 5
For the record, the AtId column in this situation is useless and possibly counterproductive. The key is (EmpId,atDate).

Find the third-highest salary in each department

I am trying to find the third-highest salary in each department if there is such.
SELECT DepartmentID
FROM Employees
GROUP BY DepartmentID
This is what I can do.
I looked at similar posts but not sure I understand how to do it with my table.
You can use row_number function to assign a order of salary, then get the 3rd one:
SELECT s.DepartmentID, s.Salary
FROM (
SELECT DepartmentID, Salary, ROW_NUMBER() OVER(PARTITION BY DepartmentID ORDER BY Salary DESC) AS salary_rank
FROM Employees) s
WHERE s.salary_rank=3
SELECT e.first_name,
e.last_name,
d.department_name,
salary,
ROW_NUMBER() OVER ( PARTITION BY d.id ORDER BY salary DESC ) AS
salary_rank
FROM department d
JOIN employee e ON d.id = e.department_id
ORDER BY department_name;

SQL Query change row values

Consider i have 2 employers(Google,Yahoo) and google is having 3 employee's and yahoo is 2 employee
My query should add
1000 to employee1 for google company
2000 to employee2 for google company
3000 to employee3 for google company
1000 to employee1 for Yahoo company
2000 to employee2 for Yahoo company
For each set of employees it should adding salary starting from 1000
You can use ROW_NUMBER() to calculate the new Salary:
SELECT Company,
Emp,
ROW_NUMBER() OVER(PARTITION BY Company ORDER BY Emp) * 1000 + Salary As NewSalary
FROM YourTable
If you want to actually update the salary, one way is to use that query as a CTE:
;WITH CTE AS (
SELECT Company,
Emp,
ROW_NUMBER() OVER(PARTITION BY Company ORDER BY Emp) * 1000 + Salary As NewSalary
FROM YourTable
)
UPDATE T
SET Salary = NewSalary
FROM YourTable T
INNER JOIN CTE C ON(T.Company = C.Company AND T.Emp = C.Emp)

Third highest salary department wise without using rank or other functions

I want to find third highest salary department wise without using any functions such as rank or row_number or dense_rank in sql
My logic
select max(salary),deptno from emp
where salary not in
(select max(salary) from emp where salary not in
(select max(salary) from emp group by deptno)group by deptno)
group by deptno
This is my query.syntax is correct but it is not giving wright answer/result
This should do the job
select salary, deptno from emp e where 2 =
(select count(distinct salary) from emp where
salary > e.salary and deptno = e.depto)
Using MSSQL,
SELECT MAX(salary) as '3rdHighest', deptno
FROM EMP n WHERE 2 =
(SELECT COUNT(DISTINCT salary) FROM NewTbl
WHERE salary > n. salary and deptno = n.deptno)
GROUP BY deptno

T-Sql query for the situation - Need a departmentID with max number of employees?

let's say employee table has employee details and deptId of the employee.
to get the number of employees in each deptid,
select deptId, COUNT(*) from employee group by deptId;
question is: to get the deptId having max number of employees of the above result set,
select Top 1 deptId, COUNT(*) from employee group by deptId order by 2 desc
(2-ref to second column in the query list) - will do.. but
Is there anyway to avoid ordering this set? or better way of writing this sql,
thanks
If you just want the MAX number of employees within a department, you can do this:
SELECT TOP 1 DepartmentID,
COUNT(EmployeeID)
FROM EmployeeTable
GROUP BY DepartmentID
ORDER BY COUNT(EmployeeID) DESC
Without any ordering, it is hard, but try
Select deptId, cnt
From (Select deptId, count(*) cnt
from employee
Group By deptId) Z
Where cnt = (Select Max(cnt)
From (Select deptId, count(*) cnt
From employee
Group By deptId) ZZ)

Resources