|
| 1 | +<p>Table: <code>Employee</code></p> |
| 2 | + |
| 3 | +<pre> |
| 4 | ++--------------+---------+ |
| 5 | +| Column Name | Type | |
| 6 | ++--------------+---------+ |
| 7 | +| id | int | |
| 8 | +| name | varchar | |
| 9 | +| salary | int | |
| 10 | +| departmentId | int | |
| 11 | ++--------------+---------+ |
| 12 | +id is the primary key (column with unique values) for this table. |
| 13 | +departmentId is a foreign key (reference columns) of the ID from the <code>Department </code>table. |
| 14 | +Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department. |
| 15 | +</pre> |
| 16 | + |
| 17 | +<p> </p> |
| 18 | + |
| 19 | +<p>Table: <code>Department</code></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | ++-------------+---------+ |
| 23 | +| Column Name | Type | |
| 24 | ++-------------+---------+ |
| 25 | +| id | int | |
| 26 | +| name | varchar | |
| 27 | ++-------------+---------+ |
| 28 | +id is the primary key (column with unique values) for this table. It is guaranteed that department name is not <code>NULL.</code> |
| 29 | +Each row of this table indicates the ID of a department and its name. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | + |
| 34 | +<p>Write a solution to find employees who have the highest salary in each of the departments.</p> |
| 35 | + |
| 36 | +<p>Return the result table in <strong>any order</strong>.</p> |
| 37 | + |
| 38 | +<p>The result format is in the following example.</p> |
| 39 | + |
| 40 | +<p> </p> |
| 41 | +<p><strong class="example">Example 1:</strong></p> |
| 42 | + |
| 43 | +<pre> |
| 44 | +<strong>Input:</strong> |
| 45 | +Employee table: |
| 46 | ++----+-------+--------+--------------+ |
| 47 | +| id | name | salary | departmentId | |
| 48 | ++----+-------+--------+--------------+ |
| 49 | +| 1 | Joe | 70000 | 1 | |
| 50 | +| 2 | Jim | 90000 | 1 | |
| 51 | +| 3 | Henry | 80000 | 2 | |
| 52 | +| 4 | Sam | 60000 | 2 | |
| 53 | +| 5 | Max | 90000 | 1 | |
| 54 | ++----+-------+--------+--------------+ |
| 55 | +Department table: |
| 56 | ++----+-------+ |
| 57 | +| id | name | |
| 58 | ++----+-------+ |
| 59 | +| 1 | IT | |
| 60 | +| 2 | Sales | |
| 61 | ++----+-------+ |
| 62 | +<strong>Output:</strong> |
| 63 | ++------------+----------+--------+ |
| 64 | +| Department | Employee | Salary | |
| 65 | ++------------+----------+--------+ |
| 66 | +| IT | Jim | 90000 | |
| 67 | +| Sales | Henry | 80000 | |
| 68 | +| IT | Max | 90000 | |
| 69 | ++------------+----------+--------+ |
| 70 | +<strong>Explanation:</strong> Max and Jim both have the highest salary in the IT department and Henry has the highest salary in the Sales department. |
| 71 | +</pre> |
0 commit comments