Example 1
List orders whose customer does not exist
Orders 101 and 103 reference existing customers, so the joined customer row is present. Orders 102 and 104 point to customer ids 9 and 8, which are missing from customers, so the parent side is NULL and those orders are flagged as orphans.
CREATE TABLE customers (id INT, name VARCHAR(50));
CREATE TABLE orders (id INT, customer_id INT, total INT);
INSERT INTO
customers
VALUES
(1, 'Ada'),
(2, 'Bob');
INSERT INTO
orders
VALUES
(101, 1, 50),
(102, 9, 75),
(103, 2, 20),
(104, 8, 40);SELECT
o.id,
o.customer_id,
o.total
FROM
orders o
LEFT JOIN customers c ON c.id = o.customer_id
WHERE
c.id IS NULL
ORDER BY
o.id;| id | customer_id | total |
|---|---|---|
| 102 | 9 | 75 |
| 104 | 8 | 40 |
The child-to-parent audit uses the same `LEFT JOIN` and `IS NULL` pattern across engines.