Javatpoint Logo
Javatpoint Logo

How to Use LIKE in SQL

In this article, we will learn how to use LIKE to the column in the table of SQL database.

What is Like?

The LIKE is a SQL operator used to search for a particular pattern in each row of the field. This operator is always used with the WHERE clause in the SQL statement.

The syntax for using the Like operator in Structured Query Language:

In Structured Query Language, the LIKE operator is used in conjunction with the following two wildcard characters:

  1. Percent Sign (%)
  2. Underscore (_)

Percent Sign (%)

This sign or wildcard character compares any string with zero, one, or multiple characters.

Syntax of using Percent Sign with LIKE operator:

1. The following syntax matches all the strings which begin with the character 'M':

2. The following syntax matches all the strings which end with the character 'M':

3. The following syntax matches all the strings which contain the character 'M' at any position:

4. The following syntax matches all the strings which begin with the character 'M' and end with character 'T':

Underscore (_)

This wildcard character in SQL compares any string with a single character.

Syntax of using Underscore Sign with LIKE operator:

1. The following syntax matches all the strings which contain only two characters:

2. The following syntax matches all the strings which contain the character 'm' at the second position:

3. The following syntax matches all the strings which contain at least 5 characters and begin with the character 'M':

If you want to add the SQL LIKE operator to the column in the table, you have to follow the following steps in the given sequence:

  1. Create a database in the system.
  2. Create the table in the database and insert the data into the database.
  3. View the inserted data
  4. Use the LIKE operator to the column of the table.

Now, we are going to explain the above steps with an example:

Step 1: Create a Database

In the Structured Query Language, creating a database is the first step for storing the structured tables in the database.

Use the following SQL syntax to create a database:

Suppose you want to create a College database. For this, you have to type the following command in Structured Query Language:

Step 2: Create a Table and Insert the data

Now, use the following SQL syntax for creating the table in your database:

Suppose you want to create the Student table with five columns in the College database. For this, you have to write the following query in your application:

Now, you have to insert the data in the table using the following syntax:

Use the following query to insert the record of multiple students in the Student table of the College database:

Step 3: View the Inserted Data

After table creation and data insertion, you can view the inserted record of the Student table by typing the following query in your SQL application:


Roll_No First_Name City Age Percentage Grade
101 Akash Delhi 18 89 A2
102 Bhavesh Kanpur 19 93 A1
103 Yash Delhi 20 89 A2
104 Bhavna Delhi 19 78 B1
105 Yatin Lucknow 20 75 B1
106 Ishika Ghaziabad 19 91 C1
107 Vivek Goa 20 80 B2

Step 4: Use the Like operator to the column in the table

The following query shows the record of those students from the Student table whose First_Name starts with 'B' letter:

Output of above query:

Roll_No First_Name Percentage Grade
102 Bhavesh 93 A1
104 Bhavna 78 B1

As shown in the above output, the table only contains the record of Bhavesh and Bhavna because their names begin with B letters.

The following query shows the record of those students from the Student table whose First_Name ends with the 'h' letter:

Output of above query:

Roll_No First_Name Percentage Grade
101 Akash 89 A2
102 Bhavesh 93 A1
103 Yash 89 A2

As shown in the above output, the table only contains the record of Akash, Bhavesh, and Yash students because their names end with the letter h.

The following query shows the record of those students from the given Student table whose First_Name contains the character 'a' in any position:

Output of above query:

Roll_No First_Name City Age Percentage Grade
101 Akash Delhi 18 89 A2
102 Bhavesh Kanpur 19 93 A1
103 Yash Delhi 20 89 A2
104 Bhavna Delhi 19 78 B1
105 Yatin Lucknow 20 75 B1
106 Ishika Ghaziabad 19 91 C1

As shown in the SQL output, the table contains the record of all students except Vivek student because Vivek name does not contain the letter 'a' in any position.

The following query shows the record of those students from the Student table whose city name begins with the 'D' letter and ends with the 'I' letter:

Output of above query:

Roll_No First_Name City Percentage
101 Akash Delhi 89
103 Yash Delhi 89
104 Bhavna Delhi 78

As shown in the above SQL output, the table only contains the record of those students whose City is Delhi.

The following query shows the record of those students from the Student table whose Percentage start with the '7' digit:

Output of above query:

Roll_No First_Name City Age Percentage Grade
104 Bhavna Delhi 19 78 B1
105 Yatin Lucknow 20 75 B1

As shown in the above SQL output, the table only contains the record of those students whose marks is 78 and 75.

The following query shows the record of those students from the Student table whose First_Name contains 'a' at third position:

Output of above query:

Roll_No First_Name City Percentage Grade
101 Akash Delhi 89 A2
102 Bhavesh Kanpur 93 A1
104 Bhavna Delhi 78 B1

As shown in the above output, the table only contains the record of those students whose First_Name contains the character 'a' at the third position.


Next TopicCursor in SQL





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA