Join related rows
Match rows by a key and keep unmatched rows when the report needs them.
What you are trying to do
A work queue needs its owner label while still showing unassigned tickets.
Work through it
- Compare the key columns in both table variables.
- Trace the LEFT JOIN condition.
- Explain why the unassigned row has a NULL owner.
Check your work
Every ticket appears once and the unassigned ticket remains in the result.
Practice SQL
Open in SQL FlowDECLARE @Ticket TABLE (TicketId int PRIMARY KEY, OwnerId int NULL); INSERT INTO @Ticket VALUES (1, 10), (2, NULL); DECLARE @Owner TABLE (OwnerId int PRIMARY KEY, Team nvarchar(20)); INSERT INTO @Owner VALUES (10, N'North'); SELECT t.TicketId, o.Team FROM @Ticket AS t LEFT JOIN @Owner AS o ON o.OwnerId = t.OwnerId ORDER BY t.TicketId;
Commands in this lesson
Next lesson Group rows into totals