SQL Tutorial 0/85 lessons ~6 min read Lesson 12

    WHERE Clause

    WHERE filters rows before grouping or projection.

    Course progress0%
    Focus
    13 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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

    1. State the business question WHERE Clause answers and the tables involved.
    2. Read the workflow diagram and map each stage to parser, optimizer, or storage.
    3. Write the smallest correct statement (or design change) for a TechLearningPRO Orders/learners dataset.
    4. Predict the failure mode: wrong cardinality, missing index, or unsafe write — then verify.

    Syntax reference

    Visual workflow / architecture:

    bash
    Query rows ── WHERE filter ──▶ remaining rows ──▶ GROUP/SELECT
    WHERE status = 'paid'
    AND amount > 100
    AND created_at >= now() - interval '7 days'
    AND email ILIKE '%@gmail.com'
    Syntax Pattern
    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.
    Live Example
    sql
    SELECT id, email, amount
    FROM orders
    WHERE status = 'paid'
    AND amount > 50;
    orders
    idemailstatusamount
    101leo@acme.iopaid120
    102mia@acme.iopending80
    103ana@acme.iopaid45
    104rio@acme.iopaid220
    filtered
    idemailamount
    101leo@acme.io120
    104rio@acme.io220
    WHERE filter
    drops rows that don't match

    WHERE acts like a funnel — only matching rows continue downstream.

    Informative example

    Worked SQL example:

    sql
    SELECT id, email, amount
    FROM orders
    WHERE 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

    • = NULL never 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.

    5 questions
    1BeginnerQuestionQ1. Why doesn't <code>= NULL</code> work?+

    Answer

    WHERE removes rows before any aggregation.
    2IntermediateQuestionQ2. Difference between WHERE and HAVING.+

    Answer

    NULL has special rules — use IS NULL.
    3IntermediateQuestionQ3. LIKE vs ILIKE.+

    Answer

    Indexable predicates are the key to fast queries.
    4AdvancedQuestionQ4. What is a sargable predicate?+

    Answer

    Indexable predicates are the key to fast queries.
    5AdvancedQuestionQ5. Inclusive nature of BETWEEN.+

    Answer

    Indexable predicates are the key to fast queries.

    Hands-on exercise

    Hands-on + interview drill:

    • Implement the smallest correct example for WHERE Clause on an Orders-like schema.
    • Q1. Why doesn't = NULL work?
    • 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.

    Ready to mark this lesson complete?Track your journey across the entire course.