MediumNo. 1875LeetCode Premium
Group Employees of the Same Salary — LeetCode 1875 Python Solution
- Topics
- Database
- Reading time
- 4 min
- Source
- leetcode.com
Table schema
Table: Employees +-------------+---------+ | Column Name | Type | +-------------+---------+ | employee_id | int | | name | varchar | | salary | int | +-------------+---------+ employee_id is the column with unique values for this table. Each row of this table indicates the employee ID, employee name, and salary.Example
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| employee_id | int |
| name | varchar |
| salary | int |
+-------------+---------+
employee_id is the column with unique values for this table.
Each row of this table indicates the employee ID, employee name, and salary.Python solution
import duckdb
import pandas as pd
def solution(employees: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Employees", employees)
return con.execute("""WITH
S AS (
SELECT salary
FROM Employees
GROUP BY salary
HAVING COUNT(1) > 1
),
T AS (
SELECT salary, ROW_NUMBER() OVER (ORDER BY salary) AS team_id
FROM S
)
SELECT e.*, t.team_id
FROM
Employees AS e
JOIN T AS t ON e.salary = t.salary
ORDER BY 4, 1;""").df()Complexity
- Time
- O(n log n)(typical)
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0175 | Combine Two Tables | Easy |
| 0176 | Second Highest Salary | Medium |
| 0177 | Nth Highest Salary | Medium |
| 0178 | Rank Scores | Medium |
| 0180 | Consecutive Numbers | Medium |
| 0181 | Employees Earning More Than Their Managers | Easy |
Frequently asked questions
- How hard is LeetCode 1875. Group Employees of the Same Salary?
LeetCode 1875. Group Employees of the Same Salary is rated Medium on LeetCode.
- What topics does LeetCode 1875. Group Employees of the Same Salary cover?
LeetCode 1875. Group Employees of the Same Salary is tagged Database on LeetCode.
- Is LeetCode 1875. Group Employees of the Same Salary a premium problem?
Yes. LeetCode 1875. Group Employees of the Same Salary is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.