PREP WITH PROOF
Menu
FREE SQL LEARNING TOOL · NO SIGNUP

SQL UNION vs UNION ALL explorer

See exactly which selected rows collapse, where they came from and how the total changes. Compare both operations before choosing a duplicate policy.

Define two small tables

Every input pair is [id, amount]. Repeated pairs represent separate occurrences. Inputs are calculated in your browser and are not submitted or put in the URL.

JSON arrays only. Up to 20 rows per table and 4000 characters per field. Each value must be an integer from -1000000 to 1000000 or lowercase null. Use [] for an empty table.

Load an example:

Compare counts and totals

Same projection, two duplicate policies
OperationOutput rowsSUM(amount)Occurrences removed
UNION ALL7700
UNION4453

SUM ignores NULL amounts and returns NULL when no non-NULL amount remains. A zero sum is different from NULL. Changing selected columns can change which complete rows count as duplicates.

Result: UNION

4 output rows. A1 means the first input row in A. Provenance is an explanation beside the result, not an extra SQL column. Under UNION, one row can represent several input occurrences.

Selected values and contributing input occurrences
idamountInput occurrences
110A1
210A2, A3, B1
320B2
NULL5A4, B3

Display order follows the generated ORDER BY: selected columns ascending, NULLs last. SQL without an explicit final ORDER BY does not guarantee this order.

Run the same comparison

Select and copy this read-only query for PostgreSQL or SQLite 3.30 or later. It creates no stored tables. Provenance is not included; the optional source label is included only when selected above.

WITH a(id, amount) AS (VALUES
  (1, 10),
  (2, 10),
  (2, 10),
  (CAST(NULL AS INTEGER), 5)
),
b(id, amount) AS (VALUES
  (2, 10),
  (3, 20),
  (CAST(NULL AS INTEGER), 5)
)
SELECT id, amount FROM a
UNION
SELECT id, amount FROM b
ORDER BY id ASC NULLS LAST, amount ASC NULLS LAST;

Read the default example

A has four rows; B has three. UNION ALL preserves all seven occurrences and sums the amounts to 70. UNION over id and amount retains four distinct pairs and sums to 45. Switch to amount only: the three distinct values are 5, 10 and 20, giving 35. Those are different questions, not interchangeable ways to repair a total.

Source labels change row identity

Include A / B as a selected column to distinguish equal values from different sources. In the default fixture UNION then retains six rows: the repeated [2,10] inside A still collapses. The diagnostic A1/B1 occurrence labels beside the results never participate in comparison; adding a unique row ID to SQL would change the projection again.

NULL and repeated facts

Duplicate elimination treats corresponding NULL values as equal for this comparison. That differs from a WHERE equality predicate, where NULL = NULL is unknown. Two identical projected rows can therefore collapse even when their id is NULL. This does not establish that the records are the same business event. Decide which columns identify a fact before deduplicating.

Use a bounded model, then test your engine

This is a learning model for two-column integer-or-NULL inputs, not a database connection or arbitrary SQL runner. It does not model text collations, mixed storage types, floating-point values, implicit conversions, joins, filters or optimizer performance. It compares complete selected rows rather than deduplicating each column independently. Negative and zero amounts are valid; neither implies a missing value.

The generated query makes ordering explicit and was compared with actual in-memory SQLite execution. PostgreSQL syntax is documented but was not executed against a PostgreSQL server here. Do not infer a production performance result from these tiny examples. Reset input rows reloads the sample while retaining your selected projection and operation.

Read the guide: UNION, duplicates, totals and ordering →

Trace row multiplication in the SQL JOIN explorer →

Explore SQL NULL predicates →

Official references