The WHERE Clause

The WHERE clause filters rows returned by a query. Think of it as asking: "Show me only the rows where this condition is true."

Basic Syntax

SELECT column1, column2
FROM table_name
WHERE condition;

Comparison Operators

Operator Meaning Example
= Equal to WHERE department = 'Engineering'
<> or != Not equal to WHERE department <> 'Sales'
< Less than WHERE salary < 80000
> Greater than WHERE salary > 90000
<= Less than or equal to WHERE salary <= 72000
>= Greater than or equal to WHERE salary >= 95000

Examples

Find all engineers:

SELECT name, salary
FROM employees
WHERE department = 'Engineering';

Find high earners (salary over $90,000):

SELECT name, department, salary
FROM employees
WHERE salary > 90000;

Find employees hired after 2020:

SELECT name, hire_date
FROM employees
WHERE hire_date > '2020-01-01';

Important: Text Values Use Single Quotes

In SQL, text values must be wrapped in single quotes:

  • Correct: WHERE department = 'Engineering'
  • Wrong: WHERE department = Engineering
  • Wrong: WHERE department = "Engineering" (double quotes are for column names)

NULL Values

NULL means "no value" or "unknown." You cannot use = to check for NULL:

-- This WON'T work:
SELECT * FROM employees WHERE manager_id = NULL;

-- Use IS NULL instead:
SELECT * FROM employees WHERE manager_id IS NULL;

-- Or IS NOT NULL:
SELECT * FROM employees WHERE manager_id IS NOT NULL;

Try It Yourself

  1. Find all employees in the Marketing department
  2. Find employees earning less than $75,000
  3. Find employees who are not active (is_active = false)
  4. Find employees hired before 2020
Take Quiz Next Lesson
SQL Editor Ctrl+Enter to run
Results
Run a query to see results here
Tables: