Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 142

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.

💡 Find repeated salaries with GROUP BY ... HAVING COUNT(*) > 1, then list the matching employees.

Which salaries repeat

SELECT salary, COUNT(*) AS how_many
FROM employees
GROUP BY salary
HAVING COUNT(*) > 1;
+--------+----------+ | salary | how_many | +--------+----------+ | 50000 | 3 | | 60000 | 2 | +--------+----------+

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;
+------+------+--------+ | emp1 | emp2 | salary | +------+------+--------+ | Aman | Ravi | 50000 | +------+------+--------+

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?
First find which salaries repeat using GROUP BY salary HAVING COUNT(*) > 1, then select employees whose salary is in that list. This shows all employees sharing a salary with at least one other person.
How do I list the salaries that appear more than once?
Use 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?
Put the group query in a subquery and filter: 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?
Use a self-join on equal salary and different id: 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.
Why use HAVING instead of WHERE here?
WHERE filters individual rows before grouping, but COUNT(*) is only known after grouping. HAVING filters on the aggregate after GROUP BY, so it is the correct place to test COUNT(*) > 1.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicLogin Authentication Query Your progress: 142 / 151 (94%) · Lesson 143 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free