Skip to content

2988. Manager of the Largest Department 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
WITH
  RankedDepartments AS (
    SELECT
      dep_id,
      DENSE_RANK() OVER(
        ORDER BY COUNT(*) DESC
      ) AS `rank`
    FROM Employees
    GROUP BY 1
  )
SELECT
  Employees.emp_name AS manager_name,
  Employees.dep_id
FROM Employees
INNER JOIN RankedDepartments
  USING (dep_id)
WHERE
  Employees.position = 'Manager'
  AND RankedDepartments.`rank` = 1
ORDER BY dep_id;