Find Pairs of Employees in the Same Department
Find pairs of employees who work in the same department. Exclude self-pairs and avoid duplicate (A,B)/(B,A) pairs by enforcing employee1_id < employee2_id. Skip rows where department_id IS NULL. Return employee1_name, employee2_name, department_id — ordered by department_id, employee1_name, employee2_name.
- Joins
- NULL handling
- Filtering
- Sorting
Exercise brief
Understand the request
Workforce collaboration analyst A peer-matching export needs unique pairs within each department without mirrored or self-pairs.
Return
- Return both names and department_id.
- Order by department and both names.
Constraints
- Self-join on department_id.
- Enforce employee1_id < employee2_id.
- Exclude NULL department keys.
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
employees
employee_idINTEGERemployee_nameTEXTdepartment_idINTEGER
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
Pair-finding self-join: ON shared_attr = shared_attr AND id1 < id2.
Hint 2
The strict-less-than (<) does double duty: drops self-pairs and dedupes (A,B)/(B,A).
Hint 3
NULL-safe: filter `department_id IS NOT NULL` because NULL = NULL is unknown.
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
SELECT e1.employee_name AS employee1_name, e2.employee_name AS employee2_name, e1.department_id FROM employees e1 INNER JOIN employees e2 ON e1.department_id = e2.department_id AND e1.employee_id < e2.employee_id WHERE e1.department_id IS NOT NULL ORDER BY e1.department_id, e1.employee_name, e2.employee_name;Why this works
The id1 < id2 trick is the canonical way to enumerate unordered pairs in SQL. Without it you would get N×N rows including self-pairs and mirror duplicates.
Success check
Every unordered same-department pair appears once and no employee is paired with themself.
Expected result
Use this output to verify values, aliases, ordering, and row count.
| employee1_name | employee2_name | department_id |
|---|---|---|
| Bob VP Sales | Emma Sales Mgr | 2 |
| Bob VP Sales | Henry Sales Rep | 2 |
| Bob VP Sales | Ivy Sales Rep | 2 |
| Bob VP Sales | Mia Overpaid Jr | 2 |
| Bob VP Sales | Olivia Sales Rep | 2 |
| Emma Sales Mgr | Henry Sales Rep | 2 |
| Emma Sales Mgr | Ivy Sales Rep | 2 |
| Emma Sales Mgr | Mia Overpaid Jr | 2 |
| Emma Sales Mgr | Olivia Sales Rep | 2 |
| Henry Sales Rep | Ivy Sales Rep | 2 |
Previewing 10 of 28 expected rows. Run the query in the editor to inspect the full result.
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics:
Continue practicing
Build the next SQL skill
SQL Joins
Practice reliable INNER, LEFT, FULL, CROSS, self, semi, anti, range, temporal, and many-to-many join patterns.
CTEs & Window Functions
Practice modular CTE pipelines, deterministic window analytics, period comparisons, deduplication, frames, and gaps-and-islands.
SQL Subqueries
Practice scalar, derived-table, correlated, EXISTS, NULL-safe anti-subquery, quantified, and row-subquery patterns.
Open the interactive workspace and practice across SQL topics.