๐ง Advanced SQL Interview Question โก
๐ Find pairs of employees who work in the same department and earn the same salary
Table: Employees
Columns:
employee_id, employee_name, department_id, salary
๐ Query:
SELECT e1.employee_id AS emp1_id,
e1.employee_name AS emp1_name,
e2.employee_id AS emp2_id,
e2.employee_name AS emp2_name,
e1.department_id,
e1.salary
FROM Employees e1
JOIN Employees e2
ON e1.department_id = e2.department_id
AND e1.salary = e2.salary
AND e1.employee_id < e2.employee_id;
๐ฏ Why this question matters:
โ
Tests self joins deeply
โ
Evaluates logical thinking in SQL
โ
Commonly asked in advanced interview rounds
๐ Pro Tip:
Self joins are extremely useful for comparing rows within the same table without using loops.
๐ฅ React โค๏ธ for more advanced SQL interview questions ๐
Post #2193
2.01K
- โค 6