|
| 1 | +/* |
| 2 | +
|
| 3 | +Problem Link -> https://leetcode.com/problems/department-top-three-salaries/description/?envType=study-plan-v2&envId=top-sql-50 |
| 4 | +
|
| 5 | +------------------------------------------------------------- QUESTION ----------------------------------------------------------- |
| 6 | +
|
| 7 | +Table: Employee |
| 8 | +
|
| 9 | ++--------------+---------+ |
| 10 | +| Column Name | Type | |
| 11 | ++--------------+---------+ |
| 12 | +| id | int | |
| 13 | +| name | varchar | |
| 14 | +| salary | int | |
| 15 | +| departmentId | int | |
| 16 | ++--------------+---------+ |
| 17 | +id is the primary key (column with unique values) for this table. |
| 18 | +departmentId is a foreign key (reference column) of the ID from the Department table. |
| 19 | +Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department. |
| 20 | + |
| 21 | +
|
| 22 | +Table: Department |
| 23 | +
|
| 24 | ++-------------+---------+ |
| 25 | +| Column Name | Type | |
| 26 | ++-------------+---------+ |
| 27 | +| id | int | |
| 28 | +| name | varchar | |
| 29 | ++-------------+---------+ |
| 30 | +id is the primary key (column with unique values) for this table. |
| 31 | +Each row of this table indicates the ID of a department and its name. |
| 32 | + |
| 33 | +
|
| 34 | +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. |
| 35 | +Write a solution to find the employees who are high earners in each of the departments. |
| 36 | +Return the result table in any order. |
| 37 | +The result format is in the following example. |
| 38 | + |
| 39 | +
|
| 40 | +Example 1: |
| 41 | +
|
| 42 | +Input: |
| 43 | +Employee table: |
| 44 | ++----+-------+--------+--------------+ |
| 45 | +| id | name | salary | departmentId | |
| 46 | ++----+-------+--------+--------------+ |
| 47 | +| 1 | Joe | 85000 | 1 | |
| 48 | +| 2 | Henry | 80000 | 2 | |
| 49 | +| 3 | Sam | 60000 | 2 | |
| 50 | +| 4 | Max | 90000 | 1 | |
| 51 | +| 5 | Janet | 69000 | 1 | |
| 52 | +| 6 | Randy | 85000 | 1 | |
| 53 | +| 7 | Will | 70000 | 1 | |
| 54 | ++----+-------+--------+--------------+ |
| 55 | +
|
| 56 | +Department table: |
| 57 | ++----+-------+ |
| 58 | +| id | name | |
| 59 | ++----+-------+ |
| 60 | +| 1 | IT | |
| 61 | +| 2 | Sales | |
| 62 | ++----+-------+ |
| 63 | +
|
| 64 | +Output: |
| 65 | ++------------+----------+--------+ |
| 66 | +| Department | Employee | Salary | |
| 67 | ++------------+----------+--------+ |
| 68 | +| IT | Max | 90000 | |
| 69 | +| IT | Joe | 85000 | |
| 70 | +| IT | Randy | 85000 | |
| 71 | +| IT | Will | 70000 | |
| 72 | +| Sales | Henry | 80000 | |
| 73 | +| Sales | Sam | 60000 | |
| 74 | ++------------+----------+--------+ |
| 75 | +
|
| 76 | +Explanation: |
| 77 | +In the IT department: |
| 78 | +- Max earns the highest unique salary |
| 79 | +- Both Randy and Joe earn the second-highest unique salary |
| 80 | +- Will earns the third-highest unique salary |
| 81 | +
|
| 82 | +In the Sales department: |
| 83 | +- Henry earns the highest salary |
| 84 | +- Sam earns the second-highest salary |
| 85 | +- There is no third-highest salary as there are only two employees |
| 86 | +
|
| 87 | +*/ |
| 88 | + |
| 89 | +-- ----------------------------------------------------------- SOLUTION ----------------------------------------------------------- |
| 90 | + |
| 91 | +SELECT |
| 92 | + D.name AS Department, |
| 93 | + E.name AS Employee, |
| 94 | + E.salary AS Salary |
| 95 | +FROM Employee E INNER JOIN Department D |
| 96 | +ON E.departmentId = D.id |
| 97 | +WHERE 3 > ( |
| 98 | + SELECT COUNT(DISTINCT(emp.salary)) |
| 99 | + FROM Employee emp |
| 100 | + WHERE emp.salary > E.salary |
| 101 | + AND E.departmentId = emp.departmentId |
| 102 | +) |
| 103 | + |
0 commit comments