RienSQL
LESSON 17 / 62 · Operator LIKE

Operator LIKE

SQL syntax of the LIKE and ILIKE operators to search for records by a patterned string

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:

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

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

The text column whose values are compared with the pattern.

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

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

NOT
KeywordOptional

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

%Any sequence of charactersFor example:Many characters…1 character0 characters_Exactly one character1 character

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

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

... WHERE table_field LIKE 'text%'

Matches any strings beginning with "text"

... WHERE table_field LIKE '%text'

Matches any strings ending with "text"

... 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"

... WHERE table_field LIKE 'begin%end'

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

By default, MySQL patterns are not case-sensitive

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:

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

WHERE table_field LIKE string_pattern ESCAPE escape_character

The text column whose values are compared with the pattern.

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

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:

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:

Practice

Solve it in the editor. The query runs against a sample database in your browser.

Task 1Filtering by string pattern
Find all family members with the lastname "Quincey" and output the field member_name.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 2Short first names
Print the names of passengers (the name field) from the Passenger table whose first name, before the space, is exactly three characters long.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 3An underscore in the name
Print the company names name from the Company table that contain the underscore character _.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.