ANTI-JOINs & Finding Non-Matching Rows: Functions
Module: Joins & Relationships
SELECT c.* FROM customers c LEFT JOIN orders o ON c.id = o.customer_id WHERE o.id IS NULL;
Use LEFT JOIN to preserve all left rows
Check right table PRIMARY KEY for NULL
Do not check foreign key for NULL
WHERE clause filters to unmatched rows only
Alternative: NOT EXISTS subquery
Core references in this topic include WHERE, =, != / <>. Learn what each one does, when to use it, and the execution or engine rules that matter.
WHERE
Filters rows before projection and sorting. It decides which rows continue through the query pipeline.
SELECT ... FROM table WHERE condition;
Most performance issues start with a weak WHERE clause or a missing supporting index.
=
Returns rows where the left and right values are exactly equal.
column = value
Use with exact matches. Do not use = NULL.
!= / <>
Returns rows where the compared values are not equal.
column <> value
SQL supports both <> and != in many engines, but <> is the portable form.
<, >, <=, >=
Range comparison operators for less-than, greater-than, and inclusive boundary checks.
salary >= 80000
IS NULL / IS NOT NULL
Tests whether a value is missing. SQL NULL semantics require dedicated NULL predicates.
manager_id IS NULL
Never use = NULL or != NULL.
EXISTS
Tests whether a correlated or non-correlated subquery returns at least one row.
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id)
ANY / ALL
Compares one value against every or at least one value from a subquery result.
salary > ALL (SELECT salary FROM interns)
INTERVAL
Represents a duration that can be added to or subtracted from dates and timestamps.
order_date + INTERVAL '7 days'
PRIMARY KEY
Uniquely identifies each row and implicitly requires NOT NULL.
customer_id INT PRIMARY KEY
FOREIGN KEY
Enforces referential integrity by requiring a matching row in another table.