TECHNOLOGIESTieInn Technologies

Finding the exact rows: EXCEPT and FULL OUTER JOIN

ETL & Data Warehouse Testing · 7 questions

Short notes

Key idea

Reconciliation says something is wrong; row comparison names the exact rows. EXCEPT in both directions finds missing and extra rows (and, with copied columns added, changed values). A FULL OUTER JOIN on the key labels every difference. EXCEPT removes duplicates, so check those separately.

Syntax

SELECT order_id FROM orders EXCEPT SELECT order_id FROM dw_orders;   -- missing
SELECT order_id FROM dw_orders EXCEPT SELECT order_id FROM orders;   -- extra
SELECT order_id, amount FROM orders
EXCEPT SELECT order_id, amount FROM dw_orders;                       -- changed
SELECT order_id FROM dw_orders EXCEPT ALL SELECT order_id FROM orders; -- duplicates
SELECT order_id, COUNT(*) FROM dw_orders GROUP BY 1 HAVING COUNT(*) > 1;

In joins, compare nullable columns with IS DISTINCT FROM.

Example

Shop warehouse: 57 and 75 missing (month-end dates), 46 and 47 duplicated, 66's amount changed from 30469.40 to 34069.40. Together they explain the +₹14,214.95 total difference exactly.

Traps

  • EXCEPT in only one direction.
  • Expecting EXCEPT to show duplicates.
  • Comparing transformed columns raw.
  • Joining a target with duplicates without aggregating first.
  • Row hashes: concat_ws skips NULLs; format numbers the same on both sides.

Want the full lesson with step-by-step examples, a project and a certificate? ETL & Data Warehouse Testing course, ₹120 →

Questions

Open in Code Lab5 free questions
5 questions

    Finding the exact rows: EXCEPT and FULL OUTER JOIN

  1. Question 1easy

    Return the order_id of every order in orders that is not in dw_orders.

    Tables
  2. Finding the exact rows: EXCEPT and FULL OUTER JOIN

  3. Question 2easy

    What does this return?

    SELECT COUNT(*) FROM (
      SELECT order_id FROM dw_orders
      EXCEPT
      SELECT order_id FROM orders
    );
  4. Finding the exact rows: EXCEPT and FULL OUTER JOIN

  5. Question 3medium

    How many rows does this return?

    SELECT COUNT(*) FROM (
      SELECT order_id FROM dw_orders
      EXCEPT ALL
      SELECT order_id FROM orders
    );
  6. Finding the exact rows: EXCEPT and FULL OUTER JOIN

  7. Question 4medium

    In a join, the source column is NULL and the target is 'Card'. Which condition reports this row as different?

  8. Finding the exact rows: EXCEPT and FULL OUTER JOIN

  9. Question 5hard

    You hash rows with md5(concat_ws('|', a, b, c)). Rows ('x', NULL, 'y') and ('x', 'y', NULL) get the same hash. Why, and what's the fix?

2 more questions on this topic, with hints, and tests

Sign up free for 21 days of every question and test.