Example 1
Preserve repeated names across two event sources
Bob appears in both sources, so UNION ALL returns both rows. If this query used plain UNION, one of the Bob rows would be removed. That difference matters whenever duplicate rows carry meaning, such as repeated events, log entries, or transactions.
CREATE TABLE web_signups (name VARCHAR(50));
CREATE TABLE store_signups (name VARCHAR(50));
INSERT INTO
web_signups (name)
VALUES
('Alice'),
('Bob');
INSERT INTO
store_signups (name)
VALUES
('Bob'),
('Carol');SELECT
name
FROM
web_signups
UNION ALL
SELECT
name
FROM
store_signups
ORDER BY
name;| name |
|---|
| Alice |
| Bob |
| Bob |
| Carol |
This example uses `UNION ALL` specifically to show that the second `Bob` is preserved.