SQL LIKE operator
The SQL LIKE operator gives you the ability to search through a database using wildcards. The syntax is:
SELECT column_name1 FROM table_name WHERE column_name1 LIKE pattern;
We will use the Employees table to demonstrate how to fetch specific data with the SQL LIKE operator:
+----------------+-----------+-----------+
| employeeNumber | lastName | firstName |
+----------------+-----------+-----------+
| 1002 | Murphy | Diane |
| 1056 | Patterson | Mary |
| 1076 | Firrelli | Jeff |
| 1088 | Patterson | William |
| 1102 | Bondur | Gerard |
| 1143 | Bow | Anthony |
| 1165 | Jennings | Leslie |
| 1166 | Thompson | Leslie |
| 1188 | Firrelli | Julie |
| 1216 | Patterson | Steve |
| 1286 | Tseng | Foon Yue |
| 1323 | Vanauf | George |
| 1337 | Bondur | Loui |
| 1370 | Hernandez | Gerard |
| 1401 | Castillo | Pamela |
| 1501 | Bott | Larry |
| 1504 | Jones | Barry |
| 1611 | Fixter | Andy |
| 1612 | Marsh | Peter |
| 1619 | King | Tom |
| 1621 | Nishi | Mami |
| 1625 | Kato | Yoshimi |
| 1702 | Gerard | Martin |
+----------------+-----------+-----------+
To fetch records whose firstName parameter begins with M, we can use the following command:
SELECT * FROM employees WHERE firstName LIKE ‘M%’;
This would produce the following result:
+----------------+-----------+-----------+
| employeeNumber | lastName | firstName |
+----------------+-----------+-----------+
| 1056 | Patterson | Mary |
| 1621 | Nishi | Mami |
| 1702 | Gerard | Martin |
+----------------+-----------+-----------+
.