How to Search Text in SQLite with LIKE
To find every book with "moon" in its title, you need a pattern, not an exact match. LIKE compares text with a pattern in which % stands for any run of characters and _ for exactly one. This guide searches book titles three ways, with real output from SQLite 3.54.1.
Syntax
SELECT <columns> FROM <table> WHERE <column> LIKE '<pattern>';
| Part | What it means |
|---|---|
LIKE '<pattern>' | True when the text matches the pattern. English letters match whatever their capitals |
% in the pattern | Any run of characters, including none |
_ in the pattern | Exactly one character |
SQLite's documentation on LIKE: "A percent symbol ("%") in the LIKE pattern matches any sequence of zero or more characters in the string. An underscore ("_") in the LIKE pattern matches any single character in the string."
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 like_search.sql:
CREATE TABLE books (
title TEXT NOT NULL
);
INSERT INTO books (title) VALUES ('Emma');
INSERT INTO books (title) VALUES ('Moonfleet');
INSERT INTO books (title) VALUES ('The Moonstone');
INSERT INTO books (title) VALUES ('Dune');
SELECT title FROM books WHERE title LIKE '%moon%';
SELECT title FROM books WHERE title LIKE 'D%';
SELECT title FROM books WHERE title LIKE '_mma';

'%moon%'matches "moon" anywhere in the title.'D%'matches titles that start with D.'_mma'matches one character followed by "mma".
Step 2: Run It
In the folder that holds the script, run:
sqlite3 -table library.db ".read like_search.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 |
+---------------+
| Moonfleet |
| The Moonstone |
+---------------+
+-------+
| title |
+-------+
| Dune |
+-------+
+-------+
| title |
+-------+
| Emma |
+-------+

'%moon%' found Moonfleet and The Moonstone, although both spell it with a capital M: LIKE ignores the capitals of English letters. 'D%' found Dune, and '_mma' found Emma.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
A pattern without %, as in LIKE 'moon' | No rows: without a wildcard, the whole title has to be "moon" | '%moon%' for "anywhere", or 'moon%' for "starts with", which returned Moonfleet |
Searching for a real percent sign with LIKE '%50%%' | It matched 500 off as well as 50% off, because the % was a wildcard | Escape it: LIKE '%50\%%' ESCAPE '\' matched only 50% off |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
A search misses accented letters in other capitals: 'Émile' LIKE 'émile' returned 0 | LIKE folds capitals only for ASCII letters | Type accented letters with the capitals they're stored with |
Parse error near line 1 of like_search.sql: table books already exists | The script already ran on library.db | Delete library.db and run the script again |
FAQ
Is LIKE case-sensitive?
Not for English letters: 'Emile' LIKE 'emile' returned 1. It is for letters outside ASCII: 'Émile' LIKE 'émile' returned 0. The documentation: "SQLite only understands upper/lower case for ASCII characters by default."
What is GLOB?
A search like LIKE that uses Unix-style wildcards, such as *, and respects capitals: 'The Moonstone' GLOB '*Moon*' returned 1, and 'The Moonstone' GLOB '*moon*' returned 0. The documentation: "The GLOB operator is similar to LIKE but uses the Unix file globbing syntax for its wildcards. Also, GLOB is case sensitive, unlike LIKE."
Conclusion
LIKE '%moon%' found both "moon" titles whatever their capitals, 'D%' found the one that starts with D, and '_mma' found Emma.
Additional Resources
- The LIKE operator — wildcards,
ESCAPEand case. - The GLOB operator — the case-sensitive alternative.
