×

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the mentioned statements:

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

The Like Operator in the Structured Query Language filters the records from the fields based upon the pattern used in the condition in the SQL query.

There are two Wildcard characters used with the SQL like Operator are as follows:

  1. Percent Sign (%): This character is used to match either zero, one, or more than one character in the column.
  2. Underscore (_): This character is used to match one or a single character.

The syntax for using the LIKE Operator is as follows:

SELECT * FROM TABLENAME WHERE COLUMN NAME LIKE PATTERN;

The pattern in the syntax is nothing but the pattern to be searched in the column.

Let's Look at Examples of LIKE Operators.

1. LIKE OPERATOR WITH SELECT STATEMENT

Consider the already existing table with the following data

Table Number: Student

Student_IdStudent_NameCityAge
1Pratik SrivastavPune23
2Utkarsh RokadeMumbai22
3Sourabh ChougaleNashik23
4Prateek ZimbrePune24
5Sakshi PatilAurangabad22
6Shruti SharmaMumbai21
7Pranoti ShendeAurangabad23
8Harshada DhanwatNashik24
9Tejas BairagiNashik21
10Nikhil PatilPune24
11Samaira SharmaMumbai22
12Anushka SenAurangabad23
13Bhushan PachpandeBangalore25
14John ChaudharyBangalore24
15Sonakshi SenBangalore25
16Mayuri WaghHyderabad22
17Ritika PatilHyderabad21
18Tushar MahaleHyderabad24

First look at the Like Operator with the % sign:

Example 1:  For example, you want to display student information but want only those students whose city name starts with 'A'. For such a query, we will need to write the following query:

SELECT * FROM Student WHERE City LIKE ‘A%’;

We have displayed the student's information whose city name starts with the letter 'A' in the above query. 

The output of the above query is as follows:

Student_IdStudent_NameCityAge
5Sakshi PatilAurangabad22
7Pranoti ShendeAurangabad23
12Anushka SenAurangabad23
Pattern Matching in SQL

Example 2:  Suppose you want to display a student's information but only that student whose name ends with 'e'. For such a query, we will need to write the following query:

SELECT * FROM Student WHERE Student_Name LIKE ‘%E’;

We have displayed the student's information whose name ends with the letter 'E' in the above query.

The output of the above query is as follows:

Student_IdStudent_NameCityAge
2Utkarsh RokadeMumbai22
3Sourabh ChougaleNashik23
4Prateek ZimbrePune24
7Pranoti ShendeAurangabad23
13Bhushan PachpandeBangalore25
18Tushar MahaleHyderabad24
Pattern Matching in SQL

Example 3:  For instance, you want to display the student's information but only those students whose name starts with 'P' and ends with 'E'. For such a query, we will need to write the following query:

SELECT * FROM Student WHERE Student_Name LIKE ‘P%E’;

We have displayed the student's information whose name starts with the letter 'P' and ends with the letter 'E' in the above query.

The output of the above query is as follows:

Student_IdStudent_NameCityAge
4Prateek ZimbrePune24
7Pranoti ShendeAurangabad23
Pattern Matching in SQL

Example 4:  Suppose you want to display the student’s information but only those employees whose name contains 'a' at any index. For such a query, we will need to write the following query:

SELECT * FROM Student WHERE Student_Name LIKE ‘%A%’;

We have displayed the student's information whose name contains the letters 'A' at any position in the above query.

The output of the above query is as follows:

Student_IdStudent_NameCityAge
1Pratik SrivastavPune23
2Utkarsh RokadeMumbai22
3Sourabh ChougaleNashik23
4Prateek ZimbrePune24
5Sakshi PatilAurangabad22
6Shruti SharmaMumbai21
7Pranoti ShendeAurangabad23
8Harshada DhanwatNashik24
9Tejas BairagiNashik21
10Nikhil PatilPune24
11Samaira SharmaMumbai22
12Anushka SenAurangabad23
13Bhushan PachpandeBangalore25
14John ChaudharyBangalore24
15Sonakshi SenBangalore25
16Mayuri WaghHyderabad22
17Ritika PatilHyderabad21
18Tushar MahaleHyderabad24
Pattern Matching in SQL

2. LIKE OPERATOR WITH UPDATE STATEMENT: -

In the Structured Query Language, we can use Like Operator with an UPDATE statement in the Where Clause. The Update Statement will update only those records or data included in the query's pattern.

The syntax of the LIKE operator is as follows:

UPDATE TABLENAME SET COLUMNNAME = VALUES WHERE COLUMN NAME LIKE PATTERN(%,_);

Example of Like with Update Statement:

Here, we have taken or explained a few examples to help you understand the Like operation with the Update statement.

Consider the already existing table with the following data

Table Number: Student

Student_IdStudent_NameCityAge
1Pratik SrivastavPune23
2Utkarsh RokadeMumbai22
3Sourabh ChougaleNashik23
4Prateek ZimbrePune24
5Sakshi PatilAurangabad22
6Shruti SharmaMumbai21
7Pranoti ShendeAurangabad23
8Harshada DhanwatNashik24
9Tejas BairagiNashik21
10Nikhil PatilPune24
11Samaira SharmaMumbai22
12Anushka SenAurangabad23
13Bhushan PachpandeBangalore25
14John ChaudharyBangalore24
15Sonakshi SenBangalore25
16Mayuri WaghHyderabad22
17Ritika PatilHyderabad21
18Tushar MahaleHyderabad24

Example 1: Suppose you want to update the student city whose student name starts with 'A'. For such a query, we will need to write the following query:

UPDATE Student SET City = 'Chennai' WHERE Student_Name LIKE 'A%';

We have set the student city name as 'Chennai', where the student’s name starts with the letter 'A' in the above query.

To check whether the query is successfully executed or not, execute Select Statement and see changes:

The output of the above query is as follows:

SELECT * FROM Student WHERE Student_Name LIKE 'A%';
Student_IdStudent_NameCityAge
12Anushka SenChennai23
Pattern Matching in SQL

Example 2: Suppose you want to update the student city name whose student’s name ends with ‘E’. For such a query, we will need to write the following query:

UPDATE Student SET City = 'Delhi' WHERE Student_Name LIKE '%E';

We have set the student city name as 'Delhi', where the student name ends with the letter 'E' in the above query.

To check whether the query is successfully executed or not, execute Select Statement and see changes:

The output of the above query is as follows:

SELECT * FROM Student WHERE Student_Name LIKE '%E';
Student_IdStudent_NameCityAge
2Utkarsh RokadeDelhi22
3Sourabh ChougaleDelhi23
4Prateek ZimbreDelhi24
7Pranoti ShendeDelhi23
13Bhushan PachpandeDelhi25
18Tushar MahaleDelhi24
Pattern Matching in SQL

3. LIKE OPERATOR WITH DELETE STATEMENT: -

In the Structured Query Language, we can use Like Operator with a DELETE statement in the Where Clause. The Delete Statement will remove only those records or data included in the query's pattern.

The syntax of a LIKE operator is as follows:

DELETE FROM TABLENAME WHERE COLUMN NAME LIKE PATTERN(%,_);

Example of Like with Delete Statement:

Here, we have taken or explained a few examples to help you understand the Like operation with the Delete statement.

Consider the already existing table with the following data

Table Number: Student

Student_IdStudent_NameCityAge
1Pratik SrivastavPune23
2Utkarsh RokadeDelhi22
3Sourabh ChougaleDelhi23
4Prateek ZimbreDelhi24
5Sakshi PatilAurangabad22
6Shruti SharmaMumbai21
7Pranoti ShendeDelhi23
8Harshada DhanwatNashik24
9Tejas BairagiNashik21
10Nikhil PatilPune24
11Samaira SharmaMumbai22
12Anushka SenChennai23
13Bhushan PachpandeDelhi25
14John ChaudharyBangalore24
15Sonakshi SenBangalore25
16Mayuri WaghHyderabad22
17Ritika PatilHyderabad21
18Tushar MahaleDelhi24

Example: Suppose you want to remove the student information whose student name contains the letter 'A' at any index. For such a query, we will need to write the following query:

DELETE FROM Student WHERE Student_Name LIKE ‘%A%’;

We have displayed the student's information whose name contains the letters 'A' at any position in the above query.

To check whether the query is successfully executed or not, just execute Select Statement and see changes:

The output of the above query is as follows:

SELECT * FROM Student;
Pattern Matching in SQL

Related Topics

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

SQL Truncate

This command deletes all records from table. Truncate is a DDL command. Syntax: TRUNCATE table table_name; Example: Truncate table teacher; ORDER BY The ORDER BY clause arranges the table or column in ascending...

5 minutes read.

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

SQL Data Control Language

Data Control Language decides to whom should (which user) permit access privileges. GRANT and REVOKE are the commands of DCL. GRANT: It gives privileges to user. REVOKE: It takes back privileges from granted...

1 minute read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.