LIKE and Wildcards
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".LIKEis case-sensitive in PostgreSQL; useILIKE(PostgreSQL only) orlower(title) LIKE 'java%'.- To match a real
%or_, put a backslash before it:LIKE '%\%%'. NOT LIKEleaves out rows where the value isNULL.
The Basics
WHERE column LIKE 'pattern'
WHERE column NOT LIKE 'pattern'| Wildcard | Matches | Example | Matches |
|---|---|---|---|
% | 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":
SELECT title
FROM courses
WHERE title LIKE 'Java%'
ORDER BY title; 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 %'.
Where the % Goes
The position of % decides what you're asking.
| Pattern | Means | Fee Tracker matches |
|---|---|---|
'Java%' | Starts with Java | 4 titles |
'%Basics' | Ends with Basics | 4 titles |
'%OOP%' | Contains OOP anywhere | Java OOP |
'SQL Basics' | Exactly this text (no wildcard) | SQL Basics |
Titles that end with "Basics":
SELECT title
FROM courses
WHERE title LIKE '%Basics'
ORDER BY title; 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:
SELECT first_name, phone
FROM students
WHERE phone LIKE '(415)%'
ORDER BY first_name; 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:
SELECT first_name
FROM students
WHERE first_name LIKE '_a%'
ORDER BY first_name; 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:
SELECT title
FROM courses
WHERE title LIKE 'java%'; title
-------
(0 rows)Two ways to ignore case:
SELECT title
FROM courses
WHERE title ILIKE 'java%'
ORDER BY title; title
-------------------
Java Basics
Java Collections
Java OOP
JavaScript Basics
(4 rows)SELECT title
FROM courses
WHERE lower(title) LIKE 'java%'
ORDER BY title; 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.
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:
SELECT phrase, phrase LIKE '%\%%' AS has_percent
FROM (VALUES ('50% off'), ('50 off')) AS t(phrase)
ORDER BY phrase; 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:
SELECT first_name, email
FROM students
WHERE email NOT LIKE '%@example.com'
ORDER BY first_name; 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.
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
SELECT title, fee_usd
FROM courses
WHERE title LIKE 'Java %'
ORDER BY title; 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.
Key Takeaways
| You want to | Write |
|---|---|
| Starts with | LIKE 'Java %' |
| Ends with | LIKE '%Basics' |
| Contains | LIKE '%OOP%' |
| One character | LIKE '_a%' |
| Ignore case | ILIKE 'java%' or lower(title) LIKE 'java%' |
| A real percent sign | LIKE '%\%%' |
Was this tutorial helpful?