CODE WORKSPACE

Referential integrity audit

Return check_name and failing_rows for two checks: 'orders_without_items' and 'orphan_line_items'. Order by check_name.

Write each direction as a LEFT JOIN with an IS NULL filter, count the rows, then UNION ALL them.

Sample input

ordersFive orders (1003, 1006, 1009, 1011, 1012) have no rows in order_items.
order_idstatus
1003pending
1006refunded
1009pending
1011paid
1012shipped
order_itemsEvery order_items.order_id exists in orders, so there are no orphan line items.
order_idsku
1001SKU-A
1002SKU-C
1004SKU-A
1005SKU-F
1007SKU-B

Expected output

Expected outputTwo referential checks as counts; the orphan check passes with 0.
check_namefailing_rows
orders_without_items5
orphan_line_items0

Constraints

Use a LEFT JOIN + IS NULL anti-join for each direction, COUNT the failing rows, and UNION ALL the two checks. A clean direction must return 0, not no rows.

Expected skills

Anti-join referential checks and reporting gaps as counts.

SQL
Loading...

AI evaluation

Run the SQL query to inspect preview rows.