SQLRepPath
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
00/60 solved

Intermediate Joins · UNION vs UNION ALL

20. 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).