Pairs of Employees with the Same Job Title
Return every unique same-job employee pair with its job title.
- Joins
- Sorting
Exercise brief
Understand the request
Talent mobility analyst A peer programme needs each pair of employees sharing a job, without mirrored or self-pairs.
Find every UNIQUE pair of employees who share the same job title (each pair listed once, not twice). Return employee1_id, employee1_first_name, employee1_last_name, employee2_id, employee2_first_name, employee2_last_name, job_title — ordered by job_title, employee1_id, employee2_id.
Return
- Return employee1_id, employee1_first_name, employee1_last_name, employee2_id, employee2_first_name, employee2_last_name, job_title in this exact left-to-right order.
Constraints
- Self-join employees on job_id.
- Use employee1_id < employee2_id to eliminate self-pairs and mirrored duplicates.
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
employees
employee_idINTEGERfirst_nameVARCHAR(50)last_nameVARCHAR(50)job_idVARCHAR(20)
jobs
job_idVARCHAR(20)job_titleVARCHAR(100)
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
Without the < filter the self-join produces both (A,B) AND (B,A) for every pair, plus self-pairs (A,A).
Hint 2
Putting `e1.employee_id < e2.employee_id` IN THE ON clause is fine — most engines push it down anyway.
Hint 3
Three jobs (IT_PROG, HR_REP, FIN_ANALYST) have at least 2 employees → 3+1+1 = 5 unique pairs.
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
SELECT e1.employee_id AS employee1_id, e1.first_name AS employee1_first_name, e1.last_name AS employee1_last_name, e2.employee_id AS employee2_id, e2.first_name AS employee2_first_name, e2.last_name AS employee2_last_name, j.job_title FROM employees e1 INNER JOIN employees e2 ON e1.job_id = e2.job_id AND e1.employee_id < e2.employee_id INNER JOIN jobs j ON e1.job_id = j.job_id ORDER BY j.job_title, e1.employee_id, e2.employee_id;Why this works
The 'unique pairs' technique (`a.id < b.id`) is essential whenever you need cartesian-style comparisons within a table — duplicates, neighbours, similarity searches, etc. — without double-counting.
Success check
Every valid peer pair appears exactly once and no employee is paired with themself.
Expected result
Use this output to verify values, aliases, ordering, and row count.
| employee1_id | employee1_first_name | employee1_last_name | employee2_id | employee2_first_name | employee2_last_name | job_title |
|---|---|---|---|---|---|---|
| 104 | David | Brown | 109 | Ivy | Martinez | Financial Analyst |
| 103 | Carol | Davis | 108 | Henry | Clark | HR Representative |
| 101 | Alice | Johnson | 102 | Bob | Wilson | Software Developer |
| 101 | Alice | Johnson | 107 | Grace | White | Software Developer |
| 102 | Bob | Wilson | 107 | Grace | White | Software Developer |
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics:
Continue practicing
Build the next SQL skill
Self Joins & Hierarchical Queries
Query organization charts, trees, and parent-child relationships.
SQL Subqueries
Practice scalar, derived-table, correlated, EXISTS, NULL-safe anti-subquery, quantified, and row-subquery patterns.
SQL Aggregations
Build reliable SQL metrics from aggregate functions through grain, fan-out, weighted ratios, rollups, percentiles, and approximate counts.
Open the interactive workspace and practice across SQL topics.