|
1 |
| ---185. Department Top Three Salaries |
2 |
| --- The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id. |
3 |
| --- |
4 |
| ---+----+-------+--------+--------------+ |
5 |
| ---| Id | Name | Salary | DepartmentId | |
6 |
| ---+----+-------+--------+--------------+ |
7 |
| ---| 1 | Joe | 70000 | 1 | |
8 |
| ---| 2 | Henry | 80000 | 2 | |
9 |
| ---| 3 | Sam | 60000 | 2 | |
10 |
| ---| 4 | Max | 90000 | 1 | |
11 |
| ---| 5 | Janet | 69000 | 1 | |
12 |
| ---| 6 | Randy | 85000 | 1 | |
13 |
| ---+----+-------+--------+--------------+ |
14 |
| --- |
15 |
| ---The Department table holds all departments of the company. |
16 |
| --- |
17 |
| ---+----+----------+ |
18 |
| ---| Id | Name | |
19 |
| ---+----+----------+ |
20 |
| ---| 1 | IT | |
21 |
| ---| 2 | Sales | |
22 |
| ---+----+----------+ |
23 |
| --- |
24 |
| ---Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows. |
25 |
| --- |
26 |
| ---+------------+----------+--------+ |
27 |
| ---| Department | Employee | Salary | |
28 |
| ---+------------+----------+--------+ |
29 |
| ---| IT | Max | 90000 | |
30 |
| ---| IT | Randy | 85000 | |
31 |
| ---| IT | Joe | 70000 | |
32 |
| ---| Sales | Henry | 80000 | |
33 |
| ---| Sales | Sam | 60000 | |
34 |
| ---+------------+----------+--------+ |
35 |
| --- |
36 |
| - |
37 | 1 | select D.Name as Department, E.Name as Employee, E.Salary as Salary
|
38 | 2 | from Department D, Employee E
|
39 | 3 | where (select(count(distinct(salary))) from Employee
|
|
0 commit comments