6. A data quality report that counts NULLs correctly

SQLEasyNULL trapDuplicate trap

data quality · nulls · not in · duplicates

How the Colab notebook works

The notebook has the data, an answer cell and a check() function. You need a Google account; nothing is installed on your machine. Free Colab runs only two or three notebooks at a time, so end the session of a finished problem (Runtime > Disconnect and delete runtime) before opening the next one. "Mark as solved" is remembered in this browser only.

The situation

Before the shipments table is published each morning, a check has to report how many rows break each data quality rule. The report goes to a dashboard, so it must always list every rule, including the ones with no failures today.

Input tables

shipments

Supposed to be one row per shipment.

Column Type Description
shipment_id BIGINT Supposed to be unique.
order_id BIGINT The order being shipped.
warehouse_code STRING The warehouse it shipped from.
shipped_on DATE Never NULL.
delivered_on DATE NULL while the shipment is in transit.
weight_kg DOUBLE Weight of the parcel.

warehouses

The list of warehouses.

Column Type Description
warehouse_code STRING The code used in shipments. One closed site has a NULL code.
city STRING Where the warehouse is.

Requirement

Return one row for each of these six rules, with the number of rows in shipments that fail it. A rule that no row fails has failed_rows = 0.

rule_name A row fails when
missing_shipment_id shipment_id is NULL.
missing_order_id order_id is NULL.
duplicate_shipment_id Its shipment_id has already appeared. For an id that occurs three times, two rows fail.
delivered_before_shipped delivered_on is earlier than shipped_on. A shipment still in transit does not fail. Delivery on the shipping date does not fail.
invalid_weight weight_kg is NULL, zero or negative.
unknown_warehouse warehouse_code is NULL, or does not exactly match a code in warehouses.

Expected output

Exactly six rows.

Column Type Description
rule_name STRING One of the six names above.
failed_rows BIGINT Number of failing rows.

Solve it in the browser

This editor runs DuckDB inside your browser. Nothing is installed and nothing is sent to a server. DuckDB's SQL is close to Spark SQL but not identical. The Colab notebook runs real Spark.

query.sql DuckDB, in your browser
Ctrl + Enter runs the query

Your result appears here. Your draft is kept in this browser.

Explanation

Show explanationGives the answer away. Try the problem first.

The approach

Write one small query per rule and stack them with UNION ALL:

SELECT 'missing_order_id' AS rule_name, COUNT(*) AS failed_rows
FROM shipments
WHERE order_id IS NULL

An aggregate without GROUP BY always returns exactly one row, even when no input row matches. So a rule with no failures reports 0 instead of vanishing from the report.

The rules themselves are short. Each one has a NULL question hiding in it, and the whole problem is answering those correctly. In SQL, a comparison with NULL is neither true nor false; it is NULL, and WHERE keeps only rows where the condition is true.

The correct counts on this data (206 rows):

Rule Failed rows
missing_shipment_id 0
missing_order_id 7
duplicate_shipment_id 6
delivered_before_shipped 5
invalid_weight 13
unknown_warehouse 9

Common wrong answers

warehouse_code NOT IN (SELECT warehouse_code FROM warehouses). Returns 0, not 9. The warehouses table contains a NULL code. x NOT IN (a, b, NULL) expands to x <> a AND x <> b AND x <> NULL, and the last part is never true. One NULL in the list makes NOT IN reject every row. This is the most expensive NULL mistake in SQL because the query does not fail, it reports that everything is fine.

Filtering the NULL out of the list gives 5, still wrong: now the 4 shipments whose own warehouse_code is NULL are skipped, because NULL NOT IN (...) is NULL too.

Use NOT EXISTS (or a LEFT ANTI JOIN). It asks "is there a matching row?", and for a NULL code the answer is plainly no.

weight_kg <= 0 without the NULL check. Returns 7 instead of 13. The 6 rows with no weight at all are the worst ones, and they pass.

delivered_on <= shipped_on. Returns 35 instead of 5. Same-day delivery is valid. Note what goes right here without effort: rows still in transit have a NULL delivered_on, the comparison is NULL, and they are correctly not counted.

Counting duplicates the wrong way. There are 5 ids that occur more than once, and 11 rows carry one of those ids. Neither is the answer. Four ids occur twice and one occurs three times, so 6 rows are surplus: COUNT(shipment_id) - COUNT(DISTINCT shipment_id). Decide which of the three numbers a rule means before writing it.

Selecting the failing rows and then GROUP BY rule_name. Returns five rows. missing_shipment_id has no failing rows, so there is nothing to group and the rule drops out of the report. A dashboard cannot tell "no failures" from "check did not run".

Edge cases in the data

Case Count What it catches
NULL warehouse_code in warehouses 1 row NOT IN returning nothing.
NULL warehouse_code in shipments 4 rows NOT IN with a cleaned list; inner joins.
Codes in the wrong case (pnq1, blr1) 2 rows Assuming a lookup is case-insensitive.
NULL weight 6 rows weight_kg <= 0 alone.
Delivered on the shipping date about 30 rows <= instead of <.
Not delivered yet about 1 in 10 rows Treating NULL delivered_on as a failure.
One id three times 1 id Counting duplicated ids instead of surplus rows.
A rule with no failures missing_shipment_id Reports that omit zero rows.