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

Combine Results With UNION ALL

Append result sets together without removing duplicates.

Docker-validated Not currently validation-green

Combine two result sets and keep duplicates

Unlike UNION, UNION ALL keeps both Linus rows in the final output.

Rows loaded before the example query runs.
Setup
CREATE TABLE morning (name VARCHAR(50));

CREATE TABLE evening (name VARCHAR(50));

INSERT INTO
  morning (name)
VALUES
  ('Ada'),
  ('Linus');

INSERT INTO
  evening (name)
VALUES
  ('Linus'),
  ('Grace');
Shared across supported engines.
SQL
SELECT
  name
FROM
  morning
UNION ALL
SELECT
  name
FROM
  evening
ORDER BY
  name;
Returned rows for the shared example.
name
Ada
Grace
Linus
Linus

The preserved duplicate row is the key behavior shown here.

Where this command helps.

  • combining event streams where duplicates are meaningful
  • merging multiple query branches without paying deduplication cost

What the command is doing.

UNION ALL stacks the rows from multiple queries and preserves duplicates. It is usually faster than UNION because the database does not need to deduplicate the combined output.