Latest web development tutorials

SQL LIKE operator

LIKE operator is used in the WHERE clause to search columns specified pattern.


SQL LIKE operator

LIKE operator is used in the WHERE clause to search columns specified pattern.

SQL LIKE syntax

SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern ;


The demo database

In this tutorial, we will use w3big sample database.

The following is a selected "Websites" table data:

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | 本教程      | http://www.w3big.com/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
| 7  | stackoverflow | http://stackoverflow.com/ |   0 | IND     |
+----+---------------+---------------------------+-------+---------+


SQL LIKE operator examples

The following SQL statement to select all the customer name with the letter "G" begins:

Examples

SELECT * FROM Websites
WHERE name LIKE 'G%';

Execution output:

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

The following SQL statement to select all the customer name with the letter "k" at the end of:

Examples

SELECT * FROM Websites
WHERE name LIKE '% k';

Execution output:

The following SQL statement selects all the customer name contains the pattern "oo" of:

Examples

SELECT * FROM Websites
WHERE name LIKE '% oo%';

Execution output:

By using the NOT keyword, you can select the record does not match the pattern.

The following SQL statement does not contain the name chosen mode "oo" of all customers:

Examples

SELECT * FROM Websites
WHERE name NOT LIKE '% oo%';

Execution output: