Drill 9 of 9 in Joining Tables
0

Joining Tables · UNION vs UNION ALL

9. Total touchpoints per user

Concept: UNION vs UNION ALLshow

UNION stacks the rows of two queries with matching columns into one result, then removes duplicate rows. UNION ALL stacks them and keeps every row. Use UNION ALL whenever the repeats are real data, for example when you're about to count them.

Example (different data):

SELECT customer_id, COUNT(*) AS contacts
FROM (
  SELECT customer_id FROM calls
  UNION ALL
  SELECT customer_id FROM emails
) AS c
GROUP BY customer_id;

Your task

Customer success scores engagement by touchpoints: every login and every product event counts as one.

Return user_id and touches (number of logins plus number of events) for every user who appears in either table. Sort by touches (highest first), then user_id.

Solution unlocks after 3 more tries

Starting Postgres in your browser…

query.sql
Output

Write a query and press Run Query (Ctrl/⌘ + Enter).