Module 2 · Filtering data

Find text patterns with LIKE

By the end you will be able to search for texts that start, end or fit a pattern.

About 12 min

0 of 52 lessons

Before you start, you need

Filter rows with WHERE

Search for a pattern in a text

If you remember part of a name, you can search for it with LIKE. You do not need to know the full text: you just say which part is fixed and which part can vary. We call that rule a pattern.

% allows zero or more characters. 'M%' finds text that starts with M; '%27', text ending in 27; '%o%', text with an o anywhere.

_ allows exactly one character. The pattern 'M_' accepts Ma, but not M or May. On the other hand, 'M%' accepts all three, because what follows can be any length.

Write the pattern in single quotes after LIKE. Use = when you need to compare a complete text. In the challenge you will try short names to see the difference between % and _.

Minimal syntax
WHERE name LIKE 'M%'

See it in action

We want product names that start with M.

SQL
SELECT name
FROM products
WHERE name LIKE 'M%';
Example result: 3 rows
name
Mouse
Monitor 24
Monitor 27
  1. M fixes the start and % allows anything after it. Monitor 24 and Monitor 27 match the pattern.
  2. M% would also accept the text M on its own: the percent sign can stand for zero characters.
  3. To look for M followed by a single character, use 'M_'. The challenge checks that difference with M, Ma and May.
  4. The pattern is a text value and needs single quotes. Pattern matching does not understand what the words mean.

Practice with help

Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.

Guided practice

Find a letter inside the name

Not started

Write a query that returns the names of products that contain the letter 'o'. The order does not matter.

Expected result: 6 rows with the column name, in that order. Row order does not matter.

View tables and data

Open this panel to see the full data.

Look for an ending

Guided practice

Look for an ending

Not started

Show the name of products whose name ends in 27. Use LIKE.

Expected result: 1 row with the column name, in that order. Row order does not matter.

View tables and data

Open this panel to see the full data.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Count one character

Not started

Show the name of products that match M followed by exactly one character. Use LIKE.

Expected result: 1 row with the column name, in that order. Row order does not matter.

This activity uses a copy of the data with extra edge cases. Open the full data before you predict the result.

View tables and data

Open this panel to see the full data.

Key takeaways

  • % represents zero or more characters.
  • _ represents exactly one character.
  • The pattern is enclosed in single quotes.

Next: Work correctly with NULL values

Go deeper

How LIKE treats upper case and accents can vary between engines and settings. Check the behaviour with your application's text. The main examples do not depend on that difference.