☰ Learn SQL Tutorial Menu

Filtering Rows with WHERE

Written by CSA mentors · Updated 16 Sept 2026 · 3 min read


WHERE filters rows so you only see the ones matching a condition — this is how you go from "show me everything" to "show me exactly what I need."

Comparison operators

OperatorMeaning
=equal to
<> or !=not equal to
> / <greater / less than
>= / <=greater or equal / less or equal
SELECT * FROM employees WHERE department_id = 1;

SELECT * FROM products WHERE price > 20000;

SELECT * FROM orders WHERE status <> 'cancelled';

Combining conditions: AND, OR, NOT

-- Both conditions must be true
SELECT * FROM employees
WHERE department_id = 1 AND salary > 100000;

-- Either condition can be true
SELECT * FROM products
WHERE category = 'Electronics' OR category = 'Furniture';

-- Negate a condition
SELECT * FROM orders WHERE NOT status = 'cancelled';

When mixing AND and OR, use parentheses to make the logic explicit — AND is evaluated before OR by default, which can surprise you:

SELECT * FROM employees
WHERE department_id = 1 AND (salary > 150000 OR job_title = 'Engineering Manager');

Matching a list: IN

SELECT * FROM employees
WHERE department_id IN (1, 3, 5);

This is shorthand for department_id = 1 OR department_id = 3 OR department_id = 5 — much easier to read.

Matching a range: BETWEEN

SELECT * FROM products
WHERE price BETWEEN 5000 AND 20000;

BETWEEN is inclusive on both ends.

Checking for missing data: IS NULL

Some employees have no manager (the department heads) — their manager_id is empty. You can't use = NULL; you must use IS NULL:

SELECT * FROM employees WHERE manager_id IS NULL;

SELECT * FROM employees WHERE manager_id IS NOT NULL;

We'll cover exactly why NULL needs special treatment in the next article.

Open the Code Playground, pick any SQL engine (SQLite, PostgreSQL, MySQL, or SQL Server), and paste these queries in directly — the practice database above is already loaded for you, every time.

Lesson 4 of 27

Sign in to track your progress and earn learning points for every lesson you finish.

Example

-- A ready-to-query practice database is loaded automatically before every run —
-- no CREATE TABLE needed. Available tables:
--   departments  (id, name, location)
--   employees    (id, first_name, last_name, email, department_id, job_title, salary, hire_date, manager_id)
--   customers    (id, customer_name, city, country, signup_date)
--   products     (id, product_name, category, price, stock_quantity)
--   orders       (id, customer_id, employee_id, order_date, status)
--   order_items  (id, order_id, product_id, quantity, unit_price)

SELECT * FROM employees WHERE department_id = 1;
Try it Yourself »