Courses/SQL/WHERE Clause & Filtering

    Lesson 4 โ€ข Beginner

    WHERE Clause & Filtering

    Filter your queries to retrieve exactly the rows you need using comparison operators, logical operators, and pattern matching.

    ๐ŸŽฏ What You'll Learn

    • Filter with comparison operators: =, !=, >, <, >=, <=
    • Combine conditions with AND, OR, NOT
    • Use BETWEEN, IN, LIKE for advanced filtering
    • Handle NULL values with IS NULL / IS NOT NULL

    Comparison Operators

    The WHERE clause filters rows that match a condition. Think of it as a bouncer at a club โ€” only rows that pass the check get through.

    OperatorMeaningExample
    =Equal toage = 25
    != or <>Not equalstatus != 'cancelled'
    >Greater thanprice > 100
    <Less thanstock < 10
    >=Greater or equalrating >= 4.0
    <=Less or equalsalary <= 50000

    Comparison Operators

    Filter rows using =, >, <, >=, <= and !=

    Try it Yourself ยป
    SQL
    -- Comparison operators
    SELECT * FROM products WHERE price > 100;
    SELECT * FROM products WHERE price <= 50;
    SELECT * FROM employees WHERE department = 'Engineering';
    SELECT * FROM orders WHERE status != 'cancelled';
    
    -- Numeric comparisons
    SELECT name, salary FROM employees
    WHERE salary >= 60000;

    Logical Operators: AND, OR, NOT

    Combine multiple conditions to build precise filters:

    AND โ€” Both must be true

    "Electronics AND under $500" โ†’ only cheap electronics

    OR โ€” Either can be true

    "IT OR Marketing" โ†’ employees in either department

    NOT โ€” Reverses a condition

    "NOT Sales" โ†’ everyone except Sales

    โš ๏ธ Common Mistake

    Forgetting parentheses when mixing AND and OR. AND has higher priority than OR, so A OR B AND C means A OR (B AND C) โ€” probably not what you intended!

    AND, OR, NOT

    Combine multiple conditions with logical operators

    Try it Yourself ยป
    SQL
    -- AND: ALL conditions must be true
    SELECT * FROM products 
    WHERE category = 'Electronics' AND price < 500;
    
    -- OR: ANY condition can be true
    SELECT * FROM students 
    WHERE grade >= 3.5 OR attendance > 90;
    
    -- NOT: reverse a condition
    SELECT * FROM employees 
    WHERE NOT department = 'Sales';
    
    -- Combine them (use parentheses!)
    SELECT * FROM products
    WHERE (category = 'Electronics' OR category = 'Books')
      AND price < 100;

    Special Operators: BETWEEN, IN, LIKE, IS NULL

    SQL provides shorthand operators that make common patterns cleaner:

    OperatorInstead ofUse
    BETWEENx >= 50 AND x <= 200x BETWEEN 50 AND 200
    INx='A' OR x='B' OR x='C'x IN ('A','B','C')
    LIKEComplex string matchingname LIKE 'J%'
    IS NULLCan't use = NULL!phone IS NULL

    ๐Ÿ’ก Pro Tip

    Never write WHERE column = NULL. NULL means "unknown" โ€” you can't compare it with =. Always use IS NULL or IS NOT NULL.

    BETWEEN, IN, LIKE, IS NULL

    Use special operators for cleaner, more powerful filtering

    Try it Yourself ยป
    SQL
    -- BETWEEN: range of values (inclusive)
    SELECT * FROM products
    WHERE price BETWEEN 50 AND 200;
    
    -- IN: match any value in a list
    SELECT * FROM employees
    WHERE department IN ('IT', 'Marketing', 'HR');
    
    -- LIKE: pattern matching
    -- % = any number of characters
    -- _ = exactly one character
    SELECT * FROM customers WHERE name LIKE 'John%';
    SELECT * FROM products WHERE code LIKE 'A__1';
    SELECT * FROM customers WHERE email LIKE '%@gmail.com';
    
    -- IS NULL / IS NOT NULL
    SELECT * FROM employees WHERE phon
    ...

    Real-World Filtering

    Build complex filters like a real e-commerce product search

    Try it Yourself ยป
    SQL
    -- Real-world filtering scenario:
    -- E-commerce product search
    SELECT product_name, price, rating, stock
    FROM products
    WHERE category IN ('Laptops', 'Tablets')
      AND price BETWEEN 300 AND 1500
      AND rating >= 4.0
      AND stock > 0
      AND product_name LIKE '%Pro%';
    
    -- Find inactive customers
    SELECT first_name, last_name, last_order_date
    FROM customers
    WHERE last_order_date < '2024-01-01'
      OR last_order_date IS NULL;

    ๐Ÿ“˜ Quick Reference

    PatternWhat it matches
    'John%'Starts with "John"
    '%son'Ends with "son"
    '%an%'Contains "an" anywhere
    'A__'Starts with A, exactly 3 chars total
    '_r%'Second character is "r"

    ๐ŸŽ‰ Lesson Complete!

    You can now filter data with precision using WHERE, logical operators, and special operators. Next, you'll learn to sort results with ORDER BY!

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service