Lesson 3: Filtering Data with WHERE

25 min read Beginner Lesson 3 of 12 13.5k learners

What is the WHERE Clause?

The WHERE clause filters records based on specific conditions. Without it, SQL queries return all rows in a table.

SQL
SELECT * FROM customers
WHERE city = 'New York';

Comparison Operators

  • = → Equal
  • <> or != → Not equal
  • >, <, >=, <= → Greater/less than
SQL
SELECT first_name, age FROM customers
WHERE age >= 30;

Logical Operators

  • AND → Combines multiple conditions (both must be true)
  • OR → At least one condition must be true
  • NOT → Negates a condition
SQL
SELECT first_name, city FROM customers
WHERE city = 'New York' OR city = 'Los Angeles';

Special Operators

  • IN → Match against a list of values
  • BETWEEN → Match within a range
  • LIKE → Match using patterns
  • IS NULL → Find null values

Practice Quiz

Test your understanding of the WHERE clause:

1. Which clause filters rows based on a condition?
2. Which operator checks if a value is within a range?
3. Which operator is used for pattern matching?
4. Which operator checks if a value is missing?
5. What does AND do in a WHERE clause?