Module 1: SQL Foundations45 min

Filtering with WHERE

Progress Tracking

Log in to save this lesson and continue from where you left off.

Log in

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.

SQL
SELECT column1, column2
FROM table_name
WHERE condition;

Get all employees in the HR department:

SQL
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:

SQL
WHERE department = 'Admin'
WHERE first_name = 'Monika'
WHERE order_details = 'Shoes'
Single Quotes for Text Values

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.

Table: los_angeles_restaurant_health_inspections
serial_numberactivity_datefacility_namescoregradeservice_codeservice_descriptionemployee_idfacility_addressfacility_cityfacility_idfacility_statefacility_zipowner_idowner_namepe_descriptionprogram_element_peprogram_nameprogram_statusrecord_id
DAQHRSETQ2017-06-08MARGARITAS CAFE93A1ROUTINE INSPECTIONEE00000065026 S CRENSHAW BLVDLOS ANGELESFA0023656CA90043OW0004133BAZAN, ASCENCIONRESTAURANT (61-150) SEATS HIGH RISK1638MARGARITAS CAFEACTIVEPR0011718
DA2GQRJOS2017-03-07LAS MOLENDERAS97A1ROUTINE INSPECTIONEE00009972635 WHITTIER BLVDLOS ANGELESFA0160416CA90023OW0125379MARISOL FEREGRINORESTAURANT (0-30) SEATS HIGH RISK1632LAS MOLENDERASINACTIVEPR0148504
DAMQTA46T2016-03-22SANDRA'S TAMALES93A1ROUTINE INSPECTIONEE00010495390 WHITTIER BLVDLOS ANGELESFA0171769CA90022-4032OW0178828SANDRA'S TAMALES INC.RESTAURANT (0-30) SEATS MODERATE RISK1631SANDRA'S TAMALESACTIVEPR0164225
DAXMBTIRZ2018-02-12CAFE GRATITUDE97A1ROUTINE INSPECTIONEE0000828639 N LARCHMONT BLVD STE #102LOS ANGELESFA0058921CA90004OW0005704CAFE GRATITUDE LARCHMONT LLCRESTAURANT (61-150) SEATS HIGH RISK1638CAFE GRATITUDEACTIVEPR0019854
DAK8TBMS02015-09-10THE WAFFLE90A1ROUTINE INSPECTIONEE00007096255 W SUNSET BLVD STE #105LOS ANGELESFA0051830CA90028OW0035796THE WAFFLE, LLCRESTAURANT (61-150) SEATS HIGH RISK1638THE WAFFLEACTIVEPR0010922
1
Inspection For Glassell Coffee Shop
View solution

Find all inspection details made for facilities owned by 'GLASSELL COFFEE SHOP LLC'.

Tables: los_angeles_restaurant_health_inspections

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 > 100000 finds high earners
  • < less than — example: WHERE salary < 50000 finds lower salaries
  • >= greater than or equal — example: WHERE salary >= 100000
  • <= less than or equal — example: WHERE salary <= 100000
<> and != Are the Same

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.

Table: techcorp_workforce
idfirst_namelast_namedepartmentsalaryphone_numberjoining_date
1SarahMitchellHR95000555-01012021-03-15
2MichaelChenHR88000555-01022022-06-01
3EmilyRodriguezHR825002021-09-20
4DavidParkHR80000555-01042023-01-10
5LisaThompsonHR650002021-04-05
2
Filter Employees by Salary

Find employees earning more than 100,000.

Tables: techcorp_workforce

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:

Table: techcorp_workforce
idfirst_namelast_namedepartmentsalaryphone_numberjoining_date
1SarahMitchellHR95000555-01012021-03-15
2MichaelChenHR88000555-01022022-06-01
3EmilyRodriguezHR825002021-09-20
4DavidParkHR80000555-01042023-01-10
5LisaThompsonHR650002021-04-05
3
Filter Employees by Join Date

Find employees who joined after January 1, 2022.

Tables: techcorp_workforce
Date Comparison is Chronological

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:

Table: orders
idcust_idorder_dateorder_detailstotal_order_cost
132019-03-04Coat100
232019-03-01Shoes80
332019-03-07Skirt30
472019-02-01Coat25
572019-03-10Shoes80
4
Find High-Value Orders

Find all orders with a total cost over $50.

Tables: orders

Or orders for a specific product:

5
Find Orders by Product

Find all coat orders.

Tables: orders

Common Mistakes

Using double quotes for text values

SQL
-- This will give an error (or behave unexpectedly):
WHERE department = "HR"

-- Correct:
WHERE department = 'HR'

Forgetting quotes around text

SQL
-- 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

SQL
-- This might work, but it's wrong:
WHERE salary > '100000'
-- You're comparing text, not numbers

-- Correct:
WHERE salary > 100000

Case sensitivity

This one depends on your database. In PostgreSQL, text comparisons are case-sensitive by default:

SQL
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.

SQL
-- 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

  • WHERE filters 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.