Employees with the Same Salary (MySQL)
In this lesson: SQL query to find employees having the same salary using self join, GROUP BY HAVING and IN — with output. In Hindi & English.
The task
A common query — and interview question — is "find employees who have the same salary". This means finding salary values that appear more than once, and listing the people who share them. The tools are GROUP BY with HAVING, and optionally a self-join to pair them.
Which salaries repeat
SELECT salary, COUNT(*) AS how_many FROM employees GROUP BY salary HAVING COUNT(*) > 1;
GROUP BY groups equal salaries, and HAVING COUNT(*) > 1 keeps only those shared by more than one employee.
Listing the employees
SELECT name, salary
FROM employees
WHERE salary IN (
SELECT salary FROM employees
GROUP BY salary HAVING COUNT(*) > 1
);
The subquery finds the shared salaries, and the outer query lists every employee whose salary is in that set. Now you see the actual people, not just the salary values.
Pairing with a self-join
SELECT a.name AS emp1, b.name AS emp2, a.salary FROM employees a JOIN employees b ON a.salary = b.salary AND a.id < b.id;
A self-join matches employees on equal salary. The condition a.id < b.id shows each pair once and stops an employee pairing with themselves.
A general pattern
This same approach finds any "duplicate value" case: customers in the same city, products at the same price, students with the same marks. Swap the column, and the GROUP BY / HAVING and self-join patterns work exactly the same way.
How many share each
SELECT salary, COUNT(*) AS employees FROM employees GROUP BY salary HAVING COUNT(*) > 1 ORDER BY employees DESC;
Adding ORDER BY on the count shows the most-shared salaries first — useful for spotting the biggest clusters at a glance.
Common mistakes
- Using WHERE for COUNT(*). Use HAVING after GROUP BY.
- Self-join without
a.id < b.id. You get duplicate and self-pairs. - Forgetting the subquery. You need it to list people, not just salaries.
- Grouping by the wrong column. Group by the value you are matching on.
Summary
- Find repeated salaries with GROUP BY salary HAVING COUNT(*) > 1.
- List the people with a subquery: WHERE salary IN (that group).
- Pair them with a self-join on equal salary and
a.id < b.id. - The same pattern finds any shared-value case.
- Use HAVING, not WHERE, to filter on COUNT(*).
Next, group data in GROUP BY and HAVING, or find repeats in finding duplicate rows.
Frequently Asked Questions
How do I find employees with the same salary in MySQL?
How do I list the salaries that appear more than once?
SELECT salary FROM employees GROUP BY salary HAVING COUNT(*) > 1;. GROUP BY collapses equal salaries into groups, and HAVING keeps only those groups with more than one row.How do I get the names of employees sharing a salary?
SELECT name, salary FROM employees WHERE salary IN (SELECT salary FROM employees GROUP BY salary HAVING COUNT(*) > 1);. This lists each employee whose salary is shared.How do I pair up employees with the same salary?
SELECT a.name, b.name FROM employees a JOIN employees b ON a.salary = b.salary AND a.id < b.id;. The a.id < b.id avoids repeating pairs and self-matches.