Filtering and Sorting · Lesson 12 of 64

SQL LIKE Patterns

What you will learn

  • How LIKE matches text against a pattern
  • The wildcards % (any characters) and _ (exactly one)
  • How to find text that starts with, ends with or contains something
  • How case is handled, and how NOT LIKE works

This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). Edit any query and press Run. Reset data puts the original rows back.

1. The two wildcards

= matches a text exactly. LIKE matches a pattern with two special characters:

Wildcard Matches
% any number of characters, including none
_ exactly one character

All other characters in the pattern stand for themselves.

2. Starts with, ends with, contains

Customers whose last name starts with Ah:

SELECT customer_id, first_name, last_name FROM customers WHERE last_name LIKE 'Ah%' ORDER BY customer_id LIMIT 5;
customer_id  first_name  last_name
-----------  ----------  ---------
          1  Vik         Ahmed
          2  Wanda       Ahmed
         86  Maya        Ahmed
        136  Amir        Ahmed
        146  Vera        Ahmed
(5 rows)

Emails that contain ahmed anywhere:

SELECT customer_id, email FROM customers WHERE email LIKE '%ahmed%' ORDER BY customer_id LIMIT 5;
customer_id  email
-----------  -------------------------
          1  vik.ahmed37@example.com
          2  wanda.ahmed70@example.com
         86  maya.ahmed14@example.com
        136  amir.ahmed63@example.com
        146  vera.ahmed86@example.com
(5 rows)

Customers whose name starts with A:

SELECT customer_id, full_name FROM customers WHERE full_name LIKE 'A%' ORDER BY customer_id LIMIT 5;
customer_id  full_name
-----------  -------------
         11  Ana Costa
         12  Amir Singh
         17  Anil Diaz
         45  Anil Rahman
         54  Aisha Bianchi
(5 rows)

Names that contain an anywhere:

SELECT customer_id, full_name FROM customers WHERE full_name LIKE '%an%' ORDER BY customer_id LIMIT 5;
customer_id  full_name
-----------  -----------
          4  Mei Bianchi
         10  Maya Rahman
         11  Ana Costa
         14  Leo Rahman
         17  Anil Diaz
(5 rows)

Students whose last name starts with M:

SELECT student_id, first_name, last_name FROM students WHERE last_name LIKE 'M%' ORDER BY student_id LIMIT 5;
student_id  first_name  last_name
----------  ----------  ---------
         4  Quinn       Meyer
        19  Arjun       Mahmud
        21  Liam        Meyer
        27  Wei         Mahmud
        49  Mei         Meyer
(5 rows)

Emails that contain khan anywhere:

SELECT student_id, email FROM students WHERE email LIKE '%khan%' ORDER BY student_id LIMIT 5;
student_id  email
----------  ----------------------------
         1  vera.khan38@student.example
        61  rina.khan3@student.example
        87  chloe.khan35@student.example
       170  paulo.khan57@student.example
       171  rosa.khan82@student.example
(5 rows)

The pattern 'Ah%' means "starts with Ah"; '%ahmed%' means "contains ahmed"; '%.com' would mean "ends with .com".

3. The single-character wildcard

_ stands for exactly one character, so a pattern with underscores fixes the length:

Cities with exactly four letters:

SELECT DISTINCT city FROM customers WHERE city LIKE '____' ORDER BY city;
city
----
Lima
Pune
lima
(3 rows)

Cities with exactly four letters:

SELECT DISTINCT city FROM customers WHERE city LIKE '____' ORDER BY city;
city
----
Lima
Pune
(2 rows)

Course codes with exactly six characters that start with SCI:

SELECT course_id, code FROM courses WHERE code LIKE 'SCI___' ORDER BY course_id;
course_id  code
---------  ------
        4  SCI101
        5  SCI201
        6  SCI202
        7  SCI301
(4 rows)

4. Case and NOT LIKE

In SQLite, LIKE ignores upper and lower case for the letters A to Z (unlike =, which is exact). So 'ahmed' finds Ahmed:

SELECT COUNT(*) AS exact_match FROM customers WHERE last_name = 'ahmed';
SELECT COUNT(*) AS like_match FROM customers WHERE last_name LIKE 'ahmed';
exact_match
-----------
          0
(1 row)

like_match
----------
         9
(1 row)
SELECT COUNT(*) AS exact_match FROM customers WHERE city = 'lima';
SELECT COUNT(*) AS like_match FROM customers WHERE city LIKE 'lima';
exact_match
-----------
          0
(1 row)

like_match
----------
        25
(1 row)
SELECT COUNT(*) AS exact_match FROM students WHERE last_name = 'khan';
SELECT COUNT(*) AS like_match FROM students WHERE last_name LIKE 'khan';
exact_match
-----------
          0
(1 row)

like_match
----------
         8
(1 row)

NOT LIKE keeps the rows that do not match:

SELECT COUNT(*) AS without_com FROM customers WHERE email NOT LIKE '%.com';
without_com
-----------
          0
(1 row)
SELECT COUNT(*) AS not_starting_with_a FROM customers WHERE full_name NOT LIKE 'A%';
not_starting_with_a
-------------------
                266
(1 row)
SELECT COUNT(*) AS not_starting_with_m FROM students WHERE last_name NOT LIKE 'M%';
not_starting_with_m
-------------------
                333
(1 row)

In other databases. This case rule differs. In PostgreSQL, LIKE is case-sensitive, and ILIKE is its case-insensitive version (a PostgreSQL extension, not standard SQL). In MySQL, matching depends on the collation of the column, which this lesson does not cover. Both statements are from the official documentation of the products; PostgreSQL's page says 'abc' LIKE 'ABC' is false. They were not run here. To write a query that behaves the same everywhere, make both sides the same case yourself: LOWER(city) LIKE 'lima'.

5. Searching for a real % or _

To look for a literal % or _, put an escape character in front and declare it with ESCAPE:

SELECT '100% sure' LIKE '100\% sure' ESCAPE '\' AS with_escape, '100% sure' LIKE '100% sure' AS wildcard_reading;
with_escape  wildcard_reading
-----------  ----------------
          1                 1
(1 row)
SELECT '100% sure' LIKE '100\% sure' ESCAPE '\' AS with_escape, '100% sure' LIKE '100% sure' AS wildcard_reading;
with_escape  wildcard_reading
-----------  ----------------
          1                 1
(1 row)
SELECT '100% sure' LIKE '100\% sure' ESCAPE '\' AS with_escape, '100% sure' LIKE '100% sure' AS wildcard_reading;
with_escape  wildcard_reading
-----------  ----------------
          1                 1
(1 row)

Both are true here, but only the first really asks for a percent sign.

Worked example: find the right pattern

Task Pattern
names starting with Mc LIKE 'Mc%'
names ending with son LIKE '%son'
names containing van LIKE '%van%'
codes AB1, AB2, ... exactly three characters starting with AB LIKE 'AB_'
names with Mc as the second and third letter LIKE '_Mc%'

Think "where does the fixed part sit?" and put % where anything may be. The values are examples.

Common mistakes

  • Using = with a wildcard. last_name = 'Ah%' looks for the literal text Ah%. Use LIKE.
  • Starting a pattern with % on a big table. The database must read every row; it cannot use an index (lesson 7.4 of Database Fundamentals).
  • Assuming LIKE ignores case everywhere. It does in SQLite and not in PostgreSQL.
  • Forgetting to escape a real % or _.

Key takeaways

  • LIKE matches patterns: % is any characters, _ is exactly one.
  • 'x%' starts with, '%x' ends with, '%x%' contains.
  • In SQLite LIKE ignores case for A to Z; other databases differ.
  • NOT LIKE is the opposite; ESCAPE lets you search for a literal % or _.

Practice

1. Which pattern finds words that start with Sam?

Show answer

LIKE 'Sam%'

2. What does LIKE 'a_c' match?

Show answer

A text of exactly three characters that starts with a and ends with c, such as abc or a-c.

3. What is the difference between name = 'Ah%' and name LIKE 'Ah%'?

Show answer

= looks for the exact text Ah% (with a real percent sign); LIKE treats % as a wildcard and finds names that start with Ah.

4. Does 'ahmed' LIKE 'AHMED' match in SQLite?

Show answer

Yes. In SQLite LIKE ignores case for the letters A to Z.

5. How do you find text that contains the word blue anywhere?

Show answer

LIKE '%blue%'

6. Why is LIKE '%x' slow on a very big table?

Show answer

The database cannot jump to the matching rows through an index, so it reads every row.