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_wsskips 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 →
- Question 1easy
Return the
order_idof every order inordersthat is not indw_orders.Tables
- Question 2easy
What does this return?
SELECT COUNT(*) FROM ( SELECT order_id FROM dw_orders EXCEPT SELECT order_id FROM orders );
- Question 3medium
How many rows does this return?
SELECT COUNT(*) FROM ( SELECT order_id FROM dw_orders EXCEPT ALL SELECT order_id FROM orders );
- Question 4medium
In a join, the source column is NULL and the target is 'Card'. Which condition reports this row as different?
- 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?
Finding the exact rows: EXCEPT and FULL OUTER JOIN
Finding the exact rows: EXCEPT and FULL OUTER JOIN
Finding the exact rows: EXCEPT and FULL OUTER JOIN
Finding the exact rows: EXCEPT and FULL OUTER JOIN
Finding the exact rows: EXCEPT and FULL OUTER JOIN
2 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.