Master SQLite3 LIKE for Powerful Pattern Matching

SQLite, a relational database management system, offers the LIKE operator for pattern matching in queries. It's an essential tool for flexible, dynamic searches, used widely in web and mobile applications. In this guide, we'll explore SQLite's LIKE operator, its syntax, and various use cases.

Here’s What No One Tells You About SQLITE3
Here’s What No One Tells You About SQLITE3

Before delving into the specifics, let's understand that SQLite's LIKE operator is case-sensitive and not case-preserving. This means it considers 'A' and 'a' as different characters, but it doesn't preserve the case of matched characters. For case-insensitive searches, you can use the GLOB operator or concatenate the lower() function with LIKE (discussed later).

What is SQLite3? - Essential SQL
What is SQLite3? - Essential SQL

Basic Syntax and Usage

The basic syntax for using the LIKE operator is straightforward. It's comprised of a pattern string that defines what you're searching for, and a column value from your table.

a close up of a cartoon character with big eyes and a turtle like body,
a close up of a cartoon character with big eyes and a turtle like body,

Here's the general syntax:

<SELECT> FROM <TABLE> WHERE <COLUMN> LIKE <PATTERN>

The pattern string can include literal text and wildcard characters, represented by '_' (underscore) and '%' (percent sign). The underscore matches any single character, while the percent sign matches any sequence of characters, including zero.

Sysinfo SQLite Database Recovery
Sysinfo SQLite Database Recovery

Underscore (_) - Single Character Wildcard

The underscore (_) acts as a wildcard character, matching any single character. For instance, 'J_o' matches 'Joe' or 'John' but not 'Johnsson' (too many characters).

Example:

Pokemon Aesthetic - Squirtle
Pokemon Aesthetic - Squirtle

SELECT * FROM users WHERE first_name LIKE 'J_o';

Percent Sign (%) - Sequence Wildcard

The percent sign (%) is a wildcard that matches any sequence of characters, including zero. So, '%o' matches 'Joe', 'John', 'London', etc.

Example:

279 “the peacemaker” tritype ALSO MY TRITYPE!!!
279 “the peacemaker” tritype ALSO MY TRITYPE!!!

SELECT * FROM users WHERE first_name LIKE '%o';

Advanced LIKE Patterns

Sometimes, you need more flexibility. Here are advanced LIKE patterns using CONCAT, OR, and IN:

How to use SQLite as a Document Database like MongoDB
How to use SQLite as a Document Database like MongoDB
a heart with the words what's in your heart? and an image of a man
a heart with the words what's in your heart? and an image of a man
SpokeIsHere fanart
SpokeIsHere fanart
Brat Text Generator White, Aesthetic Art
Brat Text Generator White, Aesthetic Art
♡゙Spokeishere
♡゙Spokeishere
sx4 / sexual 4 enneagram fanart
sx4 / sexual 4 enneagram fanart
a cartoon character sitting on the ground with hearts coming out of his eyes and arms
a cartoon character sitting on the ground with hearts coming out of his eyes and arms
SPOKEISHERE 👀👀👀👀👀👀👀👀👀👀👀 (MY ARTY)
SPOKEISHERE 👀👀👀👀👀👀👀👀👀👀👀 (MY ARTY)
two anime characters one with black hair and the other white
two anime characters one with black hair and the other white
Step by Step Tutorial on Programming SQLite Database Using Python for Beginners
Step by Step Tutorial on Programming SQLite Database Using Python for Beginners
SPOKEISHERE FANART ! 🌈🔲
SPOKEISHERE FANART ! 🌈🔲
💚Uma doce icon da minha largatinha, Squirm🐛
💚Uma doce icon da minha largatinha, Squirm🐛
an image of a cartoon character in front of clovers with the caption you look at me the same
an image of a cartoon character in front of clovers with the caption you look at me the same
A thorough guide to SQLite database operations in Python
A thorough guide to SQLite database operations in Python
Squirtle in the water
Squirtle in the water
a drawing of a skull with purple ink
a drawing of a skull with purple ink
spokeishere
spokeishere
the text is in different colors and sizes, with hearts on each side of the page
the text is in different colors and sizes, with hearts on each side of the page
a black and white drawing of a man with long hair
a black and white drawing of a man with long hair
squirm!!
squirm!!

The CONCAT function allows you to combine multiple LIKE conditions and eliminate false positives. The OR operator can match any of multiple patterns, while the IN clause matches any pattern in a list.

CONCAT for Combining LIKE Conditions

When searching for a name like 'John' or 'Jonathan', using a simple LIKE condition ('%ohn%') can also match 'Johnson', 'Johndoe', etc. To eliminate these false positives, use CONCAT:

Example:

SELECT * FROM users WHERE first_name LIKE CONCAT('J%h%') OR first_name LIKE CONCAT('John%');

OR for Matching Multiple Patterns

The OR operator can match any of multiple patterns. Be cautious with its performance, though, as it may scan the entire index (if any) for each pattern.

Example:

SELECT * FROM users WHERE first_name LIKE '%o%' OR first_name LIKE '%n%';

IN for Matching Values from a List

The IN clause can match any pattern in a list, making it a cleaner alternative to OR in some cases.

Example:

SELECT * FROM users WHERE first_name LIKE ANY ('%o%', '%n', '%o%');

For a more intuitive approach, consider using GLOB patterns or the FTS (Full-Text Search) module available from SQLite version 3.6.0.

Case-Insensitive Searches

As mentioned initially, LIKE is case-sensitive. For case-insensitive searches, you can use either GLOB patterns or concatenate the lower() function with LIKE:

GLOB for Case-Insensitive Patterns

GLOB patterns are more powerful and intuitive than LIKE, offering case-insensitive searches out-of-the-box. Version 3.31.0 and later also supports the IGNORE CASE clause with GLOB.

Example:

SELECT * FROM users GLOB 'first_name:*john*';

lower() Function for Case-Insensitive LIKE

If you're stuck with an older version of SQLite or prefer to stick with LIKE, you can concatenate the lower() function to make searches case-insensitive:

Example:

SELECT * FROM users WHERE lower(first_name) LIKE lower('J%h%');

While this approach works, it's slower and relies on the assumption that string case conversion is quick and deterministic.

Finally, consider other alternatives like the FTS module, which supports full-text indexing, ranking, highlight, and spellcheck capabilities. For complex, high-performance search needs, it might be worth investigating.

Related Articles

Sql Left Join With 3 Tables Sql By W3resource Joins In Sql W3schools What Does Alter Do In Sql How To Use Except In Sql Sql Window Functions Examples Sql Distinct 3 Columns Sql Tryit Editor V1 6 W3schools Sql Where Count 3 What Is Sql W3schools