LIKE and Wildcards

Updated · 7 min read
easyPostgreSQL 18sqlwherelikewildcardsiliketext

LIKE finds text that matches a pattern instead of an exact value: titles that start with "Java", phone numbers with a San Francisco area code, emails at one domain. A pattern uses two wildcards: % for any run of characters and _ for exactly one. This lesson covers both, how to search for a literal %, how case affects a match, and why some patterns are slow on big tables.

In One Minute

  • % matches any number of characters, including none; _ matches exactly one.
  • title LIKE 'Java%' finds titles that start with "Java", including "JavaScript Basics".
  • LIKE is case-sensitive in PostgreSQL; use ILIKE (PostgreSQL only) or lower(title) LIKE 'java%'.
  • To match a real % or _, put a backslash before it: LIKE '%\%%'.
  • NOT LIKE leaves out rows where the value is NULL.

The Basics

SQL
WHERE column LIKE 'pattern'
WHERE column NOT LIKE 'pattern'
WildcardMatchesExampleMatches
%Any number of characters, even zero'Java%'Java Basics, Java OOP, JavaScript Basics
_Exactly one character'_a%'Navika (one letter, then a)

Courses whose titles start with "Java":

SQL▶ Try it
SELECT title
FROM courses
WHERE title LIKE 'Java%'
ORDER BY title;
Output
       title
-------------------
 Java Basics
 Java Collections
 Java OOP
 JavaScript Basics
(4 rows)

Notice JavaScript Basics. 'Java%' means "starts with the four letters J-a-v-a", and "JavaScript" does. To find only the Java courses, include the space: 'Java %'.

% stretches over any run of characters; _ fills exactly one

Where the % Goes

The position of % decides what you're asking.

PatternMeansFee Tracker matches
'Java%'Starts with Java4 titles
'%Basics'Ends with Basics4 titles
'%OOP%'Contains OOP anywhereJava OOP
'SQL Basics'Exactly this text (no wildcard)SQL Basics

Titles that end with "Basics":

SQL▶ Try it
SELECT title
FROM courses
WHERE title LIKE '%Basics'
ORDER BY title;
Output
       title
-------------------
 Git Basics
 Java Basics
 JavaScript Basics
 SQL Basics
(4 rows)

And the phones with San Francisco's 415 area code. The parentheses in '(415)%' are ordinary characters in a LIKE pattern:

SQL▶ Try it
SELECT first_name, phone
FROM students
WHERE phone LIKE '(415)%'
ORDER BY first_name;
Output
 first_name |     phone
------------+----------------
 Advitha    | (415) 555-0178
 Srihitha   | (415) 555-0142
(2 rows)

Exactly One Character: _

_ stands for one character, no more and no fewer. Students whose second letter is a:

SQL▶ Try it
SELECT first_name
FROM students
WHERE first_name LIKE '_a%'
ORDER BY first_name;
Output
 first_name
------------
 Navika
(1 row)

Only Navika: N, then a. Srihitha's and Advitha's second letters are r and d.

Upper and Lower Case

In PostgreSQL, LIKE is case-sensitive, so 'java%' finds nothing:

SQL▶ Try it
SELECT title
FROM courses
WHERE title LIKE 'java%';
Output
 title
-------
(0 rows)

Two ways to ignore case:

SQL▶ Try it
SELECT title
FROM courses
WHERE title ILIKE 'java%'
ORDER BY title;
Output
       title
-------------------
 Java Basics
 Java Collections
 Java OOP
 JavaScript Basics
(4 rows)
SQL▶ Try it
SELECT title
FROM courses
WHERE lower(title) LIKE 'java%'
ORDER BY title;
Output
       title
-------------------
 Java Basics
 Java Collections
 Java OOP
 JavaScript Basics
(4 rows)

ILIKE is PostgreSQL only. lower(column) LIKE 'lowercase pattern' works in PostgreSQL, MySQL, SQL Server, Oracle and SQLite (MS Access spells it LCase). Whether LIKE itself ignores case differs between databases: MySQL and SQL Server usually ignore case because of their default collations, while PostgreSQL and Oracle don't. Tabs showing each database's version are coming to this course.

LIKE 'java%' finds nothing; ILIKE and lower() find all four

Matching a Real % or _

To search for an actual % or _, escape it with a backslash. '%\%%' means "anything, then a real percent sign, then anything". Here the VALUES list makes two test strings on the spot:

SQL▶ Try it
SELECT phrase, phrase LIKE '%\%%' AS has_percent
FROM (VALUES ('50% off'), ('50 off')) AS t(phrase)
ORDER BY phrase;
Output
 phrase  | has_percent
---------+-------------
 50 off  | f
 50% off | t
(2 rows)

Backslash is PostgreSQL's default escape character. ESCAPE chooses another one, which also works in databases where backslash isn't the default: LIKE '%!%%' ESCAPE '!'.

NOT LIKE and Missing Values

NOT LIKE leaves out rows whose value is NULL, for the same reason <> does: the comparison is unknown. Advitha and Navika have no email, so they aren't "not like" an example.com address either:

SQL▶ Try it
SELECT first_name, email
FROM students
WHERE email NOT LIKE '%@example.com'
ORDER BY first_name;
Output
 first_name | email
------------+-------
(0 rows)

Zero rows. To include missing values, add OR email IS NULL.

Why a Leading % Can Be Slow

A pattern that starts with fixed text can use an index; one that starts with % usually can't. An ordinary index keeps values in order, like a phone book, so "starts with Java" means jumping to the J section. "Contains OOP" could be anywhere, so the database checks every row. On the Fee Tracker's six courses that's instant; on millions of rows it matters. PostgreSQL has special indexes for "contains" searches, which you'll meet in Indexes.

Like a phone book: 'Java%' jumps to a section; '%OOP%' reads every page

In Other Databases

SQL Server and Access also accept character ranges in brackets, such as LIKE '[JS]%' for text starting with J or S (SQL Server only). PostgreSQL uses SIMILAR TO or regular expressions for that, which later lessons cover.

Common Mistakes

Forgetting that % matches more than you meant. 'Java%' includes JavaScript. Add the space: 'Java %'.

Using = with a wildcard. title = 'Java%' looks for a title that is literally "Java%". Wildcards only work with LIKE.

Expecting LIKE to ignore case in PostgreSQL. Use ILIKE or lower().

Expecting NOT LIKE to include NULL. It doesn't; add OR column IS NULL.

Practice

Challenge: Find the Java courses only, without JavaScript, using a single LIKE pattern. Show title and fee_usd, sorted by title.

Show solution
SQL▶ Try it
SELECT title, fee_usd
FROM courses
WHERE title LIKE 'Java %'
ORDER BY title;
Output
      title       | fee_usd
------------------+---------
 Java Basics      |  450.00
 Java Collections |  550.00
 Java OOP         |  500.00
(3 rows)

The space after "Java" rules out "JavaScript", where the next character is S.

Quick check: LIKE and wildcards

3 questions
1Which titles does LIKE 'Java%' match?
2In PostgreSQL, what does WHERE title LIKE 'java%' return for the Fee Tracker?
3What does _ match in a LIKE pattern?

Key Takeaways

You want toWrite
Starts withLIKE 'Java %'
Ends withLIKE '%Basics'
ContainsLIKE '%OOP%'
One characterLIKE '_a%'
Ignore caseILIKE 'java%' or lower(title) LIKE 'java%'
A real percent signLIKE '%\%%'

Was this tutorial helpful?

Lesson 19 of 115