WHERE Clause - Filtering Data
Filter query results using the WHERE clause.
Introduction to WHERE Clause
In the previous lesson, we learned to retrieve data with SELECT. But what if you don't want all the rows? What if you only want employees in Engineering, or products under $50, or customers in California?
That's where the WHERE clause comes in! It's like a filter that lets only certain rows pass through.
A SQL clause that filters rows based on a condition. Only rows where the condition is TRUE are included in the result.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Let's work with our employees table:
id | first_name | last_name | department | salary | hire_date |
|---|---|---|---|---|---|
| 1 | Alice | Johnson | Engineering | 75000 | 2022-01-15 |
| 2 | Bob | Smith | Marketing | 65000 | 2021-06-20 |
| 3 | Charlie | Davis | Engineering | 85000 | 2020-03-10 |
| 4 | Diana | Wilson | Sales | 70000 | 2022-08-01 |
| 5 | Eve | Martinez | Marketing | 68000 | 2021-11-12 |
| 6 | Frank | Brown | Engineering | 72000 | 2023-02-28 |
| 7 | Grace | Lee | Sales | 73000 | 2021-09-15 |
Basic WHERE with Equality
The simplest WHERE condition uses = (equals) to match exact values.
SELECT first_name, last_name, department
FROM employees
WHERE department = 'Engineering';Only rows where department = 'Engineering' are returned. The other rows are filtered out!
Important: Text values must be enclosed in single quotes ('...'). Numbers don't need quotes.
SELECT first_name, last_name, salary
FROM employees
WHERE salary = 70000;Tip: SQL is case-insensitive for keywords (SELECT, WHERE, FROM), but string comparisons are usually case-sensitive! 'Engineering' ≠ 'engineering'
Comparison Operators
WHERE conditions can use various comparison operators to filter data:
Operator | Meaning | Example | Reads as |
|---|---|---|---|
| `=` | Equal to | `salary = 70000` | "salary equals 70000" |
| `!=` or `<>` | Not equal to | `department != 'Sales'` | "department is not Sales" |
| `>` | Greater than | `salary > 70000` | "salary greater than 70000" |
| `<` | Less than | `salary < 70000` | "salary less than 70000" |
| `>=` | Greater than or equal | `salary >= 70000` | "salary 70000 or more" |
| `<=` | Less than or equal | `salary <= 70000` | "salary 70000 or less" |
Greater than (>):
SELECT first_name, salary
FROM employees
WHERE salary > 70000;Less than or equal (<=):
SELECT first_name, salary, hire_date
FROM employees
WHERE salary <= 68000;Not equal (!= or <>):
SELECT first_name, department
FROM employees
WHERE department != 'Engineering';Note: Both != and <> mean "not equal." They're interchangeable - use whichever you prefer! Most SQL dialects support both.
Filtering with Dates
Date comparisons work just like numbers! Dates must be in quotes, typically in 'YYYY-MM-DD' format.
-- Employees hired after January 1, 2022
SELECT first_name, last_name, hire_date
FROM employees
WHERE hire_date > '2022-01-01';-- Employees hired in 2021 or earlier
SELECT first_name, hire_date
FROM employees
WHERE hire_date <= '2021-12-31';Best Practice: Always use the standard date format 'YYYY-MM-DD' (e.g., '2024-03-15') for maximum compatibility across different SQL databases.
Filtering Text Data
When filtering text, remember that string comparisons are usually case-sensitive.
SELECT first_name, last_name
FROM employees
WHERE last_name = 'Smith';This would return nothing (case mismatch):
SELECT first_name, last_name
FROM employees
WHERE last_name = 'smith'; -- lowercase 's'Case-insensitive comparison (if needed):
SELECT first_name, last_name
FROM employees
WHERE LOWER(last_name) = 'smith';LOWER() converts the column value to lowercase before comparing. There's also UPPER() for uppercase comparisons.
Working with NULL Values
Remember that NULL means "no value" or "unknown." NULL is special - you can't use = or != with it!
Let's add a column to track employees' managers:
id | first_name | department | manager_id |
|---|---|---|---|
| 1 | Alice | Engineering | 3 |
| 2 | Bob | Marketing | 5 |
| 3 | Charlie | Engineering | NULL |
| 4 | Diana | Sales | 7 |
| 5 | Eve | Marketing | NULL |
| 6 | Frank | Engineering | 3 |
| 7 | Grace | Sales | NULL |
Finding NULL values - Use IS NULL:
-- Employees with no manager (CEOs, executives)
SELECT first_name, department, manager_id
FROM employees
WHERE manager_id IS NULL;Finding non-NULL values - Use IS NOT NULL:
-- Employees who have a manager
SELECT first_name, department, manager_id
FROM employees
WHERE manager_id IS NOT NULL;Common mistake: Using = NULL or != NULL doesn't work!
❌ Wrong: WHERE manager_id = NULL
✅ Correct: WHERE manager_id IS NULL
❌ Wrong: WHERE manager_id != NULL
✅ Correct: WHERE manager_id IS NOT NULL
NULL is special - you must use IS NULL or IS NOT NULL.
WHERE Clause Execution Order
Understanding how SQL processes your query helps you write better queries:
Step | Clause | What Happens |
|---|---|---|
| 1 | FROM | Database locates the table |
| 2 | WHERE | Rows are filtered based on condition |
| 3 | SELECT | Specified columns are retrieved |
This means:
- WHERE filters rows before SELECT picks columns
- You can filter on columns that aren't in your SELECT list
- WHERE happens on the raw data in the table
-- Filter by salary, but don't display salary
SELECT first_name, department
FROM employees
WHERE salary > 70000;Even though salary isn't in the SELECT list, we can still filter by it in WHERE!
Practical Examples
Example 1: Finding high earners
SELECT first_name, last_name, salary, department
FROM employees
WHERE salary >= 73000;Example 2: Recent hires
SELECT first_name, hire_date, department
FROM employees
WHERE hire_date >= '2022-01-01';Example 3: Specific department employees
SELECT first_name, last_name, salary
FROM employees
WHERE department = 'Sales';Common Mistakes to Avoid
Don't make these mistakes:
-
Forgetting quotes around text:
WHERE department = Engineering❌- Correct:
WHERE department = 'Engineering'✓
- Correct:
-
Using quotes around numbers:
WHERE salary = '70000'❌- Correct:
WHERE salary = 70000✓ (though '70000' often works, it's not ideal)
- Correct:
-
Using = with NULL:
WHERE manager_id = NULL❌- Correct:
WHERE manager_id IS NULL✓
- Correct:
-
Wrong date format:
WHERE hire_date = '01/15/2022'❌- Correct:
WHERE hire_date = '2022-01-15'✓
- Correct:
-
Case sensitivity issues:
WHERE department = 'engineering'❌- Correct:
WHERE department = 'Engineering'✓ (match the actual case)
- Correct:
-
Mixing single and double quotes:
WHERE name = "Alice"❌- Correct:
WHERE name = 'Alice'✓ (use single quotes for strings)
- Correct:
Key Takeaways
What you learned:
✅ WHERE filters rows based on conditions
✅ Comparison operators: =, !=, >, <, >=, <=
✅ Text values need single quotes: 'Engineering'
✅ Numbers don't need quotes: 70000
✅ Dates use quotes in YYYY-MM-DD format: '2022-01-15'
✅ NULL checks use IS NULL or IS NOT NULL (not = or !=)
✅ WHERE executes before SELECT
✅ You can filter by columns not in your SELECT list
Coming up next: In the next lesson, we'll learn how to combine multiple conditions with AND, OR, and NOT operators to create more powerful filters!
Practice Exercise: Try writing WHERE clauses to:
- Find employees in Marketing
- Find employees hired before 2022
- Find employees with salaries between 65000 and 75000
- Find employees without a manager (manager_id IS NULL)