SQL Joins SQL Topic exerciseHardVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

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_idINTEGER
  • first_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_idemployee1_first_nameemployee1_last_nameemployee2_idemployee2_first_nameemployee2_last_namejob_title
104DavidBrown109IvyMartinezFinancial Analyst
103CarolDavis108HenryClarkHR Representative
101AliceJohnson102BobWilsonSoftware Developer
101AliceJohnson107GraceWhiteSoftware Developer
102BobWilson107GraceWhiteSoftware Developer

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.