MediumSQL challenge #146JOIN

Find Pairs of Employees in the Same Department

Return every pair of employees who work in the same department.

The challenge runs on the SQLabHub practice database (SQLite) and uses the employees table. You can solve it in the browser, with nothing to install.

Solve This SQL Challenge

Why this question comes up

HR is setting up a peer-mentoring scheme inside each department and wants every possible pair of colleagues listed once, as a starting point for matching.

The task

Find pairs of employees in same department.

A correct answer returns 683 rows with 2 columns: first_name, first_name.

Tables used in this challenge

employees 120 rows

id INTEGER (PK) · first_name TEXT · last_name TEXT · email TEXT · department TEXT · department_id INTEGER · title TEXT · salary REAL · hire_date TEXT · manager_id INTEGER · city TEXT · status TEXT

First 3 rows of the employees table
idfirst_namelast_nameemaildepartmentdepartment_idtitlesalaryhire_datemanager_idcitystatus
1WilliamLewiswlewis18@corp.ioCustomer Success7CS Specialist617742022-07-2460San Diegoactive
2JohnMillerjmiller51@corp.ioLegal8Senior Counsel1916942021-08-0955San Diegoactive
3DanielRiveradrivera50@corp.ioEngineering1Staff Engineer1350672016-06-06106Houstonactive

The first rows of each table are shown. See how all the tables relate on the database schema page.

Expected output

Expected output of the challenge
first_namefirst_name
WilliamAmy
WilliamBarbara
WilliamCharles
WilliamDorothy
WilliamEdward

First 5 of 683 rows. This is the output of the model solution on the practice database. The task does not fix a sort order, so your rows may come back in a different order.

What you practise

  • Choosing exactly which columns a query returns
  • Combining rows from two tables on a matching key with an inner join
  • Joining a table to itself under two aliases

Hint and approach

Try the challenge first. Open these only when you need them.

Show the hint

Self join

Show the step-by-step approach

How the model solution is built, in the order the database evaluates it:

  1. Read from the employees table, under the alias e1.
  2. Join employees again, under the alias e2, on e1.department = e2.department, and only where e1.id is less than e2.id.
  3. Return only the columns e1.first_name and e2.first_name.
Show the model solution
SELECT e1.first_name, e2.first_name
FROM employees e1
JOIN employees e2 ON e1.department = e2.department AND e1.id < e2.id;

This query produced the expected output above. Yours does not have to match it word for word: any query that returns the same rows is correct.

What to watch for

The condition e1.id < e2.id does two jobs: it stops an employee being paired with themselves and it keeps only one of (A, B) and (B, A). Using <> instead would list each pair twice.

  • Self join. Without a condition such as a.id < b.id, a self join pairs every row with itself and lists each pair twice.
  • INNER JOIN. An inner join drops rows that have no partner on the other side, and it repeats a row once for every match, which inflates SUM and COUNT.
Solve This SQL Challenge

Keep learning