RianAcademy
Search
⌘
K
Premium

LIKE operator

The LIKE operator is used in conditional queries when we want to find out whether a string matches a certain pattern.

For example, we have a Users table that has an email field:

MySQL 8.4
SELECT name, email FROM Users;
nameemail
Bruce Willisbarjam@hotmail.com
George Clooneytellis@me.com
Kevin Costnermetzzo@hotmail.com
Samuel L. Jacksonmoonlapse@outlook.com
Kurt Russellgator@live.com

Suppose we want to find all users whose email is in the second-level domain "hotmail". That is, we need to select only those records that meet the following condition:

  • after the "@" symbol, "hotmail" follows
  • after "hotmail," a "." symbol follows and then any sequence of characters

For such non-trivial searches on string fields, we need the LIKE operator.

Syntax

MySQL 8.4
WHERE table_field [NOT] LIKE string_pattern
  • [ ]optional part
table_field

The text column whose values are compared with the pattern.

NOT

Reverses the condition: only rows that do not match the pattern remain.

KeywordOptional
string_pattern

A string in quotes that the value should look like, for example '%@hotmail.%'.

The pattern may include two special characters — % and _. Here is what each of them means:

%Any sequence of characters
For example:
Many characters
…
1 character
0 characters
_Exactly one character
1 character

So our query for finding users in the "hotmail" domain might look like this:

MySQL 8.4
SELECT name, email FROM Users
WHERE email LIKE '%@hotmail.%'
nameemail
Bruce Willisbarjam@hotmail.com
Kevin Costnermetzzo@hotmail.com
Jennifer Lopezbarjam@hotmail.com
Harrison Fordkostas@hotmail.com
Michael Douglastimtroyr@hotmail.com
Catherine Zeta-Jonesflakeg@hotmail.com

Examples

  • MySQL 8.4
    ... WHERE table_field LIKE 'text%'
    

    Matches any strings beginning with "text"

  • MySQL 8.4
    ... WHERE table_field LIKE '%text'
    

    Matches any strings ending with "text"

  • MySQL 8.4
    ... WHERE table_field LIKE '_ext'
    

    Matches strings with a length of 4 characters, with the last 3 characters required to be "ext". For example, the words "text" and "next"

  • MySQL 8.4
    ... WHERE table_field LIKE 'begin%end'
    

    Matches strings starting with "begin" and ending with "end"

Escaping special characters

Sometimes you need to search for strings where % and _ are part of the text itself rather than pattern elements. In such cases, these characters must be escaped. In LIKE patterns, \ is used as the default escape character. For example, if you need to retrieve the IDs of tasks with a progress of 3%, you can write:

MySQL 8.4
SELECT job_id FROM Jobs
WHERE progress LIKE '3\\%';

ESCAPE character

If you want to use a different escape character instead of the default one, you can specify it explicitly with ESCAPE.

Syntax with ESCAPE

MySQL 8.4
WHERE table_field LIKE string_pattern ESCAPE escape_character
table_field

The text column whose values are compared with the pattern.

string_pattern

The search pattern. The escape character before % or _ turns them into ordinary characters.

escape_character

A single character that escapes % and _ in this pattern instead of \, for example '!'.

The same query can be written with an explicitly specified escape character:

MySQL 8.4
SELECT job_id FROM Jobs
WHERE progress LIKE '3!%' ESCAPE '!';

Here, ! plays the same role as \ in the previous example.

Interactive Exercise

Now let's practice what we've learned!
In the exercise below, you need to match the email addresses to the LIKE patterns by dragging them into the matching areas.

Match the email addresses to the LIKE patterns by dragging them into the matching areas.

Email addresses to distribute:

john.doe@gmail.com
manager123@office.net
simple@gmail.ru
test_user@business.io
user456@platform.net
contact@gmail.org
my_email@service.io
support789@helpdesk.net
data_analyst@startup.co
%@gmail.%
%\_%@%
%@%.net
Tasks for self-testing
0 / 3
1. Filtering by string pattern

Find all family members with the lastname "Quincey" and output the field member_name.
FamilyMembers
member_id
INT
status
VARCHAR
member_name
VARCHAR
birthday
DATETIME
Goods
good_id
INT
good_name
VARCHAR
type
INT
GoodTypes
good_type_id
INT
good_type_name
VARCHAR
Payments
payment_id
INT
family_member
INT
good
INT
amount
INT
unit_price
INT
date
DATETIME
SELECT member_name
FROM FamilyMembers
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Result
FamilyMembers
Goods
GoodTypes
Payments

Run a query to see the result, or open a table above