SQL Tutorial
SQL Database
SQL Table
SQL Select
SQL Order By
SQL Insert
SQL Update
SQL Delete
Difference
SQL Injection
SQL String Functions
Miscl
- SQL Formatter
- SQL group by
- SQL add/drop/update column operation
- SQL CAST Function
- SQL Comments
- SQL CONCAT Function
- CTE (Common Table Expression)SQL
- How to use distinct in SQL?
- Joining Three or More Tables in SQL
- What is Web SQL?
- How to create functions in SQL?
- How to run SQL Script?
- How to Delete Duplicate Rows in SQL?
- Nth Highest salary
- 12 Codd's Rules
- SQL EXCEPT
- Types of SQL JOIN
- Change datatype of column in SQL
- SQL Auto Increment
- SQL Like
- Commit and Rollback in SQL
- SQL Concatenate
- SQL get month from the date
- Savepoint in SQL
- SQL ORDER BY DATE
- TIME Datatype in SQL
- SQL BETWEEN
- CRUD Operations in SQL
- SQL INDEX
- Scalar Functions in SQL
- SET Operators in SQL
- Types of SQL Commands
- TCL Commands in SQL
- SQL Subquery
- SQL View
- Constraints in SQL
- Pattern Matching in SQL
- SQL Date Functions
- DDL Commands in SQL
- DML Commands in SQL
- SQL CASE
- SQL Inner Join
- SQL IN Operator
- Check Constraint in SQL
- SQL CLAUSES
- SQL LOGICAL OPERATORS
- Delete Column from Table
- Add Column in the Table
- Delete one row in SQL
- Change the Column Value
- How to Add Foreign Key in SQL
- Add a Primary Key
- Insert One or More rows
- How to Use LIKE in SQL
- Cursor in SQL
- Difference Between DROP and Truncate
- SQL Comparison Operators
- SQL COUNT WHERE
- SQL SELECT MIN
- SQL Stored Procedure
- SQL SELECT AVG
- SQL SELECT MAX
- SQL ADD COLUMN
- How to use Auto-Increment in SQL
- SQL Languages
- SQL Arithmetic Operators
- How to Use GROUP BY in SQL
- How to Use ORDER BY in SQL
- Trigger in SQL
- What is Race Condition
- SQL COUNT DISTINCT
PL/SQL Tutorial
Sql Interview Question
SQl Quiz
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:
- SELECT Statement
- UPDATE Statement
- 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:
- % (percent sign): This wildcard character matches zero, one, or more than one character.
- _ (underscore sign): This wildcard character matches only one or a single character.
Syntax of the LIKE operator in SQL
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_ID | Name | Emp_Salary | Emp_Dept |
---|---|---|---|
1001 | Vivek | 9000 | Finance |
1002 | Saket | 4000 | HR |
1003 | Raman | 3000 | Coding |
1004 | Suraj | 6000 | Coding |
1005 | Seenu | 5000 | HR |
1006 | Shubham | 10000 | Marketing |
1007 | Anaya | 4000 | Coding |
1008 | Parul | 8000 | Finance |
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:
This query shows the following table in the output:
Emp_ID | Name | Emp_Salary | Emp_Dept |
---|---|---|---|
1002 | Saket | 4000 | HR |
1004 | Suraj | 6000 | Coding |
1005 | Seenu | 5000 | HR |
1006 | Shubham | 10000 | Marketing |
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:
This query shows the following table in the output:
Emp_ID | Name | Emp_Salary | Emp_Dept |
---|---|---|---|
1003 | Raman | 3000 | Coding |
1004 | Suraj | 6000 | Coding |
1006 | Shubham | 10000 | Marketing |
1007 | Anaya | 4000 | Coding |
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:
This query shows the following table in the output:
Name | Emp_Salary |
---|---|
Raman | 3000 |
Suraj | 6000 |
Anaya | 4000 |
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:
This query shows the following table in the output:
Emp_ID | Name | Emp_Salary | Emp_Dept |
---|---|---|---|
1002 | Saket | 4000 | HR |
1003 | Raman | 3000 | Coding |
1004 | Suraj | 6000 | Coding |
1006 | Shubham | 10000 | Marketing |
1007 | Anaya | 4000 | Coding |
1008 | Parul | 8000 | Finance |
Example 2: Let's take the following Student table which helps you to analyze the LIKE operator with _ (underscore) sign:
Roll_No | Name | Marks | Age |
---|---|---|---|
1 | Raman | 95 | 20 |
2 | Kapil | 60 | 19 |
3 | Arun | 85 | 17 |
4 | Ram | 92 | 18 |
5 | Suman | 55 | 20 |
6 | Sanjay | 88 | 18 |
7 | Sheetal | 65 | 19 |
8 | Rakesh | 64 | 20 |
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:
This query shows the following table in the output:
Roll_No | Name | Marks | Age |
---|---|---|---|
1 | Raman | 95 | 20 |
2 | Kapil | 60 | 19 |
4 | Ram | 92 | 18 |
6 | Sanjay | 88 | 18 |
8 | Rakesh | 64 | 20 |
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:
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_No | Name | Marks | Age |
---|---|---|---|
5 | Suman | 55 | 20 |
6 | Sanjay | 88 | 18 |
7 | Sheetal | 65 | 19 |
iii) Suppose, you want to access Roll_No, Name, and Marks of those students whose Marks is 2 digits long and ends with '5':
The above query shows the following table in the output:
Roll_No | Name | Marks | Age |
---|---|---|---|
1 | Raman | 95 | 20 |
3 | Arun | 85 | 17 |
5 | Suman | 55 | 20 |
7 | Sheetal | 65 | 19 |
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
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_No | Name | Marks | City |
---|---|---|---|
1 | Raman | 95 | Delhi |
2 | Kapil | 60 | Gurugram |
3 | Arun | 85 | Ghaziabad |
4 | Ram | 92 | Delhi |
5 | Suman | 55 | Ghaziabad |
6 | Sanjay | 88 | Gurugram |
7 | Sheetal | 65 | Gurugram |
8 | Rakesh | 64 | Delhi |
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:
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:
Roll_No | Name | Marks | City |
---|---|---|---|
5 | Suman | 55 | Jaipur |
6 | Sanjay | 88 | Jaipur |
7 | Sheetal | 65 | Jaipur |
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:
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:
Roll_No | Name | Marks | City |
---|---|---|---|
1 | Raman | 70 | Delhi |
4 | Ram | 70 | Delhi |
8 | Rakesh | 70 | Delhi |
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:
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:
Roll_No | Marks | City |
---|---|---|
3 | 90 | Ghaziabad |
5 | 90 | Ghaziabad |
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:
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:
Roll_No | Name | Marks | City |
---|---|---|---|
1 | Raman | 95 | Goa |
2 | Kapil | 60 | Goa |
4 | Ram | 92 | Goa |
5 | Suman | 55 | Goa |
6 | Sanjay | 88 | Goa |
7 | Sheetal | 65 | Goa |
8 | Rakesh | 64 | Goa |
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_ID | Name | Emp_Salary | Emp_Dept |
---|---|---|---|
1001 | Vivek | 9000 | Finance |
1002 | Saket | 4000 | HR |
1003 | Raman | 3000 | Coding |
1004 | Suraj | 6000 | Coding |
1005 | Seenu | 5000 | HR |
1006 | Shubham | 10000 | Marketing |
1007 | Anaya | 4000 | Coding |
1008 | Parul | 8000 | Finance |
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:
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:
Emp_ID | Name | Emp_Salary | Emp_Dept |
---|---|---|---|
1002 | Saket | 9000 | HR |
1003 | Raman | 9000 | Coding |
1008 | Parul | 9000 | Finance |
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:
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:
Emp_ID | Name | Emp_Salary | Emp_Dept |
---|---|---|---|
1002 | Saket | 4000 | Coding |
1004 | Suraj | 6000 | Coding |
1005 | Seenu | 5000 | Coding |
1006 | Shubham | 10000 | Coding |
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:
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:
Emp_ID | Name | Emp_Salary | Emp_Dept |
---|---|---|---|
1002 | Saket | 20000 | HR |
1005 | Seenu | 20000 | HR |
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
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_No | Name | Marks | City |
---|---|---|---|
1 | Raman | 95 | Delhi |
2 | Kapil | 60 | Gurugram |
3 | Arun | 85 | Ghaziabad |
4 | Ram | 92 | Delhi |
5 | Suman | 55 | Ghaziabad |
6 | Sanjay | 88 | Gurugram |
7 | Sheetal | 65 | Gurugram |
8 | Rakesh | 64 | Delhi |
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:
This query removes those records of students from the above student table whose name starts with S:
Roll_No | Name | Marks | City |
---|---|---|---|
5 | Suman | 55 | Ghaziabad |
6 | Sanjay | 88 | Gurugram |
7 | Sheetal | 65 | Gurugram |
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:
This query removes those records of students from the above student table whose City name ends with i:
Roll_No | Name | Marks | City |
---|---|---|---|
1 | Raman | 95 | Delhi |
4 | Ram | 92 | Delhi |
8 | Rakesh | 64 | Delhi |
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:
This query removes those records of students from the above student table whose Name of the city starts with "G" and ends with "d".
Roll_No | Name | Marks | City |
---|---|---|---|
1 | Raman | 95 | Delhi |
2 | Kapil | 60 | Gurugram |
4 | Ram | 92 | Delhi |
6 | Sanjay | 88 | Gurugram |
7 | Sheetal | 65 | Gurugram |
8 | Rakesh | 64 | Delhi |
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:
This query removes those records of students from the above student table whose Name contains the letter "a" in any position.
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:
This query removes those records of students from the above student table whose Name contains the alphabet "a" at the second position.
The above query shows the following table in the output:
Roll_No | Name | Marks | City |
---|---|---|---|
3 | Arun | 85 | Ghaziabad |
5 | Suman | 55 | Ghaziabad |
7 | Sheetal | 65 | Gurugram |
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:
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".
The above query shows the following table in the output:
Roll_No | Name | Marks | City |
---|---|---|---|
5 | Suman | 55 | Ghaziabad |
6 | Sanjay | 88 | Gurugram |
7 | Sheetal | 65 | Gurugram |
vii) Suppose, you want to delete those records from the student table whose Marks is 2 digits long and ends with '5':
This query removes those records of students from the above student table whose 'Marks' is 2 digits long and ends with '5'.
The above query shows the following table in the output:
Roll_No | Name | Marks | City |
---|---|---|---|
2 | Kapil | 60 | Gurugram |
4 | Ram | 92 | Delhi |
6 | Sanjay | 88 | Gurugram |
8 | Rakesh | 64 | Delhi |