Filtering with WHERE
Progress Tracking
Log in to save this lesson and continue from where you left off.
Now It Gets Useful
So far, your queries return every row in a table. That's fine for small datasets, but in the real world, you're usually looking for something specific. Which employees are in the HR department? What orders were placed in March? Which users signed up this week?
This is what WHERE is for. It filters your results to only the rows that match certain conditions.
Basic WHERE Syntax
WHERE goes after FROM and before ORDER BY. You specify a condition, and SQL returns only the rows where that condition is true.
SELECT column1, column2
FROM table_name
WHERE condition;Get all employees in the HR department:
SELECT
first_name,
last_name,
department
FROM techcorp_workforce
WHERE department = 'HR';Filtering Text Values
When filtering text (strings), wrap the value in single quotes:
WHERE department = 'Admin'
WHERE first_name = 'Monika'
WHERE order_details = 'Shoes'SQL uses single quotes for text values. Double quotes are for column aliases and identifiers. Mixing them up is a common source of errors.
Practice: Basic WHERE
Let's practice on a real question.
| serial_number | activity_date | facility_name | score | grade | service_code | service_description | employee_id | facility_address | facility_city | facility_id | facility_state | facility_zip | owner_id | owner_name | pe_description | program_element_pe | program_name | program_status | record_id |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| DAQHRSETQ | 2017-06-08 | MARGARITAS CAFE | 93 | A | 1 | ROUTINE INSPECTION | EE0000006 | 5026 S CRENSHAW BLVD | LOS ANGELES | FA0023656 | CA | 90043 | OW0004133 | BAZAN, ASCENCION | RESTAURANT (61-150) SEATS HIGH RISK | 1638 | MARGARITAS CAFE | ACTIVE | PR0011718 |
| DA2GQRJOS | 2017-03-07 | LAS MOLENDERAS | 97 | A | 1 | ROUTINE INSPECTION | EE0000997 | 2635 WHITTIER BLVD | LOS ANGELES | FA0160416 | CA | 90023 | OW0125379 | MARISOL FEREGRINO | RESTAURANT (0-30) SEATS HIGH RISK | 1632 | LAS MOLENDERAS | INACTIVE | PR0148504 |
| DAMQTA46T | 2016-03-22 | SANDRA'S TAMALES | 93 | A | 1 | ROUTINE INSPECTION | EE0001049 | 5390 WHITTIER BLVD | LOS ANGELES | FA0171769 | CA | 90022-4032 | OW0178828 | SANDRA'S TAMALES INC. | RESTAURANT (0-30) SEATS MODERATE RISK | 1631 | SANDRA'S TAMALES | ACTIVE | PR0164225 |
| DAXMBTIRZ | 2018-02-12 | CAFE GRATITUDE | 97 | A | 1 | ROUTINE INSPECTION | EE0000828 | 639 N LARCHMONT BLVD STE #102 | LOS ANGELES | FA0058921 | CA | 90004 | OW0005704 | CAFE GRATITUDE LARCHMONT LLC | RESTAURANT (61-150) SEATS HIGH RISK | 1638 | CAFE GRATITUDE | ACTIVE | PR0019854 |
| DAK8TBMS0 | 2015-09-10 | THE WAFFLE | 90 | A | 1 | ROUTINE INSPECTION | EE0000709 | 6255 W SUNSET BLVD STE #105 | LOS ANGELES | FA0051830 | CA | 90028 | OW0035796 | THE WAFFLE, LLC | RESTAURANT (61-150) SEATS HIGH RISK | 1638 | THE WAFFLE | ACTIVE | PR0010922 |
Find all inspection details made for facilities owned by 'GLASSELL COFFEE SHOP LLC'.
Comparison Operators
The equals sign = is just one way to compare values. Here are all the comparison operators:
=equals — example:WHERE department = 'HR'finds matches<>or!=not equal — example:WHERE department <> 'HR'excludes HR>greater than — example:WHERE salary > 100000finds high earners<less than — example:WHERE salary < 50000finds lower salaries>=greater than or equal — example:WHERE salary >= 100000<=less than or equal — example:WHERE salary <= 100000
Both <> and != mean "not equal." They work the same way. Use whichever your team prefers, but be consistent.
Filtering Numbers
Numbers don't need quotes.
| id | first_name | last_name | department | salary | phone_number | joining_date |
|---|---|---|---|---|---|---|
| 1 | Sarah | Mitchell | HR | 95000 | 555-0101 | 2021-03-15 |
| 2 | Michael | Chen | HR | 88000 | 555-0102 | 2022-06-01 |
| 3 | Emily | Rodriguez | HR | 82500 | 2021-09-20 | |
| 4 | David | Park | HR | 80000 | 555-0104 | 2023-01-10 |
| 5 | Lisa | Thompson | HR | 65000 | 2021-04-05 |
Find employees earning more than 100,000.
What about employees earning exactly 130,000?
Filtering Dates
Dates need single quotes, just like text. The format depends on your database, but 'YYYY-MM-DD' works in most systems:
| id | first_name | last_name | department | salary | phone_number | joining_date |
|---|---|---|---|---|---|---|
| 1 | Sarah | Mitchell | HR | 95000 | 555-0101 | 2021-03-15 |
| 2 | Michael | Chen | HR | 88000 | 555-0102 | 2022-06-01 |
| 3 | Emily | Rodriguez | HR | 82500 | 2021-09-20 | |
| 4 | David | Park | HR | 80000 | 555-0104 | 2023-01-10 |
| 5 | Lisa | Thompson | HR | 65000 | 2021-04-05 |
Find employees who joined after January 1, 2022.
Date comparisons use chronological order. '2023-01-15' > '2023-01-01' is true because January 15 comes after January 1.
Filtering the Orders Table
Let's practice with the orders table:
| id | cust_id | order_date | order_details | total_order_cost |
|---|---|---|---|---|
| 1 | 3 | 2019-03-04 | Coat | 100 |
| 2 | 3 | 2019-03-01 | Shoes | 80 |
| 3 | 3 | 2019-03-07 | Skirt | 30 |
| 4 | 7 | 2019-02-01 | Coat | 25 |
| 5 | 7 | 2019-03-10 | Shoes | 80 |
Find all orders with a total cost over $50.
Or orders for a specific product:
Find all coat orders.
Common Mistakes
Using double quotes for text values
-- This will give an error (or behave unexpectedly):
WHERE department = "HR"
-- Correct:
WHERE department = 'HR'Forgetting quotes around text
-- This will give an error:
WHERE department = HR
-- SQL thinks HR is a column name, not a value
-- Correct:
WHERE department = 'HR'Adding quotes around numbers
-- This might work, but it's wrong:
WHERE salary > '100000'
-- You're comparing text, not numbers
-- Correct:
WHERE salary > 100000Case sensitivity
This one depends on your database. In PostgreSQL, text comparisons are case-sensitive by default:
WHERE department = 'HR' -- matches 'HR'
WHERE department = 'hr' -- does NOT match 'HR'We'll cover case-insensitive matching later with LOWER() and ILIKE.
Using aliases in WHERE
Column aliases defined in SELECT aren't available in WHERE. SQL processes WHERE before SELECT.
-- This will give an error:
SELECT salary * 0.1 AS bonus
FROM techcorp_workforce
WHERE bonus > 5000;
-- Can't use alias here!
-- Correct: repeat the calculation
SELECT salary * 0.1 AS bonus
FROM techcorp_workforce
WHERE salary * 0.1 > 5000;Key Takeaways
WHEREfilters rows based on conditions.- Text values need single quotes, numbers don't.
- Use
=,<>,>,<,>=,<=to compare values. - Date comparisons work chronologically.
- Watch out for case sensitivity with text.
What's Next
One condition is useful, but real questions often need multiple conditions — like finding HR employees earning over 80,000, or finding orders from January or February. In the next lesson, you'll learn to combine conditions with AND, OR, and NOT.