|
| 1 | +# [1875. Group Employees of the Same Salary](https://leetcode.com/problems/group-employees-of-the-same-salary) |
| 2 | + |
| 3 | +[中文文档](/solution/1800-1899/1875.Group%20Employees%20of%20the%20Same%20Salary/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>Table: <code>Employees</code></p> |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +<pre> |
| 12 | + |
| 13 | ++-------------+---------+ |
| 14 | + |
| 15 | +| Column Name | Type | |
| 16 | + |
| 17 | ++-------------+---------+ |
| 18 | + |
| 19 | +| employee_id | int | |
| 20 | + |
| 21 | +| name | varchar | |
| 22 | + |
| 23 | +| salary | int | |
| 24 | + |
| 25 | ++-------------+---------+ |
| 26 | + |
| 27 | +employee_id is the primary key for this table. |
| 28 | + |
| 29 | +Each row of this table indicates the employee ID, employee name, and salary. |
| 30 | + |
| 31 | +</pre> |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +<p> </p> |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +<p>A company wants to divide the employees into teams such that all the members on each team have the <strong>same salary</strong>. The teams should follow these criteria:</p> |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +<ul> |
| 44 | + <li>Each team should consist of <strong>at least two</strong> employees.</li> |
| 45 | + <li>All the employees on a team should have the <strong>same salary</strong>.</li> |
| 46 | + <li>All the employees of the same salary should be assigned to the same team.</li> |
| 47 | + <li>If the salary of an employee is unique, we <strong>do not</strong> assign this employee to any team.</li> |
| 48 | + <li>A team's ID is assigned based on the <strong>rank of the team's salary</strong> relative to the other teams' salaries, where the team with the <strong>lowest</strong> salary has <code>team_id = 1</code>. Note that the salaries for employees not on a team are <strong>not included</strong> in this ranking.</li> |
| 49 | +</ul> |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +<p>Write an SQL query to get the <code>team_id</code> of each employee that is in a team.</p> |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +<p>Return the result table ordered by <code>team_id</code> <strong>in ascending order</strong>. In case of a tie, order it by <code>employee_id</code> in <strong>ascending order</strong>.</p> |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +<p>The query result format is in the following example:</p> |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +<p> </p> |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +<pre> |
| 70 | + |
| 71 | +Employees table: |
| 72 | + |
| 73 | ++-------------+---------+--------+ |
| 74 | + |
| 75 | +| employee_id | name | salary | |
| 76 | + |
| 77 | ++-------------+---------+--------+ |
| 78 | + |
| 79 | +| 2 | Meir | 3000 | |
| 80 | + |
| 81 | +| 3 | Michael | 3000 | |
| 82 | + |
| 83 | +| 7 | Addilyn | 7400 | |
| 84 | + |
| 85 | +| 8 | Juan | 6100 | |
| 86 | + |
| 87 | +| 9 | Kannon | 7400 | |
| 88 | + |
| 89 | ++-------------+---------+--------+ |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +Result table: |
| 94 | + |
| 95 | ++-------------+---------+--------+---------+ |
| 96 | + |
| 97 | +| employee_id | name | salary | team_id | |
| 98 | + |
| 99 | ++-------------+---------+--------+---------+ |
| 100 | + |
| 101 | +| 2 | Meir | 3000 | 1 | |
| 102 | + |
| 103 | +| 3 | Michael | 3000 | 1 | |
| 104 | + |
| 105 | +| 7 | Addilyn | 7400 | 2 | |
| 106 | + |
| 107 | +| 9 | Kannon | 7400 | 2 | |
| 108 | + |
| 109 | ++-------------+---------+--------+---------+ |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +Meir (employee_id=2) and Michael (employee_id=3) are in the same team because they have the same salary of 3000. |
| 114 | + |
| 115 | +Addilyn (employee_id=7) and Kannon (employee_id=9) are in the same team because they have the same salary of 7400. |
| 116 | + |
| 117 | +Juan (employee_id=8) is not included in any team because their salary of 6100 is unique (i.e. no other employee has the same salary). |
| 118 | + |
| 119 | +The team IDs are assigned as follows (based on salary ranking, lowest first): |
| 120 | + |
| 121 | +- team_id=1: Meir and Michael, salary of 3000 |
| 122 | + |
| 123 | +- team_id=2: Addilyn and Kannon, salary of 7400 |
| 124 | + |
| 125 | +Juan's salary of 6100 is not included in the ranking because they are not on a team.</pre> |
| 126 | + |
| 127 | +## Solutions |
| 128 | + |
| 129 | +<!-- tabs:start --> |
| 130 | + |
| 131 | +### **SQL** |
| 132 | + |
| 133 | +```sql |
| 134 | + |
| 135 | +``` |
| 136 | + |
| 137 | +<!-- tabs:end --> |
0 commit comments