SQL Like

The LIKE is a logical operator in the Structured Query Language. This SQL operator is used in the WHERE clause with the following three statements:

  1. SELECT Statement
  2. UPDATE Statement
  3. DELETE Statement

It filters the records from the columns based on the pattern specified in the SQL query.

Following are two wildcard characters that are used either in conjunction or independently with the SQL LIKE operator:

  1. % (percent sign): This wildcard character matches zero, one, or more than one character.
  2. _ (underscore sign): This wildcard character matches only one or a single character.

Syntax of the LIKE operator in SQL

SELECT column_Name1, column_Name2 ...., column_NameN FROM table_Name WHERE column_name LIKE pattern;   

In this syntax, the pattern is a sequence of characters that have to be searched in that column which is just specified after the WHERE clause.

Examples of LIKE Operator in SQL

In this article, we have taken the following different SQL examples which help you how to use the LIKE operator:

Example 1: Let’s take the following Employee table which helps you to analyze the LIKE operator with % sign:

Emp_IDNameEmp_SalaryEmp_Dept
1001Vivek9000Finance
1002Saket4000HR
1003Raman3000Coding
1004Suraj6000Coding
1005Seenu5000HR
1006Shubham10000Marketing
1007Anaya4000Coding
1008Parul8000Finance

i) Suppose, you want to filter the records of those employees whose names start with “S”. For this operation, you have to type the following query:

SELECT * FROM Employee WHERE Name LIKE 'S%' ;  

This query shows the following table in the output:

Emp_IDNameEmp_SalaryEmp_Dept
1002Saket4000HR
1004Suraj6000Coding
1005Seenu5000HR
1006Shubham10000Marketing

ii) Suppose, you want to filter the records of those employees whose department name ends with “g”. For this operation, you have to type the following query:

SELECT * FROM Employee WHERE Emp_Dept LIKE '%g' ;  

This query shows the following table in the output:

Emp_IDNameEmp_SalaryEmp_Dept
1003Raman3000Coding
1004Suraj6000Coding
1006Shubham10000Marketing
1007Anaya4000Coding

iii) Suppose, you want to show the name and salary of those employees whose department name starts with “C” and ends with “g”. For this operation, you have to type the following query:

SELECT Name, Emp_Salary FROM Employee WHERE Emp_Dept LIKE 'C%g' ;  

This query shows the following table in the output:

NameEmp_Salary
Raman3000
Suraj6000
Anaya4000

iv) Suppose, you want to show all records of those employees from the above employee table whose Name contains the letter “a” in any position. For this operation, you have to type the following query:

SELECT * FROM Employee WHERE Emp_Dept LIKE '%a%' ;  

This query shows the following table in the output:

Emp_IDNameEmp_SalaryEmp_Dept
1002Saket4000HR
1003Raman3000Coding
1004Suraj6000Coding
1006Shubham10000Marketing
1007Anaya4000Coding
1008Parul8000Finance

Example 2: Let’s take the following Student table which helps you to analyze the LIKE operator with _ (underscore) sign:

Roll_NoNameMarksAge
1Raman9520
2Kapil6019
3Arun8517
4Ram9218
5Suman5520
6Sanjay8818
7Sheetal6519
8Rakesh6420

i) Suppose, you want to show all records of those students whose Name contains “a” at the second position. For this operation, you have to type the following query with underscore sign:

SELECT * FROM Student WHERE Name LIKE '_a%' ;  

This query shows the following table in the output:

Roll_NoNameMarksAge
1Raman9520
2Kapil6019
4Ram9218
6Sanjay8818
8Rakesh6420

ii) Suppose, you want to access records of those students whose names contain at least 3 characters and starts with the letter “S”. For this operation, you have to type the following query:

SELECT * FROM Student WHERE Name LIKE 'S___%' ;  

In this query, you have to use the underscore sign three times after the S character. The above query shows the following table in the output:

Roll_NoNameMarksAge
5Suman5520
6Sanjay8818
7Sheetal6519

iii) Suppose, you want to access Roll_No, Name, and Marks of those students whose Marks is 2 digits long and ends with ‘5’:

SELECT * FROM Student WHERE Name LIKE '_5' ;  

The above query shows the following table in the output:

Roll_NoNameMarksAge
1Raman9520
3Arun8517
5Suman5520
7Sheetal6519

Like with UPDATE Statement

In SQL, we can also use the LIKE operator in the WHERE clause with the UPDATE statement. The LIKE operator updates those records in the table which satisfy the pattern specified in the query.

Syntax of LIKE with UPDATE Statement

UPDATE table_name SET column_Name1 = value1, column_Name2 = value2, ...., column_NameN = valueN WHERE ColumnName LIKE Pattern;  

Examples of LIKE with UPDATE Statement

Here, we have taken the following two different SQL examples which help you how to use the LIKE operator with UPDATE statement for updating the existing records in the tables:

Example 1: Let’s take the following Student table which shows you how to update records using LIKE operator with % (percent) sign in the UPDATE statement:

Roll_NoNameMarksCity
1Raman95Delhi
2Kapil60Gurugram
3Arun85Ghaziabad
4Ram92Delhi
5Suman55Ghaziabad
6Sanjay88Gurugram
7Sheetal65Gurugram
8Rakesh64Delhi

i) Suppose, you want to update the city of those students whose Name starts with “S”. For this operation, you have to type the following query with underscore sign:

UPDATE Student SET City = 'Jaipur' WHERE Name LIKE 'S%';  

The above query sets the city as Jaipur of those students whose name starts with the letter ‘S’ and if you want to see the changes in the table then you have to type the following query:

Select * From Student Where Name LIKE 'S%' ;  
Roll_NoNameMarksCity
5Suman55Jaipur
6Sanjay88Jaipur
7Sheetal65Jaipur

ii) Suppose, you want to update the Marks of those students whose City name ends with “i”. For this operation, you have to type the following query with underscore sign:

UPDATE Student SET Marks = 70 WHERE City LIKE '%i';  

The above query sets the marks as 70 of those students whose name of the city ends with the letter ‘i’ and if you want to see the changes in the table then you have to type the following query:

Select * From Student Where City LIKE '%i';  
Roll_NoNameMarksCity
1Raman70Delhi
4Ram70Delhi
8Rakesh70Delhi

iii) Suppose, you want to update the Marks of those students whose Name of the city starts with “G” and ends with “d”. For this operation, you have to type the following query with underscore sign:

UPDATE Student SET Marks = 90 WHERE City LIKE 'G%d' ;  

The above query sets the marks as 90 of those students whose name of the city starts with the letter ‘G’ and ends with the letter ‘d’ and if you want to see the changes in the table then you have to type the following query:

Select Roll_No, Marks, City From Student Where City LIKE 'G%d' ;  
Roll_NoMarksCity
390Ghaziabad
590Ghaziabad

iv) Suppose, you want to update the City of those students of the above student table whose Name contains the letter “a” in any position. For this operation, you have to type the following query:

UPDATE Student SET City = 'Goa' WHERE Name LIKE '%a%' ;  

The above query sets the City as Goa of those students whose name contains the letter “a” in any position in the table and if you want to see the changes in the table then you have to type the following query:

UPDATE Student SET City = 'Goa' WHERE Name LIKE '%a%' ;  
Roll_NoNameMarksCity
1Raman95Goa
2Kapil60Goa
4Ram92Goa
5Suman55Goa
6Sanjay88Goa
7Sheetal65Goa
8Rakesh64Goa

Example 2: Let’s take the following Employee table which shows you how to update records using LIKE operator with _ (underscore) sign in the UPDATE statement:

Emp_IDNameEmp_SalaryEmp_Dept
1001Vivek9000Finance
1002Saket4000HR
1003Raman3000Coding
1004Suraj6000Coding
1005Seenu5000HR
1006Shubham10000Marketing
1007Anaya4000Coding
1008Parul8000Finance

Table: Employee

i) Suppose, you want to update the salary of those employees whose Name contains “a” at the second position. For this operation, you have to type the following query with underscore sign:

UPDATE Employee SET Salary = 9000 WHERE Name LIKE '_a%' ;  

The above query sets the Salary as 9000 of those employees whose name contains the letter “a” at the second position in the table and if you want to see the changes in the table then you have to type the following query:

Select * From Employee WHERE Name LIKE  '_a%' ;  
Emp_IDNameEmp_SalaryEmp_Dept
1002Saket9000HR
1003Raman9000Coding
1008Parul9000Finance

ii) Suppose, you want to update the department of those employees whose name contains at least 3 characters and starts with the letter “S”. For this operation, you have to type the following query:

UPDATE Employee SET Emp_Dept = 'Coding' WHERE Name LIKE 'S___%' ;  

In this query, you have to use the underscore sign three times after the S character.

The above query sets the Emp_Dept as Coding of those employees whose name contains at least 3 characters and starts with the letter “S” in the table and if you want to see the changes in the table then you have to type the following query:

SELECT * FROM Employee WHERE Name LIKE 'S___%' ;  
Emp_IDNameEmp_SalaryEmp_Dept
1002Saket4000Coding
1004Suraj6000Coding
1005Seenu5000Coding
1006Shubham10000Coding

iii) Suppose, you want to update the Salary of those employees whose Emp_Dept is 2 Characters long and ends with the character ‘R’. For this operation, you have to type the following query:

>UPDATE Employee SET Salary = 20000 WHERE Emp_Dept LIKE '_R' ;  

The above query sets the Salary as 20000 of those employees whose Emp_Dept is 2 Character long and ends with character ‘R’ in the table and if you want to see the changes in the table then you have to type the following query:

SELECT * FROM Employee WHERE Name LIKE '_R' ;  
Emp_IDNameEmp_SalaryEmp_Dept
1002Saket20000HR
1005Seenu20000HR

Like with DELETE Statement

In SQL, we can also use the LIKE operator in the WHERE clause with the DELETE statement. The LIKE operator deletes or removes those records from the table which matches with the pattern specified in the SQL query.

Syntax of LIKE with DELETE Statement

DELETE FROM table_name WHERE ColumnName LIKE Pattern ;  

Example of LIKE with DELETE Statement

Here, we have taken the following example which helps you how to use the LIKE operator with DELETE statement for deleting the existing records from the database table:

Let’s take the following Student table which shows you how to delete records using the LIKE operator with % (percent) and _(Underscore) sign:

Roll_NoNameMarksCity
1Raman95Delhi
2Kapil60Gurugram
3Arun85Ghaziabad
4Ram92Delhi
5Suman55Ghaziabad
6Sanjay88Gurugram
7Sheetal65Gurugram
8Rakesh64Delhi

i) Suppose, you want to delete the record of those students whose Name starts with “S”. For this operation, you have to type the following query with underscore sign:

DELETE FROM Student WHERE Name LIKE 'S%';  

This query removes those records of students from the above student table whose name starts with S:

SELECT * FROM Student;  
Roll_NoNameMarksCity
5Suman55Ghaziabad
6Sanjay88Gurugram
7Sheetal65Gurugram

ii) Suppose, you want to delete the record of those students whose City name ends with “i”. For this operation, you have to type the following query with underscore sign:

DELETE FROM Student WHERE City LIKE '%i';  

This query removes those records of students from the above student table whose City name ends with i:

SELECT * FROM Student;  
Roll_NoNameMarksCity
1Raman95Delhi
4Ram92Delhi
8Rakesh64Delhi

iii) Suppose, you want to delete the record of those students whose Name of the city starts with “G” and ends with “d”. For this operation, you have to type the following query with underscore sign:

DELETE FROM Student WHERE City LIKE 'G%d';  

This query removes those records of students from the above student table whose Name of the city starts with “G” and ends with “d”.

SELECT * FROM Student;  
Roll_NoNameMarksCity
1Raman95Delhi
2Kapil60Gurugram
4Ram92Delhi
6Sanjay88Gurugram
7Sheetal65Gurugram
8Rakesh64Delhi

iv) Suppose, you want to delete those records of the student table whose Name contains the letter “a” in any position. For this operation, you have to type the following query:

DELETE FROM Student WHERE City LIKE 'G%d';  

This query removes those records of students from the above student table whose Name contains the letter “a” in any position.

SELECT * FROM Student;  

This query does not show any table in the result, because all the rows were deleted from the table.

v) Suppose, you want to delete those records from the student table whose Name contains letter “a” at the second position. For this operation, you have to type the following query with underscore sign:

DELETE FROM Student WHERE Name LIKE '_a%';  

This query removes those records of students from the above student table whose Name contains the alphabet “a” at the second position.

SELECT * FROM Student;  

The above query shows the following table in the output:

Roll_NoNameMarksCity
3Arun85Ghaziabad
5Suman55Ghaziabad
7Sheetal65Gurugram

vi) Suppose, you want to delete those records from the student table whose name contains at least 3 characters and starts with the letter “S”. For this operation, you have to type the following query:

DELETE FROM Student WHERE Name LIKE 'S___%';  

This query removes those records of students from the above student table whose Name contains at least 3 characters and starts with the letter “S”.

SELECT * FROM Student;  

The above query shows the following table in the output:

Roll_NoNameMarksCity
5Suman55Ghaziabad
6Sanjay88Gurugram
7Sheetal65Gurugram

vii) Suppose, you want to delete those records from the student table whose Marks is 2 digits long and ends with ‘5’:

DELETE FROM Student WHERE Marks LIKE '_5' ;  

This query removes those records of students from the above student table whose ‘Marks’ is 2 digits long and ends with ‘5’.

SELECT * FROM Student;  

The above query shows the following table in the output:

Roll_NoNameMarksCity
2Kapil60Gurugram
4Ram92Delhi
6Sanjay88Gurugram
8Rakesh64Delhi

Comments

Leave a Reply

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