LearnViz CompSci
Home / Learn / Databases / WHERE and Filtering

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

  1. The query is WHERE age >= 18 AND age < 65.
  2. Row with age 17 is excluded.
  3. Age 25 passes both tests.
  4. Age 40 passes.
  5. 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

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Databases quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is WHERE and Filtering?
Clauses restricting which rows are returned. It belongs to Databases and is a beginner-level topic.
Where do people use WHERE and Filtering?
Selecting recent orders, searching by customer, limiting reports by date range and applying row-level security conditions use filtering.
What is the most common mistake with WHERE and Filtering?
Writing column = NULL instead of IS NULL returns nothing.
What should I learn before this topic?
Start with SELECT Queries, the topic just before it in Databases.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Databases quiz.
Is WHERE and Filtering hard to learn?
It is a Beginner topic, so it works as an entry point. If the terms are new, read the explanation twice and try the visualizer before the quiz.
How long does it take to learn WHERE and Filtering?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to WHERE and Filtering?
SELECT Queries, ORDER BY and LIMIT, SQL Basics are closely connected. The full list is in the related topics section of this page.
What should I study after WHERE and Filtering?
Continue with ORDER BY and LIMIT, the next topic in Databases.

Related topics