Comparing Two Query Results

Set operations are used to check that a result is unchanged after a query is modified, or that the data in two tables is the same. A set operation compares all columns of a row at once, so you do not need a condition per column.

In the examples, A is the original query and B is a query whose customer range was moved by mistake.

-- A: GOLD or higher among customers 1-100
SELECT cust_id, grade FROM customers WHERE cust_id BETWEEN 1 AND 100 AND grade <> 'NORMAL'
-- B: GOLD or higher among customers 51-150
SELECT cust_id, grade FROM customers WHERE cust_id BETWEEN 51 AND 150 AND grade <> 'NORMAL'

EXCEPT in both directions

A EXCEPT B is the rows only in A, and B EXCEPT A is the rows only in B. Looking in one direction only misses the rows that exist only on the other side.

Rows only in A

SELECT cust_id, grade FROM customers WHERE cust_id BETWEEN 1 AND 100 AND grade <> 'NORMAL'
EXCEPT
SELECT cust_id, grade FROM customers WHERE cust_id BETWEEN 51 AND 150 AND grade <> 'NORMAL'
ORDER BY cust_id

Rows only in B

SELECT cust_id, grade FROM customers WHERE cust_id BETWEEN 51 AND 150 AND grade <> 'NORMAL'
EXCEPT
SELECT cust_id, grade FROM customers WHERE cust_id BETWEEN 1 AND 100 AND grade <> 'NORMAL'
ORDER BY cust_id

Only A has 10–50, and only B has 110–150. The two results are the same only if both directions return 0 rows. However, EXCEPT removes duplicates, so this only confirms that "the same kinds of rows exist". Whether the row counts also match is checked with the GROUP BY method below.

Oracle also accepts MINUS instead of EXCEPT. Versions before 21c have only MINUS.

Oracle · MINUS

SELECT cust_id, grade FROM customers WHERE cust_id BETWEEN 1 AND 100 AND grade <> 'NORMAL'
MINUS
SELECT cust_id, grade FROM customers WHERE cust_id BETWEEN 51 AND 150 AND grade <> 'NORMAL'
ORDER BY cust_id

Symmetric difference: the differences in one result

Joining the two-way EXCEPT with UNION ALL returns every row that exists on only one side. Wrap each EXCEPT in an inline view and add a constant column for the side.

Rows on one side only, with the side

SELECT 'A' AS side, d.cust_id, d.grade
FROM (SELECT cust_id, grade FROM customers WHERE cust_id BETWEEN 1 AND 100 AND grade <> 'NORMAL'
      EXCEPT
      SELECT cust_id, grade FROM customers WHERE cust_id BETWEEN 51 AND 150 AND grade <> 'NORMAL') d
UNION ALL
SELECT 'B', d.cust_id, d.grade
FROM (SELECT cust_id, grade FROM customers WHERE cust_id BETWEEN 51 AND 150 AND grade <> 'NORMAL'
      EXCEPT
      SELECT cust_id, grade FROM customers WHERE cust_id BETWEEN 1 AND 100 AND grade <> 'NORMAL') d
ORDER BY cust_id

Writing A EXCEPT B UNION ALL B EXCEPT A without the wrapping gives a different result. UNION and EXCEPT are evaluated left to right, as ((A EXCEPT B) UNION ALL B) EXCEPT A, which leaves only B EXCEPT A.

When INTERSECT is mixed in, the order differs per DB. PostgreSQL and SQL Server evaluate INTERSECT before UNION and EXCEPT, while Oracle evaluates INTERSECT left to right as well. When chaining three or more set operations, always fix the order with parentheses or inline views.

Down to duplicate counts: UNION ALL + GROUP BY

Add a source flag column to the rows of each side, stack them with UNION ALL, group by row content, and count per side. This example uses grade lists with many duplicates. A is the grades of customers 1–12 (NORMAL 11, GOLD 1), and B is the grades of customers 91–100 (NORMAL 9, VIP 1).

EXCEPT does not see the difference in NORMAL counts

SELECT grade FROM customers WHERE cust_id BETWEEN 1 AND 12
EXCEPT
SELECT grade FROM customers WHERE cust_id BETWEEN 91 AND 100

The result is 1 row, GOLD. NORMAL is on both sides, so it drops out even though the counts differ.

How many times on each side

SELECT grade, SUM(in_a) AS cnt_a, SUM(in_b) AS cnt_b
FROM (SELECT grade, 1 AS in_a, 0 AS in_b FROM customers WHERE cust_id BETWEEN 1 AND 12
      UNION ALL
      SELECT grade, 0, 1 FROM customers WHERE cust_id BETWEEN 91 AND 100) u
GROUP BY grade
HAVING SUM(in_a) <> SUM(in_b)
ORDER BY grade
gradecnt_acnt_bReading
GOLD10Only in A
NORMAL119On both sides, but A has 2 more
VIP01Only in B

Removing HAVING also shows the rows whose counts match. If the result has 0 rows, the two results match in both rows and row counts. When comparing several columns, list them all in both SELECT and GROUP BY.

EXCEPT ALL and INTERSECT ALL

Adding ALL counts duplicates instead of removing them. When a row appears m times in A and n times in B, the result is as follows.

OperationCount left in the result
EXCEPT1 if only in A, otherwise 0
EXCEPT ALLm − n (0 if negative)
INTERSECT1 if on both sides, otherwise 0
INTERSECT ALLthe smaller of m and n

Oracle, PostgreSQL · EXCEPT ALL

SELECT grade FROM customers WHERE cust_id BETWEEN 1 AND 12
EXCEPT ALL
SELECT grade FROM customers WHERE cust_id BETWEEN 91 AND 100
ORDER BY grade

Oracle, PostgreSQL · INTERSECT ALL

SELECT grade FROM customers WHERE cust_id BETWEEN 1 AND 12
INTERSECT ALL
SELECT grade FROM customers WHERE cust_id BETWEEN 91 AND 100
ORDER BY grade

EXCEPT ALL returns 1 GOLD row and 2 NORMAL rows, and INTERSECT ALL returns 9 NORMAL rows. Oracle also accepts MINUS ALL. SQL Server does not support EXCEPT and INTERSECT with ALL. If you need only the counts, get the same values with the GROUP BY method above.

EXCEPT ALL counts with GROUP BY

SELECT grade, SUM(in_a) - SUM(in_b) AS a_extra
FROM (SELECT grade, 1 AS in_a, 0 AS in_b FROM customers WHERE cust_id BETWEEN 1 AND 12
      UNION ALL
      SELECT grade, 0, 1 FROM customers WHERE cust_id BETWEEN 91 AND 100) u
GROUP BY grade
HAVING SUM(in_a) > SUM(in_b)
ORDER BY grade

NULL: depends on the comparison method

Set operations treat two NULLs as the same value (Set operations and NULL). The = in a join or NOT EXISTS treats two NULLs as unknown. So comparing a column that contains NULLs gives different answers depending on the method.

In the two example lists, nm of row 2 is NULL on both sides, and only nm of row 3 differs.

EXCEPT: only row 3 differs

SELECT id, nm FROM (VALUES (1, 'a'), (2, NULL), (3, 'c')) a(id, nm)
EXCEPT
SELECT id, nm FROM (VALUES (1, 'a'), (2, NULL), (3, 'x')) b(id, nm)

NOT EXISTS with =: row 2 with NULL is also reported as different

SELECT a.id, a.nm
FROM (VALUES (1, 'a'), (2, NULL), (3, 'c')) a(id, nm)
WHERE NOT EXISTS (SELECT 1
                  FROM (VALUES (1, 'a'), (2, NULL), (3, 'x')) b(id, nm)
                  WHERE b.id = a.id AND b.nm = a.nm)
ORDER BY a.id

EXCEPT returns only row 3, and NOT EXISTS returns rows 2 and 3. b.nm = a.nm is unknown for two NULLs, so row 2 found no match. Row 3 has the same id and only a different value, so running B EXCEPT A shows it on the other side as (3, 'x'). When the same key appears in both results, the row's value has changed.

MethodTwo NULLs
EXCEPT, INTERSECT, UNIONequal
GROUP BYsame group
= in a join or EXISTSunknown (no match)

If you must compare with a join or NOT EXISTS, handle NULLs separately. (b.nm = a.nm OR (b.nm IS NULL AND a.nm IS NULL)) works in all three DBs. PostgreSQL and SQL Server write the same meaning as b.nm IS NOT DISTINCT FROM a.nm.

TIP For comparing results, the UNION ALL + GROUP BY method that lists every column tells the most. It treats NULLs as the same value, shows which side a row is on and the count difference at once, and works as the same statement in all three DBs.