SQL Joins and Subqueries/Filter with NOT EXISTS

Return rows with no related matches.

Section: EXISTS and Set Operations

Filter with NOT EXISTS

sql
sql
SELECT c.customer_id
FROM customers c
WHERE NOT EXISTS (
  SELECT 1
  FROM orders o
  WHERE o.customer_id = c.customer_id
);
Explanation

NOT EXISTS is a robust anti-join approach.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing EXISTS and Set Operations
Filter with EXISTS
Return rows when a related row exists.
OpenIn sheetsqlsame section
Combine result sets with UNION
Merge distinct rows from two compatible queries.
OpenIn sheetsqlsame section
Combine result sets with UNION ALL
Merge all rows without deduplication.
OpenIn sheetsqlsame section
Find common rows with INTERSECT
Return rows present in both queries.
OpenIn sheetsqlsame section
Subtract rows with EXCEPT
Return rows from the first query not present in the second.
OpenIn sheetsqlsame section
Use a subquery in WHERE
Filter rows based on results from another query.
OpenIn sheetsql1 tag match