How to Filter Rows in SQLite with WHERE
Most of the time you want some rows, not all of them: the books by one author, or the long ones. WHERE keeps only the rows whose condition is true. This guide filters a table of books by author and by page count, with real output from SQLite 3.54.1.
Syntax
SELECT <columns> FROM <table> WHERE <condition>;
| Part | What it means |
|---|---|
WHERE <condition> | Keeps only the rows for which the condition is true |
<condition> | A comparison, such as author = 'Jane Austen' or pages > 400. Text goes in single quotes |
The comparisons are =, != (or <>), <, <=, > and >=. SQLite's documentation on WHERE: "Only rows for which the WHERE clause expression evaluates to true are included from the dataset before continuing."
Prerequisites
- The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run
sqlite3 --versionto see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.
Step 1: Write the Script
Save this as where_filter.sql:
CREATE TABLE books (
title TEXT NOT NULL,
author TEXT NOT NULL,
pages INTEGER NOT NULL
);
INSERT INTO books (title, author, pages) VALUES ('Emma', 'Jane Austen', 474);
INSERT INTO books (title, author, pages) VALUES ('Persuasion', 'Jane Austen', 249);
INSERT INTO books (title, author, pages) VALUES ('Dune', 'Frank Herbert', 412);
SELECT * FROM books WHERE author = 'Jane Austen';
SELECT title, pages FROM books WHERE pages > 400;

WHERE author = 'Jane Austen'keeps the books whose author is exactly that text.WHERE pages > 400keeps the books with more than 400 pages.
Step 2: Run It
In the folder that holds the script, run:
sqlite3 -table library.db ".read where_filter.sql"
sqlite3 opens the database file library.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.
+------------+-------------+-------+
| title | author | pages |
+------------+-------------+-------+
| Emma | Jane Austen | 474 |
| Persuasion | Jane Austen | 249 |
+------------+-------------+-------+
+-------+-------+
| title | pages |
+-------+-------+
| Emma | 474 |
| Dune | 412 |
+-------+-------+

The first WHERE kept Jane Austen's two books, and the second kept the two over 400 pages. Persuasion, at 249 pages, didn't pass it.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Different capitals, author = 'jane austen' | No rows: comparing text is case-sensitive | Match the capitals, or add COLLATE NOCASE: WHERE author = 'jane austen' COLLATE NOCASE returned both books |
Text in double quotes, author = "Jane Austen" | no such column: "Jane Austen" - should this be a string literal in single-quotes? | Use single quotes: 'Jane Austen' |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
A WHERE on text returns no rows | The capitals or spaces don't match the stored text | Compare with COLLATE NOCASE, or check the stored value with SELECT |
Parse error near line 1 of where_filter.sql: table books already exists | The script already ran on library.db | Delete library.db and run the script again |
FAQ
Can a WHERE use two conditions?
Yes, joined with AND or OR. WHERE author = 'Jane Austen' AND pages > 300 returned only Emma.
Does WHERE work outside SELECT?
Yes: UPDATE and DELETE take a WHERE too, which picks the rows they change. UPDATE books SET pages = 475 WHERE title = 'Emma' changed only Emma, and DELETE FROM books WHERE title = 'Dune' removed only Dune.
Conclusion
WHERE author = 'Jane Austen' kept two of the three books, and WHERE pages > 400 kept the two long ones.
Additional Resources
- WHERE clause filtering — how SQLite applies the condition.
- SQL Language Expressions — every comparison and operator.
