Filtering & Sorting Lesson 8 of 21
SQL LIKE and Wildcards — Searching Text
Search for patterns in text with LIKE and the wildcards % and _, find names that start, end or contain certain letters, and build a simple search box query.
= only finds exact matches. But what if you remember only part of a name, like "something with coffee"? LIKE searches for patterns in text, which is how search boxes on websites work.
The two wildcards
| Wildcard | Matches | Example | Finds |
|---|---|---|---|
% | Any number of characters (even zero) | 'C%' | Coffee, Corned Beef, Chips |
_ | Exactly one character | '_ggs%' | Eggs |
Starts with
SELECT name FROM products
WHERE name LIKE 'C%';
Ends with
SELECT name FROM customers
WHERE name LIKE '%z';
Contains
% on both sides finds the text anywhere:
SELECT name, price FROM products
WHERE name LIKE '%oil%';
In SQLite and MySQL, LIKE ignores upper/lowercase for normal English letters, so '%oil%' also finds "Cooking Oil". (PostgreSQL's LIKE is case-sensitive; it uses ILIKE for case-insensitive searches.)
Exactly one character with _
SELECT first_name FROM students
WHERE first_name LIKE '____';
Four underscores mean exactly four characters, so Juan and Jose match but Maria doesn't. Try '_____' (five) or 'J___' (four letters starting with J).
Phone number patterns
SELECT name, phone FROM customers
WHERE phone LIKE '0917%';
NOT LIKE
SELECT name FROM products
WHERE name NOT LIKE '%(%';
A search box query
When you type in a website's search box, the server often builds a query like this, putting your words between % signs:
SELECT name, category, price
FROM products
WHERE name LIKE '%sa%' OR category LIKE '%sa%'
ORDER BY name;
Change sa to other words, like can or ri, and run it again.
LIKE is slower than =
A pattern starting with % makes the database check every single row. That's fine for small tables like ours, but for millions of rows, big systems use special "full-text search" features instead.
Searching for a real % or _
To find text that actually contains % or _, use an escape character:
SELECT * FROM promos WHERE title LIKE '%50\% off%' ESCAPE '\';
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04