Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQL LIKE Operator

SQL LIKE Operator


The LIKE operator is used to search for a specified pattern in a column in a WHERE clause.

LIKE operator is a keyword in SQL used for WHERE fuzzy queries in the clause; it allows us to select data based on pattern matching, usually together with the % and _ wildcard.

SQL LIKE Syntax

SELECT column1, column2, ...
FROM table_name
WHERE column_name LIKE pattern;

Parameter Description:

Wildcard

Example

Assume we have a table named Products with the following data:

ProductID ProductName Category
1 iPhone 12 Electronics
2 Samsung Galaxy S21 Electronics
3 Dell XPS 13 Electronics
4 Nike Air Zoom Footwear
5 Adidas Ultraboost Footwear
6 Sony PlayStation 5 Electronics

Use the % wildcard to find all products starting with "iPhone":

SELECT ProductName, Category
FROM Products
WHERE ProductName LIKE 'iPhone%';

The following data is returned:

ProductName Category
iPhone 12 Electronics

Use the _ wildcard to find all products whose name has "e" as the second character:

SELECT ProductName, Category
FROM Products
WHERE ProductName LIKE '_e%';

The following data is returned:

ProductName Category
Dell XPS 13 Electronics

Combine the % and _ wildcards to find all products whose name contains "Zoom":

SELECT ProductName, Category
FROM Products
WHERE ProductName LIKE '%Zoom%';

The following data is returned:

ProductName Category
Nike Air Zoom Footwear

Demonstration Database

In this tutorial, we will use the RUNOOB sample database.

Below is the data selected from the "Websites" table:


mysql> SELECT * FROM Websites;
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  2 | 淘宝          | https://www.taobao.com/   |    13 | CN      |
|  3 | Runoob Tutorial       | http://www.runoob.com/    |  5000 | USA     |
|  4 | 微博           | http://weibo.com/         |    20 | CN      |
|  5 | Facebook      | https://www.facebook.com/ |     3 | USA     |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

SQL LIKE Operator Example

The following SQL statement selects all customers whose name starts with the letter "G":

Example

SELECT * FROM Websites

WHERE name LIKE 'G%';

Execution output:

Tip:The "%" symbol is used to define wildcards before and after a pattern (default letter). You will learn more about wildcards in the next chapter.

The following SQL statement selects all customers whose name ends with the letter "k":

Example

SELECT * FROM Websites

WHERE name LIKE '%k';

Execution output:

The following SQL statement selects all customers whose name contains the pattern "oo":

Example

SELECT * FROM Websites

WHERE name LIKE '%oo%';

Execution output:

By using the NOT keyword, you can select records that do not match a pattern.

The following SQL statement selects all customers whose name does not contain the pattern "oo":

Example

SELECT * FROM Websites

WHERE name NOT LIKE '%oo%';

Execution output: