SELECT Salary, Department, COUNT(*) AS DuplicateCount
FROM Employees
GROUP BY Salary, Department
HAVING COUNT(*) > 1;
Explanation:
GROUP BY Salary, Departmentensures that duplicates are checked within each department.COUNT(*)counts occurrences of each (Salary, Department) combination.HAVING COUNT(*) > 1filters out unique salaries, showing only duplicates.
Example:
Given Employees Table:
| EmployeeID | Name | Salary | Department |
|---|---|---|---|
| 1 | John | 5000 | IT |
| 2 | Alice | 6000 | HR |
| 3 | Mark | 5000 | IT |
| 4 | David | 7000 | Sales |
| 5 | Steve | 6000 | HR |
| 6 | Bob | 5000 | HR |
Query Output:
| Salary | Department | DuplicateCount |
|---|---|---|
| 5000 | IT | 2 |
| 6000 | HR | 2 |
This means that:
- Salary 5000 appears twice in the IT department.
- Salary 6000 appears twice in the HR department.
No comments:
Post a Comment