Beginner · 12 min
Filtering rows
Use comparisons to select rows that meet a condition.
Sample database
CREATE TABLE groups (id INTEGER PRIMARY KEY, label TEXT NOT NULL);
CREATE TABLE members (id INTEGER PRIMARY KEY, name TEXT NOT NULL, city TEXT, group_id INTEGER REFERENCES groups(id));
CREATE TABLE sessions (id INTEGER PRIMARY KEY, member_id INTEGER NOT NULL REFERENCES members(id), topic TEXT NOT NULL, minutes INTEGER NOT NULL CHECK(minutes > 0), completed INTEGER NOT NULL CHECK(completed IN (0, 1)), day TEXT NOT NULL);
INSERT INTO groups VALUES (1, 'Morning'), (2, 'Evening');
INSERT INTO members VALUES (1, 'Ada', 'Oslo', 1), (2, 'Bo', 'Rome', 1), (3, 'Cy', NULL, 2), (4, 'Dee', 'Oslo', 2);
INSERT INTO sessions VALUES
(1, 1, 'Travel', 20, 1, '2026-01-01'),
(2, 1, 'Food', 10, 0, '2026-01-02'),
(3, 2, 'Travel', 30, 1, '2026-01-01'),
(4, 2, 'Music', 15, 1, '2026-01-03'),
(5, 3, 'Music', 25, 0, '2026-01-02'),
(6, 3, 'Travel', 15, 1, '2026-01-04');Compare numeric values
WHERE keeps rows whose condition is true. Numeric comparisons work with numeric values; quote text literals but do not turn numeric thresholds into text unnecessarily. The sample durations are positive whole minutes.
SELECT id, minutes FROM sessions WHERE minutes >= 25 ORDER BY id;Output
id | minutes 3 | 30 5 | 25
Match a text value
Equality compares a column with a value. Text matching can depend on collation, so do not assume every database ignores case. This example uses the exact spelling stored in the sample table.
SELECT id FROM sessions WHERE topic = 'Travel' ORDER BY id;Output
id 1 3 6
Put it into practice
- Read the sample tables and predict the result before running the query.
- Solve the task, compare the returned rows, and explain why the solution works.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Run queries on a fresh sample database. Table changes last for this run only; your query draft is saved separately. Results show column names followed by rows. NULL means a missing value.
Apply what you learned
Return session IDs and minutes for sessions lasting at least 20 minutes, ordered by ID.
The threshold includes sessions lasting exactly 20 minutes.
Show solution
SELECT id, minutes FROM sessions WHERE minutes >= 20 ORDER BY id;The inclusive comparison includes the boundary value as well as longer sessions.