LIKE Function in SQL

The Like(String) is a function in Structured Query Language which matches the pattern with the string. This function return 0 if the string is not matched with the pattern, otherwise 1. We can also use the Like function with the columns of the table.

Syntax of LIKE String Function

Syntax1: This syntax uses the LIKE function with two columns of the SQL table:

In the syntax, we have to define the name of that column whose values we want to match with the given pattern.

Syntax3: This syntax uses the LIKE function with two strings:

Examples of LIKE String function

Example 1: The following SELECT query compares the length of JAVATPOINT and EXAMPLES strings:

Output:

0

Example 2: The following SELECT query matches the string 'Javatpoint is good' with the given pattern:

Output:

1

Example 3: The following SELECT query compares the length of two cities:

Output:

0

Example 4: This example uses the LIKE function with the SQL table

In this example, we are going to create a new SQL table on which we have to execute the LIKE function for matching the column values with the pattern.

The syntax for creating the new table in the SQL database is as follows:

The following CREATE statement creates the Student_Grade table:

The below INSERT queries insert the recLIKEs of students with grades and marks in the Student_Grade table:

The following SELECT statement displays the inserted recLIKEs of the above Student_Grade table:


Roll_NoFirst_NameLast_NameFirst_CitySecond_CityNew_CityHindi_MarksMaths_MarksGrade
10AmanSharmaLucknowChandigarhGhaziabad8895A2
02VishalSharmaChandigarhGhaziabadGhaziabad9582A1
07RajGuptaDelhiGhaziabadLucknow9195A1
04YashSinghaniaGhaziabadDelhiLucknow8582A2
11VinayRoyDelhiKanpurGhaziabad9597A1
16ManojGuptaGhaziabadMeerutChandigarh9590B1
19RamGuptaLucknowGhaziabadChandigarh8995A2

The following SELECT query matches the Worker_First_Name column of the above table with the specified pattern:

Output:

Worker_First_NameCompare_Firstname_City
Arush0
Bulbul0
Saurabh0
Shivani0
Avinash1
Shyam0

The following SELECT query uses LIKE() with the Worker_first_Name and Worker_Last_Name columns of the above Worker_Info table:

This SQL statement matches the name of first name and last name columns.

Output:

Worker_First_NameWorker_Last_NameLIKE_Firstname_Lastname
ArushSharma0
BulbulRoy0
SaurabhRoy0
ShivaniSinghania0
AvinashSharma0
ShyamBesas0