The Group Employees of the Same Salary coding problem is a SQL task. You are given an Employees table with employee_id, name, and salary. You need to find all employees who share the exact same salary with at least one other employee. The result should be ordered by salary (ascending), and then by employee_id (ascending).
This Database interview pattern problem is used to test your ability to filter datasets based on aggregate properties. It evaluates whether you can use subqueries, window functions, or GROUP BY with HAVING to identify duplicate values in a specific column and then retrieve the full row details for those duplicates.
There are two primary SQL patterns for this:
SELECT salary FROM Employees GROUP BY salary HAVING COUNT(*) > 1.SELECT * FROM Employees WHERE salary IN (...).COUNT(employee_id) OVER(PARTITION BY salary) as sal_count.sal_count > 1.Employees:
SELECT employee_id, name, salary FROM Employees GROUP BY salary HAVING COUNT(*) > 1. This is invalid SQL because employee_id and name are not aggregated and aren't in the GROUP BY clause.ORDER BY salary ASC, employee_id ASC).The WHERE column IN (SELECT column FROM table GROUP BY column HAVING COUNT(*) > 1) pattern is the most universal and readable way to solve "find all rows that have duplicate values in column X." Memorize this construct.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Active Businesses | Medium | Solve | |
| Active Users | Medium | Solve | |
| Activity Participants | Medium | Solve | |
| All People Report to the Given Manager | Medium | Solve | |
| All the Pairs With the Maximum Number of Common Followers | Medium | Solve |