Modules / Database / WHERE Lab

Where Clause in SQL

The WHERE clause decides which rows survive. Write a real predicate — comparisons, AND/OR, IN, BETWEEN, LIKE, IS NULL — and this lab parses and evaluates it against every row, showing you exactly which ones matched and why.

The predicate column shows what your condition evaluated to for that row. Only TRUE rows are returned — UNKNOWN is not the same as FALSE, but it is discarded just the same.

Overview

WHERE applies a condition to input rows. For each row, SQL evaluates the condition and retains the row only when the result is TRUE. FALSE and UNKNOWN are both excluded. This filtering happens before GROUP BY and aggregate calculations.

Filter before grouping

query.sqlSQLite
Result

This returns paid orders whose total is at least 50. Conditions can combine comparisons with AND, OR, and NOT; parentheses make mixed logic easier to review.

NULL and three-valued logic

A comparison such as email = NULL is UNKNOWN, so it does not select missing emails. Use IS NULL or IS NOT NULL. Be careful with NOT IN when the candidate set may contain NULL; see NULL handling and EXISTS vs IN.

Write filters the planner can use

A simple predicate on an indexed column can make an index useful, but the planner decides whether that is cheaper than scanning. Wrapping a column in a function may prevent a regular index from helping unless an expression index exists. Do not rewrite a correct condition based on guesswork; inspect query plans and measure.

For grouped results, use HAVING rather than trying to filter aggregates in WHERE. See GROUP BY.

Test boundary cases

For a numeric filter such as quantity >= 5, test values 4, 5, and 6 to confirm the boundary is inclusive. For text and dates, check collation and time-zone assumptions. Parenthesize mixed AND and OR conditions because AND generally binds more tightly than OR. A predicate like status = 'paid' OR status = 'refunded' AND region = 'west' does not mean both statuses are limited to west; parentheses make the intended grouping clear. Test NULL separately because ordinary comparisons evaluate to UNKNOWN.

Read predicates as truth tables

When conditions mix AND and OR, write the intended groups explicitly. For example, a paid or refunded order from the west region should group the two status alternatives before combining them with the region condition. Without parentheses, operator precedence can include paid orders from other regions. NULL adds a third outcome: NOT UNKNOWN remains UNKNOWN, so a row with a missing status still fails the WHERE filter. A small table with one row per boundary case is often the fastest way to validate a complicated predicate.

Further reading

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “Filter before grouping”?

  2. What does this module say about “NULL and three-valued logic”?

  3. What does this module say about “Write filters the planner can use”?

Cheat sheet

Where Clause in SQL

Filter rows one at a time with WHERE and see how NULL turns a comparison into UNKNOWN — the trap behind most wrong SQL results.

DATABASE · vizlearn.in/database/where_clause_in_sql.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.