ShareYourGroupShareYourGroupSQL

Lesson 5 of 26 · see the practice database

Searching text with LIKE

The equals sign finds exact matches. Often you know only part of what you are looking for: names that start with A, products with "kg" in them, email addresses at a certain site. LIKE matches text against a pattern.

The two wildcards

A pattern is ordinary text plus two special characters.

WildcardStands for
%any run of characters, including none at all
_exactly one character

Starts with

Put the % at the end.

SELECT name FROM customers WHERE name LIKE 'A%';

Result

name
Asha Verma
Arjun Mehta

2 rows

The pattern 'A%' means "an A, then anything".

Ends with

Put the % at the start.

SELECT name, email FROM customers WHERE email LIKE '%@example.com';

Result

nameemail
Asha Vermaasha@example.com
Ravi Kumarravi@example.com
Kabir Khankabir@example.com
Sunita Raosunita@example.com
Priya Nairpriya@example.com
Imran Aliimran@example.com
Neha Joshineha@example.com
Vikram Dasvikram@example.com
Joy Thomasjoy@example.com

9 rows

SELECT name FROM products WHERE name LIKE '%kg';

Result

name
Basmati Rice 5 kg
Toor Dal 1 kg

2 rows

Contains

Put a % on both sides.

SELECT name FROM products WHERE name LIKE '%Box%';

Result

name
Geometry Box

1 row

This finds the text anywhere: at the start, in the middle or at the end, because % can also stand for nothing.

One character with the underscore

Each underscore must match exactly one character. This finds names whose second letter is a.

SELECT name FROM customers WHERE name LIKE '_a%';

Result

name
Ravi Kumar
Kabir Khan
Tara Singh

3 rows

Underscores are useful for codes with a fixed shape. The pattern '2026-0_-__' matches every date from January to September 2026.

SELECT id, order_date FROM orders WHERE order_date LIKE '2026-0_-1_';

Result

idorder_date
22026-01-12
52026-02-14
82026-03-15
112026-04-18
132026-05-19
172026-08-11
192026-09-17

7 rows

That pattern picks the days from the 10th to the 19th of those months.

NOT LIKE

SELECT name FROM products WHERE name NOT LIKE '% %';

Result

name
Earphones

1 row

The pattern '% %' matches any text with a space in it, so NOT LIKE returns the names made of a single word.

Capital and small letters

How LIKE treats case depends on the database.

SELECT name FROM products WHERE name LIKE '%box%';

Result

name
Geometry Box

1 row

To be safe in any database, turn both sides to the same case, as the lesson on functions shows.

Combining patterns

LIKE conditions join with AND and OR like any others.

SELECT name
FROM customers
WHERE name LIKE 'A%' OR name LIKE 'S%';

Result

name
Asha Verma
Sunita Rao
Arjun Mehta

3 rows

There is no way to give LIKE a list of patterns in standard SQL. Repeat the test with OR.

Searching for a real percent sign

What if the text you want contains % or _ itself? Name an escape character with ESCAPE. Whatever follows that character in the pattern is taken as itself.

SELECT '50% off' LIKE '%\%%' ESCAPE '\' AS has_percent,
       'half off' LIKE '%\%%' ESCAPE '\' AS no_percent;

Result

has_percentno_percent
10

1 row

The result is 1 for true and 0 for false, which is how SQLite shows the answer to a test.

LIKE can be slow

A pattern that starts with % forces the database to read every row and look inside the text. On a table of a few thousand rows nobody notices. On millions of rows it is slow. A pattern with a fixed start, such as 'A%', can use an index and stays fast. Large systems that need "contains" searches use special full-text search features instead.

LIKE or equals?

If you know the whole value, use =. It is exact and fast. Use LIKE only when you need a wildcard. A LIKE without any wildcard works but tells the reader you meant something else.

Try it yourself

Exercise 1. Find the customers whose name ends with the surname Khan or Kumar. Show the names.

Solve it in the practice box

Show one answer
SELECT name FROM customers WHERE name LIKE '% Khan' OR name LIKE '% Kumar';
name
Ravi Kumar
Kabir Khan

2 rows

Exercise 2. Find the products whose name contains 1 L. Show name and price.

Solve it in the practice box

Show one answer
SELECT name, price FROM products WHERE name LIKE '%1 L%';
nameprice
Sunflower Oil 1 L145
Steel Bottle 1 L450

2 rows

Exercise 3. Find the employees whose name starts with a letter followed by a as the second letter. Show the names.

Solve it in the practice box

Show one answer
SELECT name FROM employees WHERE name LIKE '_a%';
name
Manoj Yadav
Farah Sheikh
Harpreet Kaur

3 rows

Examples on this page were run with SQLite 3.50.4 on 12 November 2026. The results shown are the real results.

This is an independent learning site. It is not connected with the makers of any database product. MySQL, PostgreSQL, SQL Server, Oracle and SQLite are named only so that you know where a piece of SQL works. The shop, its customers and its staff in the practice database are made up.