← Back to Lessons
BEGINNER ⏱ 30 minutes

Filtering Data with WHERE

Now that you can retrieve data with SELECT, it's time to learn how to filter it! The WHERE clause is like a search filter on a shopping websiteβ€”it helps you find exactly what you're looking for without sifting through everything. This is one of the most powerful tools in SQL.


πŸ“š What You'll Learn

1. What Does WHERE Do?

The WHERE clause filters rows based on conditions you specify. Without WHERE, SELECT returns every row in the table. With WHERE, you get only the rows that match your criteria.

πŸ’‘ Real-World Example: Imagine a library with 10,000 books. Instead of looking at every single book, you tell the librarian: "Show me only books about SQL published after 2020." The WHERE clause is your librarianβ€”it does the filtering for you!

Basic Syntax

SELECT column1, column2
FROM table_name
WHERE condition;

The condition is what you're checking for. Let's explore different types of conditions!

2. Comparison Operators

Comparison operators let you compare values. Here are the six essential operators:

Operator Meaning Example
= Equal to WHERE gpa = 3.8
<> or != Not equal to WHERE major <> 'Biology'
< Less than WHERE age < 25
> Greater than WHERE gpa > 3.5
<= Less than or equal to WHERE credits <= 120
>= Greater than or equal to WHERE year >= 2020

Example: Finding Students with High GPAs

SELECT name, gpa
FROM students
WHERE gpa >= 3.7;

This query says: "Show me the name and GPA of students whose GPA is 3.7 or higher."

Result:

name gpa
Alice Johnson 3.8
Carol Davis 3.9
Diana Martinez 3.7

Notice Bob Smith (GPA 3.6) is not included because 3.6 is less than 3.7.

3. Filtering Text Values

When filtering text (strings), you must use single quotes around the value:

SELECT name, major
FROM students
WHERE major = 'Computer Science';

⚠️ Critical Rule: Text values MUST be in single quotes ('Computer Science'), but numbers should NOT have quotes (gpa = 3.8, not gpa = '3.8'). This is a common mistake!

Text is Case-Sensitive (Usually)

In most databases, text comparisons are case-sensitive:

-- Finds exact match
WHERE major = 'Biology'

βœ… Matches "Biology"

-- Won't match
WHERE major = 'biology'

❌ Won't match "Biology"

πŸ’‘ Pro Tip: To make case-insensitive searches, use the LOWER() or UPPER() function: WHERE LOWER(major) = 'biology'. We'll cover this in advanced lessons!

4. Using AND for Multiple Conditions

AND means both conditions must be true. Think of it as narrowing your search:

SELECT name, major, gpa
FROM students
WHERE major = 'Computer Science' AND gpa >= 3.5;

This query says: "Show me Computer Science majors who ALSO have a GPA of 3.5 or higher."

How AND Works (Logic Table)

Condition 1 Condition 2 Result
βœ… TRUE βœ… TRUE βœ… TRUE (row is included)
βœ… TRUE ❌ FALSE ❌ FALSE (row excluded)
❌ FALSE βœ… TRUE ❌ FALSE (row excluded)
❌ FALSE ❌ FALSE ❌ FALSE (row excluded)

Chaining Multiple AND Conditions

You can chain as many AND conditions as you need:

SELECT name, major, gpa, year
FROM students
WHERE major = 'Computer Science' 
  AND gpa >= 3.5 
  AND year = 2024;

"Show me 2024 Computer Science majors with GPA β‰₯ 3.5." All three conditions must be true!

5. Using OR for Alternative Conditions

OR means at least one condition must be true. Think of it as widening your search:

SELECT name, major
FROM students
WHERE major = 'Computer Science' OR major = 'Mathematics';

This query says: "Show me students who are EITHER Computer Science OR Mathematics majors."

How OR Works (Logic Table)

Condition 1 Condition 2 Result
βœ… TRUE βœ… TRUE βœ… TRUE (row is included)
βœ… TRUE ❌ FALSE βœ… TRUE (row is included)
❌ FALSE βœ… TRUE βœ… TRUE (row is included)
❌ FALSE ❌ FALSE ❌ FALSE (row excluded)

Combining AND with OR (Be Careful!)

When mixing AND and OR, use parentheses to be clear about order:

-- Find CS or Math majors who also have GPA >= 3.5
SELECT name, major, gpa
FROM students
WHERE (major = 'Computer Science' OR major = 'Mathematics') 
  AND gpa >= 3.5;

Without parentheses, the database might interpret it differently! The parentheses say: "First check if they're CS or Math, THEN check the GPA."

⚠️ Common Mistake: Forgetting parentheses when mixing AND/OR can give you unexpected results. When in doubt, use parentheses!

6. Using IN for Lists of Values

Instead of writing multiple OR conditions, use IN with a list:

The Long Way (using OR):

SELECT name, major
FROM students
WHERE major = 'Computer Science' 
   OR major = 'Mathematics' 
   OR major = 'Physics' 
   OR major = 'Engineering';

The Short Way (using IN):

SELECT name, major
FROM students
WHERE major IN ('Computer Science', 'Mathematics', 'Physics', 'Engineering');

Both do the same thing, but IN is cleaner and easier to read! Think of IN as "check if the value is in this list."

πŸ’‘ Pro Tip: IN also works with numbers! WHERE year IN (2023, 2024, 2025) is valid and useful.

NOT IN (Exclusion)

Use NOT IN to exclude values from the list:

SELECT name, major
FROM students
WHERE major NOT IN ('Art History', 'Music', 'Theatre');

"Show me students who are NOT majoring in Art History, Music, or Theatre."

7. Writing Range Filters with BETWEEN

BETWEEN is perfect for checking if a value falls within a range (inclusive):

The Long Way:

SELECT name, gpa
FROM students
WHERE gpa >= 3.0 AND gpa <= 3.7;

The Short Way (using BETWEEN):

SELECT name, gpa
FROM students
WHERE gpa BETWEEN 3.0 AND 3.7;

Both include the endpoints (3.0 and 3.7). BETWEEN is inclusive!

⚠️ Important: The lower value must come first! BETWEEN 3.0 AND 3.7 works, but BETWEEN 3.7 AND 3.0 returns no results.

BETWEEN Works with Dates Too!

SELECT name, enrollment_date
FROM students
WHERE enrollment_date BETWEEN '2023-01-01' AND '2023-12-31';

"Show me students who enrolled anytime in 2023."

8. Understanding NULL Values (The Tricky Part)

NULL means "no value" or "unknown"β€”not zero, not an empty string, but literally nothing. NULL is special and behaves differently!

This Does NOT Work:

-- ❌ WRONG - this won't find NULL values!
SELECT name, email
FROM students
WHERE email = NULL;

This DOES Work:

-- βœ… CORRECT - use IS NULL
SELECT name, email
FROM students
WHERE email IS NULL;

Similarly, to find rows that are NOT NULL:

SELECT name, email
FROM students
WHERE email IS NOT NULL;

πŸ’‘ Why NULL is Special: In SQL logic, NULL = NULL is not true! NULL means "unknown," and you can't compare two unknowns. That's why we need IS NULL instead of = NULL.

9. Common Beginner Mistakes

Mistake #1: Using = Instead of IS NULL

-- ❌ WRONG
WHERE email = NULL

-- βœ… CORRECT
WHERE email IS NULL

Mistake #2: Forgetting Quotes Around Text

-- ❌ WRONG
WHERE major = Biology

-- βœ… CORRECT
WHERE major = 'Biology'

Mistake #3: Using Quotes Around Numbers

-- ❌ WRONG (usually works but bad practice)
WHERE gpa = '3.8'

-- βœ… CORRECT
WHERE gpa = 3.8

Mistake #4: Incorrect BETWEEN Order

-- ❌ WRONG (returns nothing!)
WHERE gpa BETWEEN 3.7 AND 3.0

-- βœ… CORRECT (lower value first)
WHERE gpa BETWEEN 3.0 AND 3.7

Mistake #5: Forgetting Parentheses with AND/OR

-- ❌ AMBIGUOUS (might not work as expected)
WHERE major = 'CS' OR major = 'Math' AND gpa >= 3.5

-- βœ… CLEAR (parentheses show intent)
WHERE (major = 'CS' OR major = 'Math') AND gpa >= 3.5

10. 🎯 Hands-On Practice Exercises

Exercise 1: Basic Comparison

Task: Write a query to find all students with a GPA greater than 3.8.

Show Answer
SELECT *
FROM students
WHERE gpa > 3.8;

Exercise 2: Text Filtering

Task: Find all Computer Science majors. Return their name and email.

Show Answer
SELECT name, email
FROM students
WHERE major = 'Computer Science';

Exercise 3: Using AND

Task: Find Biology majors with a GPA of 3.5 or higher.

Show Answer
SELECT name, major, gpa
FROM students
WHERE major = 'Biology' AND gpa >= 3.5;

Exercise 4: Using OR

Task: Find students who are majoring in either Mathematics or Physics.

Show Answer
SELECT name, major
FROM students
WHERE major = 'Mathematics' OR major = 'Physics';

Exercise 5: Using IN

Task: Rewrite the previous query using IN instead of OR.

Show Answer
SELECT name, major
FROM students
WHERE major IN ('Mathematics', 'Physics');

Exercise 6: Using BETWEEN

Task: Find all students with GPAs between 3.0 and 3.5 (inclusive).

Show Answer
SELECT name, gpa
FROM students
WHERE gpa BETWEEN 3.0 AND 3.5;

Exercise 7: Combining AND with OR

Task: Find students who are either Computer Science OR Engineering majors AND have a GPA above 3.6.

Show Answer
SELECT name, major, gpa
FROM students
WHERE (major = 'Computer Science' OR major = 'Engineering')
  AND gpa > 3.6;

Note: The parentheses are crucial here!

Challenge Exercise: Find Missing Data

Task: Find all students who do NOT have an email address on file.

Show Answer
SELECT name, student_id
FROM students
WHERE email IS NULL;

Remember: Always use IS NULL, never = NULL!

πŸ“ Key Takeaways

πŸš€ What's Next?

Great job! You now know how to filter data precisely. Next, you'll learn how to sort your results and limit the number of rows returned. This is essential for finding "top 10" lists, pagination, and organizing your data!

Up next: ORDER BY and LIMIT - Sorting and Limiting Results

← Back to Lessons
Practice Now β†’