Lesson 2 of 10 · 9 min · 3 hands-on steps
Filtering Rows with WHERE
AND, OR, IN, BETWEEN, LIKE and NULL. Ask exactly the question you mean.
WHERE picks the rows you want
Every example below runs against a practice database. Press Run and experiment.
SELECT name, city FROM customers WHERE city = 'Lagos';
Text goes in single quotes in SQL.
Combining conditions
SELECT name, salary FROM employees WHERE department_id = 1 AND salary > 100000;
SELECT name FROM customers
WHERE city IN ('Seoul', 'Pune');SELECT name, hired FROM employees WHERE hired BETWEEN '2022-01-01' AND '2023-12-31';
Pattern matching with LIKE
| Pattern | Matches |
|---|---|
'A%' |
starts with A |
'%a' |
ends with a |
'%ar%' |
contains "ar" |
'_o' |
two letters, second is o (_ = exactly one character) |
SELECT name FROM products WHERE name LIKE '%o%';
NULL means "unknown"
A missing value is NULL. You can't compare it with =. Use IS NULL or IS NOT NULL:
SELECT 'NULL = NULL is…', NULL = NULL, NULL IS NULL;
That first comparison gives NULL (unknown), not true. It's one of the most famous SQL gotchas.
🧪 Practice: 3 steps
0 / 3 doneStep 1 of 3 · Lagos customers
Tables: customers(id, name, city).
Return the name of every customer in Lagos.
SELECT * FROM customers;
Output appears here. Press Run or Ctrl/⌘ + Enter.
Step 2 of 3 · Mid-range salaries
Tables: employees(id, name, department_id, salary, hired).
Return the name and salary of employees earning between 85,000 and 100,000 (inclusive).
SELECT name, salary FROM employees;
Output appears here. Press Run or Ctrl/⌘ + Enter.
Step 3 of 3 · Names starting with a letter
Return the name of employees whose name starts with "G" or "L". Use LIKE.
SELECT name FROM employees;
Output appears here. Press Run or Ctrl/⌘ + Enter.
✅ Check your understanding
How do you check for a missing value in SQL?
Which is the same as city = 'Lagos' OR city = 'Pune'?
Create a free account to track your progress.