Search boxes, name filters and product-code checks all need to match text by a pattern rather than an exact value: “names starting with N”, “emails ending in .edu”, “codes like two letters then four digits”. SQL does this with LIKE and its wildcards. This lesson covers the two wildcards, case sensitivity, how to search for a real % or _, and SQLite’s stricter GLOB.
In this lesson you will learn
- The
%and_wildcards - Starts with, ends with and contains
- Case sensitivity in different databases
- Escaping wildcards with
ESCAPE GLOB, character sets and regular expressions
You should know the WHERE clause and string functions.
The Two Wildcards
| Wildcard | Matches | Example |
|---|---|---|
% | Any number of characters, including none | 'N%' = starts with N |
_ | Exactly one character | '_a%' = second letter is a |
SELECT name FROM students WHERE name LIKE 'N%'; -- starts with N
SELECT name FROM students WHERE name LIKE '%i'; -- ends with i
SELECT name FROM students WHERE name LIKE '_a%'; -- second letter is a
SELECT title FROM courses WHERE title LIKE '%data%'; -- contains "data" (any case in SQLite)
Output
+--------+
| name |
+--------+
| Nusrat |
| Nadia |
+--------+
(2 rows)
+------+
| name |
+------+
| Rafi |
+------+
(1 row)
+--------+
| name |
+--------+
| Tania |
| Rafi |
| Sabbir |
| Nadia |
+--------+
(4 rows)
+-----------------+
| title |
+-----------------+
| Data Structures |
| Databases |
+-----------------+
(2 rows)
'%i'finds names ending in i: only Rafi.'_a%'needs exactly one character, then a: Tania, Rafi, Sabbir and Nadia.'%data%'found “Data Structures” even though the pattern is lower case, because SQLite’sLIKEignores case for English letters.
Case Sensitivity
| Database | LIKE by default |
|---|---|
| SQLite | Case-insensitive for A–Z |
| MySQL | Usually case-insensitive (depends on the collation) |
| PostgreSQL | Case-sensitive; use ILIKE to ignore case |
| SQL Server | Usually case-insensitive (depends on the collation) |
For the same result everywhere, compare lower-case text: WHERE LOWER(title) LIKE '%data%'.
Escaping and GLOB
CREATE TABLE codes (code TEXT);
INSERT INTO codes VALUES ('SALE_10'), ('SALE10'), ('sale_25'), ('X50%OFF'), ('A1B2');
-- _ is a wildcard, so this matches both SALE_10 and SALE10... and more
SELECT code FROM codes WHERE code LIKE 'SALE_%';
-- ESCAPE makes \_ mean a real underscore
SELECT code FROM codes WHERE code LIKE 'SALE\_%' ESCAPE '\';
-- A real percent sign inside the text
SELECT code FROM codes WHERE code LIKE '%\%%' ESCAPE '\';
-- GLOB: case-sensitive, uses * and ? and [ ] character sets
SELECT code FROM codes WHERE code GLOB 'SALE*';
SELECT code FROM codes WHERE code GLOB '[A-Z][0-9][A-Z][0-9]';
Output
+---------+
| code |
+---------+
| SALE_10 |
| SALE10 |
| sale_25 |
+---------+
(3 rows)
+---------+
| code |
+---------+
| SALE_10 |
| sale_25 |
+---------+
(2 rows)
+---------+
| code |
+---------+
| X50%OFF |
+---------+
(1 row)
+---------+
| code |
+---------+
| SALE_10 |
| SALE10 |
+---------+
(2 rows)
+------+
| code |
+------+
| A1B2 |
+------+
(1 row)
- In
'SALE_%'the underscore is a wildcard, soSALE10matches too. ESCAPE '\'declares backslash as the escape character, so\_means a real underscore.sale_25still matches becauseLIKEignores case.'%\%%'finds text containing a real percent sign.GLOBis SQLite’s case-sensitive matcher. It uses*and?instead of%and_, and supports character sets:[A-Z][0-9][A-Z][0-9]matches A1B2.
For full regular expressions, PostgreSQL has ~, MySQL has REGEXP, and SQLite supports REGEXP only when a function is added by the program.
Performance
A pattern that starts with a fixed prefix, such as 'N%', can use an index on the column. A pattern with a leading wildcard, such as '%data%', must check every row. For fast word searches in large text, use full-text search.
Common Mistakes
- Using
=with wildcards.name = 'N%'looks for the literal text “N%”. - Forgetting that
_is a wildcard in codes and file names. - Assuming the same case rules everywhere. Test on your database or use
LOWER. - Expecting
LIKEto match NULL.NULL LIKE '%'is unknown, so NULL rows never appear.
Practice Exercises
Exercise 1. Find students whose name contains the letter a at least twice.
Show answer
SELECT name FROM students WHERE name LIKE '%a%a%';
Tania and Nadia.
Exercise 2. Find students whose name has exactly five letters, using only LIKE.
Show answer
SELECT name FROM students WHERE name LIKE '_____';
Five underscores match exactly five characters: Tania, Imran and Nadia.
Frequently Asked Questions
What is NOT LIKE?
It keeps rows that do not match the pattern, for example email NOT LIKE '%@%' finds entries without an @ sign. Rows with NULL are still excluded.
Does LIKE work with Bangla text?
Yes. Wildcards match Bangla characters as normal characters. Case rules do not apply, because Bangla has no upper and lower case.
Summary
%matches any run of characters;_matches exactly one.- Case sensitivity differs between databases;
LOWER()makes it predictable. - Use
ESCAPEto search for a real%or_. - Leading wildcards cannot use an index; use full-text search for big text.
Next: SQL CAST and Type Conversion.
