WHERE clause
WHERE keeps only the rows that satisfy a condition.
In this page:
Syntax
SELECT columns
FROM table_name
WHERE condition;
WHERE clause
Conditions compare columns to values and evaluate to true, false or NULL, and only true rows are returned. Text values use single quotes. WHERE runs before grouping and before column aliases are available. Combine conditions with AND, OR and NOT.
Note:
Use single quotes for strings and double quotes only for identifiers.
Example: WHERE clause
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, city TEXT, age INTEGER);
INSERT INTO users VALUES (1, 'Ada', 'London', 36), (2, 'Bob', 'Oslo', 25), (3, 'Cy', 'Oslo', 41), (4, 'Di', 'Rome', 30);
SELECT * FROM users WHERE city = 'Oslo';
SELECT name FROM users WHERE age > 30;
-- Output:
-- id | name | city | age
-- 2 | Bob | Oslo | 25
-- 3 | Cy | Oslo | 41
-- name
-- Ada
-- Cy
Login to try C/C++/Java/PHP code in the editor
Related Topics
Common Mistakes
- Using double quotes for string values
- Comparing to NULL with =
- Using aliases in WHERE
Chapter Summary
- WHERE filters rows
- Text needs single quotes
- NULL comparisons need IS NULL
- Only true rows are returned
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: