;

SQL LIKE


In this tutorial, we will learn how to use LIKE operator to retrieve a specified pattern or partial match from a table using SQL.

SQL LIKE Operator

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).

  • % (Percent Sign): it represents a zero, one or multiple characters.
  • _ (Underscore): it represent a single character.

The % (Percent Sign) and _ (Underscore) sign both can also be used in combinations.

Syntax - Like Operator
SELECT column1, column2,...columnN
FROM tableName
WHERE columnN LIKE pattern;

Example:

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

Example of LIKE Operator

Here is an example of LIKE operator where an employee name starting with "S", a query will be:

Example 
SELECT * FROM Employee
WHERE EmpName LIKE 'S%';
Output
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:

Example
SELECT * FROM Employee
WHERE EmpName LIKE '%h';
Output
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:

Example
SELECT * FROM Employee
WHERE EmpName LIKE '%ra%';
Syntax - SELECT Statement
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:

Example
SELECT * FROM Employee
WHERE EmpName LIKE '__a%';
Output
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:

Example
SELECT * FROM Employee
WHERE EmpName LIKE 's%h';
Syntax - SELECT Statement
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:

Example
SELECT * FROM Employee
WHERE EmpName NOT LIKE 's%';
Output
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