The search box of SQL
WHERE filters rows before any grouping or aggregation. Every condition you put in WHERE eliminates rows that the rest of the query never sees. Cheaper queries, smaller results, fewer surprises.
The operators
- Equality / comparison:
=,<>,<,<=,>,>=. - Boolean combination:
AND,OR,NOT— use parentheses for non-trivial logic. - Set membership:
IN (...),NOT IN (...). - Range:
BETWEEN x AND y(inclusive on both ends). - Pattern:
LIKE(case-sensitive),ILIKE(case-insensitive, PostgreSQL). - NULL:
IS NULL,IS NOT NULL,IS DISTINCT FROM.
Filter early, filter cheaply
Push every filter you can into WHERE — it shrinks the data the rest of the query has to process. Filters that can use an index ("indexable" predicates) are dramatically cheaper than ones that require a full scan.