Skip to main content

How to Match Rows on Two Conditions in SQLite with AND

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
SELECT <columns> FROM <table> WHERE <condition 1> AND <condition 2>;
PartWhat 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 --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 and_conditions.sql:

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;

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

  • author = 'Jane Austen' alone matches Emma and Persuasion.
  • pages > 300 alone matches Emma and Dune.
  • With AND, a book must match both.

Step 2: Run It​

In the folder that holds the script, run:

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

Output
+-------+-------------+-------+
| title | author | pages |
+-------+-------------+-------+
| Emma | Jane Austen | 474 |
+-------+-------------+-------+

The output in a terminal: one row, Emma by Jane Austen with 474 pages.

Only Emma passed both conditions. Persuasion is by Jane Austen but too short, and Dune is long but by another author.

Common Mistakes​

MistakeWhat happensDo this instead
Mixing OR and AND without parentheses: author = 'Jane Austen' OR author = 'Frank Herbert' AND pages > 450Emma 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 authorsUse OR for "either one"

Troubleshooting​

SymptomLikely causeFix
A WHERE with AND and OR returns too many rowsAND was worked out before ORPut parentheses around the OR part
Parse error near line 1 of and_conditions.sql: table books already existsThe script already ran on library.dbDelete 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​