Start studying
Module 5 · SQL Module contents
Module 5  ·  Card 3 / 14beginner

Filtering with WHERE

The WHERE clause filters rows retrieved by a query, returning only records that satisfy specified conditional criteria.

It uses comparison operators like equals, greater than, and less than to test numerical and text values.

For example, writing WHERE status equals active retrieves only customers whose status matches that text string.

Multiple conditions can be combined using logical operators like AND, OR, and NOT to build nuanced filtering rules.

Filtering rows at the database level reduces network transfer volume and speeds up downstream data analysis significantly.

Learn in depth ↓
Module 05 · SQL · 14 concepts
In depth  /  15 lines

Filtering with WHERE, in more detail

The WHERE clause restricts the set of rows returned by evaluating a conditional predicate against each individual record.

In relational algebra, filtering rows corresponds to the selection operator, producing a horizontal slice of the table.

Predicates evaluate to a boolean truth value for every row: true, false, or unknown under three-valued logic.

Only rows where the predicate evaluates strictly to true are included in the query result set.

Standard comparison operators include equality, inequality, greater than, less than, and inclusive ranges with BETWEEN.

The IN operator tests whether a column value matches any item in a specified list or nested query result.

Pattern matching on text columns is accomplished using the LIKE operator paired with percent and underscore wildcards.

Percent matches any sequence of zero or more characters, while an underscore matches exactly one single character.

Multiple predicates are combined using boolean logic: AND requires all conditions, while OR requires at least one.

Parentheses are essential when mixing AND and OR operators to enforce intended precedence during evaluation.

The database query engine uses indexes on filtered columns to jump directly to matching rows without scanning the whole table.

Writing non-sargable predicates, such as wrapping columns in functions, prevents the database from utilising indexes.

For example, filtering on the year of a date using a function often forces a slow sequential table scan.

WHERE filters rows before any grouping or aggregation takes place, reducing the volume of data processed downstream.

A common mistake is placing aggregate conditions like sums or averages inside WHERE, which triggers an immediate syntax error.

Want the full technical article? Wikipedia ↗
Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
An analyst wants orders from 2024 where total value exceeds 100 pounds. How should the WHERE clause combine these conditions?
SQL

14 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close