Combine compatible result sets
Use UNION to combine compatible row shapes and remove duplicate result rows.
What you are trying to do
Two shifts report the product codes they handled. The handover needs one list of codes, even when both shifts handled the same product.
Work through it
- Compare the number and types of columns in both SELECT statements.
- Follow the two inputs into UNION.
- Check how the repeated B2 code appears in the final result.
Check your work
The output contains A1, B2, and C3 once each, ordered by Code.
Practice SQL
Open in SQL FlowSELECT Code FROM (VALUES (N'A1'), (N'B2')) AS Morning(Code)
UNION
SELECT Code FROM (VALUES (N'B2'), (N'C3')) AS Afternoon(Code)
ORDER BY Code;
Knowledge check
Commands in this lesson
Next lesson Read an invalid-column error