Aral Code

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

WildcardMatchesExampleFinds
%Any number of characters (even zero)'C%'Coffee, Corned Beef, Chips
_Exactly one character'_ggs%'Eggs

Starts with

SQL Products starting with C
SELECT name FROM products
WHERE name LIKE 'C%';

Ends with

SQL Customers whose names end in z
SELECT name FROM customers
WHERE name LIKE '%z';

Contains

% on both sides finds the text anywhere:

SQL Products containing 'oil' or 'Oil'
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 _

SQL First names with exactly 4 letters
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

SQL Globe/TM-style numbers starting with 0917
SELECT name, phone FROM customers
WHERE phone LIKE '0917%';

NOT LIKE

SQL Products whose name has no parentheses
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:

SQL Search: 'sa'
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. 🙂

1. Which pattern finds names that start with "Ma"?
2. What does _ match?
3. Which pattern finds text containing "kape" anywhere?
4. What is SQL injection?

By Sir Reyel · Last updated 2026-12-04