SQL Flow Field Guide

Keep unmatched rows with LEFT JOIN

Preserve every left-side row when no right-side match exists.

What you are trying to do

A team roster should show every staff member, including people who have not received a task.

Work through it

  1. Identify Staff as the left input and Task as the right input.
  2. Match tasks using StaffId.
  3. Check the output for the staff member without a task.

Check your work

All three staff members appear. StaffId 102 has NULL for TaskId because no task matches.

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)
LEFT JOIN (VALUES (501, 101), (503, 103)) AS Task(TaskId, StaffId)
  ON Task.StaffId = Staff.StaffId
ORDER BY Staff.StaffId;

Knowledge check

Commands in this lesson

Next lesson Summarize rows by group