SQL
SQL Query to Find Records Without Relationships
Identify parent records that do not have any corresponding child records using LEFT JOIN and IS NULL, useful for data cleanup or reporting.
SELECT c.customer_id, c.customer_name
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;
How it works: This query finds all customers who have not placed any orders. It uses a `LEFT JOIN` to include all customers, even those without matching orders. The `WHERE o.order_id IS NULL` condition then filters the results to only show customers for whom no matching order was found, effectively identifying "inactive" customers.