Skip to content

Commit 2a4cf2e

Browse files
authored
LC hard problem ~ DepartmentTop3Salaries.sql
1 parent bec1c41 commit 2a4cf2e

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
-- Table: Employee
2+
3+
-- +--------------+---------+
4+
-- | Column Name | Type |
5+
-- +--------------+---------+
6+
-- | id | int |
7+
-- | name | varchar |
8+
-- | salary | int |
9+
-- | departmentId | int |
10+
-- +--------------+---------+
11+
-- id is the primary key (column with unique values) for this table.
12+
-- departmentId is a foreign key (reference column) of the ID from the Department table.
13+
-- Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department.
14+
15+
-- Table: Department
16+
17+
-- +-------------+---------+
18+
-- | Column Name | Type |
19+
-- +-------------+---------+
20+
-- | id | int |
21+
-- | name | varchar |
22+
-- +-------------+---------+
23+
-- id is the primary key (column with unique values) for this table.
24+
-- Each row of this table indicates the ID of a department and its name.
25+
26+
-- A company's executives are interested in seeing who earns the most money in each of the company's departments. A high earner in a department is an employee who has a salary in the top three unique salaries for that department.
27+
28+
-- Write a solution to find the employees who are high earners in each of the departments.
29+
30+
-- Return the result table in any order.
31+
32+
-- The result format is in the following example.
33+
34+
-- Example 1:
35+
36+
-- Input:
37+
-- Employee table:
38+
-- +----+-------+--------+--------------+
39+
-- | id | name | salary | departmentId |
40+
-- +----+-------+--------+--------------+
41+
-- | 1 | Joe | 85000 | 1 |
42+
-- | 2 | Henry | 80000 | 2 |
43+
-- | 3 | Sam | 60000 | 2 |
44+
-- | 4 | Max | 90000 | 1 |
45+
-- | 5 | Janet | 69000 | 1 |
46+
-- | 6 | Randy | 85000 | 1 |
47+
-- | 7 | Will | 70000 | 1 |
48+
-- +----+-------+--------+--------------+
49+
-- Department table:
50+
-- +----+-------+
51+
-- | id | name |
52+
-- +----+-------+
53+
-- | 1 | IT |
54+
-- | 2 | Sales |
55+
-- +----+-------+
56+
-- Output:
57+
-- +------------+----------+--------+
58+
-- | Department | Employee | Salary |
59+
-- +------------+----------+--------+
60+
-- | IT | Max | 90000 |
61+
-- | IT | Joe | 85000 |
62+
-- | IT | Randy | 85000 |
63+
-- | IT | Will | 70000 |
64+
-- | Sales | Henry | 80000 |
65+
-- | Sales | Sam | 60000 |
66+
-- +------------+----------+--------+
67+
-- Explanation:
68+
-- In the IT department:
69+
-- - Max earns the highest unique salary
70+
-- - Both Randy and Joe earn the second-highest unique salary
71+
-- - Will earns the third-highest unique salary
72+
73+
-- In the Sales department:
74+
-- - Henry earns the highest salary
75+
-- - Sam earns the second-highest salary
76+
-- - There is no third-highest salary as there are only two employees
77+
78+
-- Write your PostgreSQL query statement below
79+
-- Solution
80+
with rank_salaries_each_dep as (
81+
select employee.id as emp_id,
82+
employee.name as emp_name,
83+
department.id as dep_id,
84+
department.name as dep_name,
85+
employee.salary as employee_salary,
86+
dense_rank() over (partition by department.id,department.name
87+
order by employee.salary desc) rank
88+
from department inner join employee
89+
on department.id = employee.departmentId
90+
)
91+
select dep_name as Department,
92+
emp_name as Employee,
93+
employee_salary as Salary
94+
from rank_salaries_each_dep where rank in (1,2,3);

0 commit comments

Comments
 (0)