sqlcmd.net validated sql reference
intermediate set-operations MySQL MariaDB SQL Server PostgreSQL SQLite

Choose UNION Or UNION ALL Intentionally

`UNION` removes duplicates, while `UNION ALL` keeps them. Picking the wrong one silently changes row counts.

Docker-validated Not currently validation-green

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.

Rows loaded before the example query runs.
Setup
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');
Shared across supported engines.
SQL
SELECT
  name
FROM
  web_signups
UNION ALL
SELECT
  name
FROM
  store_signups
ORDER BY
  name;
Returned rows for the shared example.
name
Alice
Bob
Bob
Carol

This example uses `UNION ALL` specifically to show that the second `Bob` is preserved.

Where this command helps.

  • preserving duplicate events or records from multiple sources
  • avoiding accidental deduplication when combining query branches

What the command is doing.

A common set-operation mistake is using UNION when duplicates are meaningful, or using UNION ALL when deduplication is required. UNION performs duplicate elimination across the combined result, while UNION ALL simply stacks rows. That affects both correctness and performance.