Reading history

SQL LIKE and Pattern Matching

4 min read 0 views

Match text patterns in SQL with LIKE, the % and _ wildcards, case sensitivity rules, ESCAPE for literal wildcards, and SQLite GLOB character sets.

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

WildcardMatchesExample
%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’s LIKE ignores case for English letters.

Case Sensitivity

DatabaseLIKE by default
SQLiteCase-insensitive for A–Z
MySQLUsually case-insensitive (depends on the collation)
PostgreSQLCase-sensitive; use ILIKE to ignore case
SQL ServerUsually 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, so SALE10 matches too.
  • ESCAPE '\' declares backslash as the escape character, so \_ means a real underscore. sale_25 still matches because LIKE ignores case.
  • '%\%%' finds text containing a real percent sign.
  • GLOB is 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

  1. Using = with wildcards. name = 'N%' looks for the literal text “N%”.
  2. Forgetting that _ is a wildcard in codes and file names.
  3. Assuming the same case rules everywhere. Test on your database or use LOWER.
  4. Expecting LIKE to 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 ESCAPE to search for a real % or _.
  • Leading wildcards cannot use an index; use full-text search for big text.

Next: SQL CAST and Type Conversion.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *