☰ Learn SQL Tutorial Menu

Filtering Rows with WHERE

Written by Faisal Adnan · Updated 14 Oct 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);

Result: first 5 of 13 rows (the order can differ on other engines unless you add ORDER BY)

idfirst_namelast_nameemaildepartment_idjob_titlesalaryhire_datemanager_id
1AliSheikhali.sheikh1@csa-demo.local1Engineering Manager1880002024-10-16NULL
3AhmedQureshiahmed.qureshi3@csa-demo.local3SEO Specialist1010002023-12-28NULL
5BilalAhmedbilal.ahmed5@csa-demo.local5Finance Manager1220002020-06-19NULL
7UsmanAhmedusman.ahmed7@csa-demo.local1Engineering Manager1280002020-11-061
9HassanShahhassan.shah9@csa-demo.local3SEO Specialist980002022-01-023

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;

Result: first 5 of 6 rows (the order can differ on other engines unless you add ORDER BY)

idproduct_namecategorypricestock_quantity
2Mechanical KeyboardElectronics650060
5Office ChairFurniture1850018
13Noise Cancelling HeadphonesElectronics15500140
14WhiteboardOffice Supplies5200118
16External SSD 1TBElectronics1350081

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 XP for every lesson you finish.

Practice tip: the built-in code runner is being upgraded. Until it is back, copy the examples into any SQL or Python tool you have (for example SQLite, PostgreSQL, MySQL, SQL Server or a free online editor) and run them there. You can keep reading and earning XP here.