Skip to main content

How to Search Text in SQLite with LIKE

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
SELECT <columns> FROM <table> WHERE <column> LIKE '<pattern>';
PartWhat it means
LIKE '<pattern>'True when the text matches the pattern. English letters match whatever their capitals
% in the patternAny run of characters, including none
_ in the patternExactly 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 --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 like_search.sql:

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';

The like_search.sql script in a macOS-style code window: a books table with Emma, Moonfleet, The Moonstone and Dune, and three SELECTs of the titles LIKE '%moon%', LIKE 'D%' and 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:

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

Output
+---------------+
| title |
+---------------+
| Moonfleet |
| The Moonstone |
+---------------+
+-------+
| title |
+-------+
| Dune |
+-------+
+-------+
| title |
+-------+
| Emma |
+-------+

The output in a terminal: Moonfleet and The Moonstone for the first pattern, Dune for the second and Emma for the third.

'%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​

MistakeWhat happensDo 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 wildcardEscape it: LIKE '%50\%%' ESCAPE '\' matched only 50% off

Troubleshooting​

SymptomLikely causeFix
A search misses accented letters in other capitals: 'Émile' LIKE 'émile' returned 0LIKE folds capitals only for ASCII lettersType accented letters with the capitals they're stored with
Parse error near line 1 of like_search.sql: table books already existsThe script already ran on library.dbDelete 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​