WHERE Clause
WHERE filters rows before grouping or projection.
Introduction
WHERE filters rows before grouping or projection. It is your single most-used optimization tool — a good WHERE turns a 10-second scan into a 5-millisecond index lookup.
Learning objective: Apply WHERE Clause correctly in a TechLearningPRO production database — with clear query intent, safe defaults, and failure modes you can explain in review.
Treat every example as production-shaped SQL: explicit column lists, deterministic predicates, and indexes that match the access path. Prefer PostgreSQL dialect notes when dialects diverge; call out MySQL differences when they matter.
Purpose of this lesson
Apply WHERE Clause correctly in a TechLearningPRO production database — with clear query intent, safe defaults, and failure modes you can explain in review.
Understanding the topic
Core concepts to understand:
- Comparison:
= < > <= >= <>. - Logical:
AND, OR, NOT. - Membership:
IN, NOT IN. - Pattern:
LIKE,ILIKE(case-insensitive in PG),~regex. - NULL:
IS NULL / IS NOT NULL— never= NULL. - Range:
BETWEEN a AND b(inclusive both sides).
Step-by-step explanation
- State the business question WHERE Clause answers and the tables involved.
- Read the workflow diagram and map each stage to parser, optimizer, or storage.
- Write the smallest correct statement (or design change) for a TechLearningPRO Orders/learners dataset.
- Predict the failure mode: wrong cardinality, missing index, or unsafe write — then verify.
Syntax reference
Visual workflow / architecture:
Query rows ── WHERE filter ──▶ remaining rows ──▶ GROUP/SELECTWHERE status = 'paid'AND amount > 100AND created_at >= now() - interval '7 days'AND email ILIKE '%@gmail.com'
SELECT <cols> FROM <table> WHERE <col> <op> <value> [AND|OR <col> <op> <value> ...];
WHEREKeeps only rows where the predicate is TRUE.=, <, >Comparison operators.AND / ORCombine multiple conditions.IN / LIKE / BETWEENSet, pattern and range matching.IS NULLRequired for NULL — never use = NULL.
SELECT id, email, amountFROM ordersWHERE status = 'paid'AND amount > 50;
| id | status | amount | |
|---|---|---|---|
| 101 | leo@acme.io | paid | 120 |
| 102 | mia@acme.io | pending | 80 |
| 103 | ana@acme.io | paid | 45 |
| 104 | rio@acme.io | paid | 220 |
| id | amount | |
|---|---|---|
| 101 | leo@acme.io | 120 |
| 104 | rio@acme.io | 220 |
WHERE acts like a funnel — only matching rows continue downstream.
Informative example
Worked SQL example:
SELECT id, email, amountFROM ordersWHERE status = 'paid'AND amount > 50;
Real-world use
Every page in every product is filtered: 'unread emails', 'orders this month', 'active users'. WHERE drives all of it.
Security implications
SQL is a trust boundary. Application code must never concatenate untrusted input into statements.
- Use bind parameters / prepared statements — never string-built SQL from request bodies.
- Least-privilege DB roles: app users should not own DDL in production.
- Avoid returning raw DB errors to clients; log server-side with a correlation id.
Best practices
- Filter first, project last — use WHERE, not HAVING, for row filters.
- Make sure WHERE columns have indexes for big tables.
- Avoid functions on indexed columns:
WHERE lower(email) = ...kills the index.
Common mistakes
= NULLnever matches — use IS NULL.NOT IN (...)with NULLs always returns empty.- Functions on columns prevent index use.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionQ1. Why doesn't <code>= NULL</code> work?+
Answer
2IntermediateQuestionQ2. Difference between WHERE and HAVING.+
Answer
3IntermediateQuestionQ3. LIKE vs ILIKE.+
Answer
4AdvancedQuestionQ4. What is a sargable predicate?+
Answer
5AdvancedQuestionQ5. Inclusive nature of BETWEEN.+
Answer
Hands-on exercise
Hands-on + interview drill:
- Implement the smallest correct example for WHERE Clause on an Orders-like schema.
- Q1. Why doesn't
= NULLwork? - Q2. Difference between WHERE and HAVING.
- Q3. LIKE vs ILIKE.
- Q4. What is a sargable predicate?
- Q5. Inclusive nature of BETWEEN.
Summary
WHERE Clause is a production SQL skill: state the question, write the precise statement, and prove it with an execution plan or constraint — not with lucky result sets.