Updated March 16, 2023
Introduction to SQL LIKE
SQL LIKE operator is used to find the specified pattern row we have defined in the like and where condition. In SQL, there are two types of wildcards used in like operators. First, the operator is used to substitute the single or more characters from the specified string. At the time of pattern matching, the recurring characters need to match with the characters exactly.
What is SQL LIKE?
We can use the wildcard operator to select, update and delete statements with where condition. The SQL-like pattern includes the wildcard or regular characters. The pattern will check whether the character string will match the specified pattern or not. The pattern had the wildcard and recurring characters; the operator’s most usable pattern is wildcard characters.
When using the operator, if our argument is not character type, then if possible, the SQL server database engine will convert it into the string data type of character.
We are using two wildcard operators with a like clause as follows.
Below is the wildcard which we are using with like operator.
- %: This wildcard operator will represent the one, zero, or multiple characters.
- Underscore (_): This wildcard operator will represent only a single character.
- List of characters’ wildcard: This wildcard operator is nothing but the single character with the specified set.
- The [character-character]: This wildcard operator is nothing but the single character used in the specified range.
- ^: This wildcard operator is a single character that was not used in a list of ranges.
The SQL-like operator is handy and essential in SQL to match the condition of the specified row by using the where clause.
Use of SQL LIKE
Below are the uses of SQL-like operators as follows. First, we can use the like operator to find the matched string using a specified pattern.
- We can use the operator with the conjunction of select and where clause to find the matched string rows from a table.
- We can find specified rows from the SQL table using the wildcard operator. The rows will be returned that match the specified criteria of the like operator.
- We can use the operator by using the select, update, and delete query to update and delete specified criteria records.
- We can also use the escape character with a like operator, which treats the wildcard operator as a recurring character.
- The operator returns the number of rows when it matches the specified condition.
- We can use an operator to select, delete, and update specified records in the pattern.
- Suppose we want to delete a specified pattern of records from the table, then the operator is the best choice compared to the = and != operator.
- We can also use the like operator in SQL and order by, group by, and many other clauses.
The below example shows how we are using our query as follows.
In the below example, we are using the like operator on the city column.
Code:
select id, name, city from like_test where city like '%u%';
Output:
SQL LIKE Clause
An SQL-like clause is defined to retrieve data as per a specified pattern, which we have used in the query.
Below is the syntax of SQL-like clauses with a select statement as follows.
Syntax:
Select name_of_column1, name_of_column2, …, name_of_columnN from name_of_table where name_of_column like 'xxx%';
Select name_of_column1, name_of_column2, …, name_of_columnN from name_of_table where name_of_column like '%xxx%';
Select name_of_column1, name_of_column2, …, name_of_columnN from name_of_table where name_of_column like '%xxx';
Select name_of_column1, name_of_column2, …, name_of_columnN from name_of_table where name_of_column like 'xxx_';
Select name_of_column1, name_of_column2, …, name_of_columnN from name_of_table where name_of_column like '_xxx_';
Select name_of_column1, name_of_column2, …, name_of_columnN from name_of_table where name_of_column like '_xxx';
Below is the syntax of the clause with the delete statement as follows.
Syntax:
Delete from name_of_table where column_name like 'xxx%';
Delete from name_of_table where column_name like '%xxx%';
Delete from name_of_table where column_name like '%xxx';
Delete from name_of_table where column_name like 'xxx_';
Delete from name_of_table where column_name like '_xxx_';
Delete from name_of_table where column_name like '_xxx';
The below example shows how SQL-like clauses will word on delete query as follows.
Code:
DELETE FROM like_test where city like 'che%';
select * from like_test;
Output:
The below example shows that the operator is not case sensitive. This is because, in the first example, we have used like operator name in an uppercase letter. In the second example, we have used like operator name in lowercase letters both times; it will return the same result without issuing any error. So we can say that operator is not case sensitive.
Code:
select * from like_test where city like 'pu%';
select * from like_test where city LIKE 'pu%';
Output:
Examples of SQL LIKE
Different examples are mentioned below:
Example #1
Operator with % wildcard operator as follows.
Code:
select * from like_test where city LIKE 'Mu%';
select * from like_test where city LIKE '%u%';
select * from like_test where city LIKE 'p%';
Output:
Example #2
Operator with (_) wildcard operator as follows.
Code:
select * from like_test where city LIKE 'Mumba_';
select * from like_test where city LIKE '_une';
select * from like_test where city LIKE '_un_';
Output:
Example #3
Operator with (_), % wildcard operator using delete query.
Code:
DELETE FROM like_test where city like 'che%';
DELETE FROM like_test where city like '%he%';
DELETE FROM like_test where city like '%ai';
DELETE FROM like_test where city like 'chenna_';
DELETE FROM like_test where city like '_henna_';
DELETE FROM like_test where city like '_hennai';
Output:
Example #4
Operator with (_), % wildcard operator using an update query.
Code:
update like_test set id = 10 where city like '%um%';
update like_test set id = 20 where city like '%un%';
Output:
Conclusion
At the time of pattern matching, the recurring characters need to match exactly with the characters specified in the string of characters. Therefore, using a wildcard operator in like clause pattern matching is very easy compared to using the = and != comparison operator of string.
Recommended Articles
We hope that this EDUCBA information on “SQL LIKE” was beneficial to you. You can view EDUCBA’s recommended articles for more information.