skipnothing/SQL

WHERE & Filters

Four products sit as rows, one of them with a blank value. The test price > 40 keeps just two, but the test = NULL keeps zero, so even the blank row stays hidden.

  • ▸A row survives only when its test comes out TRUE; one that comes out UNKNOWN is dropped, same as FALSE.
  • ▸category = NULL matches no rows at all, not even the row whose value is truly blank.
  • ▸Without parentheses, a OR b AND c becomes a OR (b AND c): AND groups ahead of OR.
BUILDS ON
01

Keeping Only the Rows That Pass

The products table holds four rows: Keyboard, Mouse, Monitor, Cable. You want only the ones priced over 40, so you add a test after the table: SELECT name, price FROM products WHERE price > 40.

The engine takes that test, price > 40, and runs it against each row on its own. Keyboard at 42.00 passes (TRUE), Mouse at 17.50 fails (FALSE), Monitor at 299.00 passes, Cable at 4.50 fails. Two rows come out. The per-row test that decides keep-or-drop is the predicate, and a row reaches the result only when its predicate is TRUE, never on a FALSE.

The test runs at a fixed spot in the pipeline. FROM products gathers the table's rows first, then the predicate filters those rows, and only the survivors reach the select list. So the filter sees the raw stored rows, not the name, price shape you wrote: the select list is applied last, to whatever rows are left. PostgreSQL's plan, its record of how it ran the query, shows the same order: the test sits inside the Seq Scan, its pass over every row, and Rows Removed by Filter counts the rows the test dropped.

YOU STAMP THE ROWS, POSTGRESQL STAMPS THEM TOO

Keep going, sign up to unlock the rest

3 more parts in this topic, plus 30+ more topics in SQL.

Sign up, it's freeSee the full SQL