SQL Flow Field Guide

Match rows with an inner join

Join two row sources on their shared key and predict the joined grain.

What you are trying to do

A work report needs the staff member for each task. A staff member can own more than one task.

Work through it

  1. Compare Task.StaffId with Staff.StaffId.
  2. Follow each matching task-staff pair through the INNER JOIN.
  3. Check why staff member 102 has no output row.

Check your work

The result has three rows for task-staff pairs 501/101, 502/101, and 503/103. Staff member 102 has no matching task.

Practice SQL

Open in SQL Flow
SELECT Staff.StaffId, Task.TaskId
FROM (VALUES (101, N'Care'), (102, N'Care'), (103, N'Ops')) AS Staff(StaffId, Team)
INNER JOIN (VALUES (501, 101), (502, 101), (503, 103)) AS Task(TaskId, StaffId)
  ON Task.StaffId = Staff.StaffId
ORDER BY Staff.StaffId, Task.TaskId;

Knowledge check

Commands in this lesson

Next lesson Keep unmatched rows with LEFT JOIN