SQL · Level 2 Combining

SQL UNION and UNION ALL

Build one mailing list from customers and newsletter subscribers with UNION, and know when duplicates are removed or kept.

  • UNION stacks results
  • UNION removes duplicates, UNION ALL keeps them
  • ORDER BY at the very end

UNION stacks the rows of two queries that have the same columns. Trap: UNION removes duplicate rows but UNION ALL keeps them. Choose on purpose: one email per person, or every row for counting.

SELECT city FROM customers
UNION ALL          -- keeps repeats, e.g. to count
SELECT city FROM suppliers;

Return one mailing list with every email from customers and from subscribers, each address once, in alphabetical order. In the database below, [email protected] is in both tables.

Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.

customers 2 rows
idINTEGERemailTEXT
1[email protected]
2[email protected]
subscribers 2 rows
idINTEGERemailTEXT
1[email protected]
2[email protected]
solution.sql
Test Suites Pending
Output