How to Match Rows on Two Conditions in SQLite with AND
Sometimes one condition isn't enough: you want the Jane Austen books, but only the long ones. AND joins two conditions in a WHERE, and a row has to pass both. This guide finds them, with real output from SQLite 3.54.1.
Syntax
SELECT <columns> FROM <table> WHERE <condition 1> AND <condition 2>;
| Part | What it means |
|---|---|
<condition 1> AND <condition 2> | True only when both conditions are true |
<condition 1> OR <condition 2> | True when either one is, or both |
When a WHERE mixes them, AND is worked out before OR. SQLite's documentation lists its operators "in precedence order (top to bottom / highest to lowest)", with AND just above OR.
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 and_conditions.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' AND pages > 300;

author = 'Jane Austen'alone matches Emma and Persuasion.pages > 300alone matches Emma and Dune.- With
AND, a book must match both.
Step 2: Run It
In the folder that holds the script, run:
sqlite3 -table library.db ".read and_conditions.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 |
+-------+-------------+-------+

Only Emma passed both conditions. Persuasion is by Jane Austen but too short, and Dune is long but by another author.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Mixing OR and AND without parentheses: author = 'Jane Austen' OR author = 'Frank Herbert' AND pages > 450 | Emma and Persuasion came back. AND went first, so it read as "by Jane Austen, or by Frank Herbert and over 450 pages" | Add parentheses: (author = 'Jane Austen' OR author = 'Frank Herbert') AND pages > 450 returned only Emma |
AND between two values of one column, author = 'Jane Austen' AND author = 'Frank Herbert' | No rows: no book has both authors | Use OR for "either one" |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
A WHERE with AND and OR returns too many rows | AND was worked out before OR | Put parentheses around the OR part |
Parse error near line 1 of and_conditions.sql: table books already exists | The script already ran on library.db | Delete library.db and run the script again |
FAQ
Is there a shorter way to write a range?
Yes, BETWEEN: WHERE pages BETWEEN 249 AND 412 returned Persuasion and Dune, so both ends count. The documentation says x BETWEEN y AND z "is equivalent to" x>=y AND x<=z, "except that with BETWEEN, the x expression is only evaluated once."
Can I join more than two conditions?
Yes. Each extra AND adds one more condition that every row must pass: WHERE author = 'Jane Austen' AND pages > 200 AND pages < 300 returned Persuasion.
Conclusion
WHERE author = 'Jane Austen' AND pages > 300 kept only Emma, the one book that passed both conditions.
Additional Resources
- Operators — every operator, in precedence order.
- The BETWEEN operator — ranges in one condition.
