Skip to main content

How to Filter Rows in SQLite with WHERE

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
SELECT <columns> FROM <table> WHERE <condition>;
PartWhat 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 --version to 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:

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;

The where_filter.sql script in a macOS-style code window: a books table with three books, a SELECT of the books WHERE author = 'Jane Austen', and a SELECT of title and pages WHERE pages > 400.

  • WHERE author = 'Jane Austen' keeps the books whose author is exactly that text.
  • WHERE pages > 400 keeps the books with more than 400 pages.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
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.

Output
+------------+-------------+-------+
| title | author | pages |
+------------+-------------+-------+
| Emma | Jane Austen | 474 |
| Persuasion | Jane Austen | 249 |
+------------+-------------+-------+
+-------+-------+
| title | pages |
+-------+-------+
| Emma | 474 |
| Dune | 412 |
+-------+-------+

The output in a terminal: the books by Jane Austen, Emma and Persuasion, then the books over 400 pages, Emma at 474 and Dune at 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​

MistakeWhat happensDo this instead
Different capitals, author = 'jane austen'No rows: comparing text is case-sensitiveMatch 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​

SymptomLikely causeFix
A WHERE on text returns no rowsThe capitals or spaces don't match the stored textCompare with COLLATE NOCASE, or check the stored value with SELECT
Parse error near line 1 of where_filter.sql: table books already existsThe script already ran on library.dbDelete 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​