🔷 SQL: UNION vs UNION ALL
UNION — combines results and removes duplicates (slower — must sort/compare). UNION ALL — combines results and keeps everything including duplicates (faster).
Rule: Use UNION ALL unless you specifically need deduplication. It's significantly faster.
-- Combine two event sources
SELECT user_id, event_time, 'web' AS source FROM web_events
UNION ALL
SELECT user_id, event_time, 'mobile' AS source FROM mobile_events;
Practice Questions
Q: When would you actually need UNION (not UNION ALL)?