Using LEFT JOIN and IS NULL to Include Non-Matching Rows
To include rows from one table even when there are no matching rows in the related table, you can use a LEFT JOIN. A LEFT JOIN returns all rows from the left (primary) table and any matching rows from the right table. When no match exists, the right-side columns become NULL. By adding an IS NULL condition, you can specifically identify rows that do not have a match.
In this query, all customers are selected, but only those with no corresponding orders (where orders.customer_id is NULL) are returned.
To find rows in one table that have no related rows in another table
To identify missing or orphaned records
To perform anti-joins (show 'A' records that don't exist in 'B')
To ensure a complete list from the primary table even when data is missing on the joined side