In this tutorial, we will learn how to use LIKE operator to retrieve a specified pattern or partial match from a table using SQL.
LIKE operator is used in a WHERE clause to search for a specified pattern in a column of a table.
Two Wildcards are used in conjunction with the LIKE Operator: % (Percent Sign) and _ (Underscore).
The % (Percent Sign) and _ (Underscore) sign both can also be used in combinations.
SELECT column1, column2,...columnN
FROM tableName
WHERE columnN LIKE pattern;
Let us consider this table "Employee" for records.
Table Name : Employee
| ID | EmpName | City | Country | Gender | Salary |
| 1 | Shankar | Delhi | India | male | 25000 |
| 2 | Sourabh | Delhi | India | male | 30000 |
| 3 | Ranvijay | Mumbai | India | male | 15000 |
| 4 | Kapil | Noida | India | male | 25000 |
| 5 | Shalini | Jaipur | India | female | 18000 |
| 6 | Rakesh | Faridabad | India | male | 23000 |
| 7 | Akshay | Mumbai | India | male | 21000 |
| 8 | Sarah | New York | US | female | 76000 |
| 9 | Rocky | Noida | India | male | 28000 |
Here is an example of LIKE operator where an employee name starting with "S", a query will be:
SELECT * FROM Employee
WHERE EmpName LIKE 'S%';
| ID | EmpName | City | Country | Gender | Salary |
| 1 | Shankar | Delhi | India | male | 25000 |
| 2 | Sourabh | Delhi | India | male | 30000 |
| 5 | Shalini | Jaipur | India | female | 18000 |
| 8 | Sarah | New York | US | female | 76000 |
Here is another example of LIKE operator where an employee name Ending with "%h". a query will be:
SELECT * FROM Employee
WHERE EmpName LIKE '%h';
| ID | EmpName | City | Country | Gender | Salary |
| 2 | Sourabh | Delhi | India | male | 30000 |
| 6 | Rakesh | Faridabad | India | male | 23000 |
| 8 | Sarah | New York | US | female | 76000 |
Here is another example of LIKE operator where an Employee Name that has "%ra%" in any position, a query will be:
SELECT * FROM Employee
WHERE EmpName LIKE '%ra%';
| ID | EmpName | City | Country | Gender | Salary |
| 2 | Sourabh | Delhi | India | male | 30000 |
| 3 | Ranvijay | Mumbai | India | male | 15000 |
| 6 | Rakesh | Faridabad | India | male | 23000 |
| 8 | Sarah | New York | US | female | 76000 |
Here is an example of LIKE operator where an employee name that has "a" at a third position, a query will be:
SELECT * FROM Employee
WHERE EmpName LIKE '__a%';
| ID | EmpName | City | Country | Gender | Salary |
| 1 | Shankar | Delhi | India | male | 25000 |
| 5 | Shalini | Jaipur | India | female | 18000 |
Here is an example of LIKE operator where an employee name that starts with "s" and ends with "h", a query will be:
SELECT * FROM Employee
WHERE EmpName LIKE 's%h';
| ID | EmpName | City | Country | Gender | Salary |
| 2 | Sourabh | Delhi | India | male | 30000 |
| 8 | Sarah | New York | US | female | 76000 |
Here is an example of LIKE operator where an Employee Name that not starts with "s", query will be:
SELECT * FROM Employee
WHERE EmpName NOT LIKE 's%';
| ID | EmpName | City | Country | Gender | Salary |
| 3 | Ranvijay | Mumbai | India | male | 15000 |
| 4 | Kapil | Noida | India | male | 25000 |
| 6 | Rakesh | Faridabad | India | male | 23000 |
| 7 | Akshay | Mumbai | India | male | 21000 |
| 9 | Rocky | Noida | India | male | 28000 |