Beginner Topic 9 of 51 Databases
WHERE and Filtering
Clauses restricting which rows are returned.
How it works
The WHERE clause keeps only rows for which a condition is true. Conditions use comparison operators, AND, OR and NOT, BETWEEN, IN, LIKE for pattern matching and IS NULL for missing values. SQL uses three-valued logic, true, false and unknown, so comparisons with NULL are never true and a row is kept only when the condition is true. Filters can use indexes if written in a sargable form, comparing a bare column to a value.
Step-by-step walkthrough
- The query is WHERE age >= 18 AND age < 65.
- Row with age 17 is excluded.
- Age 25 passes both tests.
- Age 40 passes.
- Age 64 passes, and rows with NULL age never match comparisons.
Where it is used
Selecting recent orders, searching by customer, limiting reports by date range and applying row-level security conditions use filtering.
Common pitfall
Writing column = NULL instead of IS NULL returns nothing. Wrapping an indexed column in a function, such as WHERE YEAR(date) = 2024, prevents index use. Mixing AND and OR without parentheses gives wrong results because AND binds tighter. LIKE with a leading wildcard scans the whole table. Always check boundary conditions in ranges.
Where it fits in the course
WHERE and Filtering is topic 9 of 51 in Databases, rated Beginner. It follows SELECT Queries. The next topic is ORDER BY and LIMIT.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Databases quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Databases quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is WHERE and Filtering?
Where do people use WHERE and Filtering?
What is the most common mistake with WHERE and Filtering?
What should I learn before this topic?
How can I practice it?
Is WHERE and Filtering hard to learn?
How long does it take to learn WHERE and Filtering?
Which topics are related to WHERE and Filtering?
What should I study after WHERE and Filtering?
Related topics
- SELECT Queries (Databases)
- ORDER BY and LIMIT (Databases)
- SQL Basics (Databases)
- INSERT, UPDATE, DELETE (Databases)
- Image Filtering (Graphics, Vision & Media)