MySQL Tutorial – LIKE Operator

LIKE operator

The LIKE operator is used to retrieve records from database table by matching pattern in SELECT SQL statement.

The syntax of LIKE operator:

SELECT column_name(s)
FROM table_name
LIKE “value”;

Let’s see some example:

Here is our database table: (employee_record)

[TABLE=3]

We want to display the f_name and l_name of employees whose is name is like “John”. Issue the command:

SELECT f_name, l_name
FROM employee_record
WHERE f_name LIKE “John”;

Result:

+--------+--------+
| f_name | l_name |
+--------+--------+
| John   | Prats  |
+--------+--------+
1 row in set (0.00 sec)

Using (%) percent sign – (%) percent sign is used to define wildcard or to represent any collection of characters.

Example: We want to display the first name and last name of employees whose first letter starts with letter J. Issue the following command:

SELECT f_name,l_name
FROM employee_record
WHERE f_name LIKE “J%”;

Result:

+------------+---------+
| f_name     | l_name  |
+------------+---------+
| John Lloyd | Cruz    |
| Jericho    | Rosales |
| John       | Prats   |
+------------+---------+
3 rows in set (0.00 sec)

Note: “%R” – means all string that ends with letter R.

“R%” –means all string that starts with the letter R.

“%Rol% – means all string that contains Rol.

, , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.