Javatpoint Logo
Javatpoint Logo

SQL Compare String

In this SQL section, we will discuss how to compare two or more strings in the Structured Query Language

We can compare two or more strings using the STRCMP string function, LIKE operator, and Equal operator.

STRCMP String function

STRCMP is a function of string that compares the specified two strings and gives 0 if the length of the first string is equal to the length of the second string. If the length of the first string is more than the length of the second string, then the function returns 1 otherwise -1.

Syntax of STRCMP Function

Examples of STRCMP String function

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

Output:

1

Example 2: The following SELECT query compares the length of two sentences passing in the STRCMP function:

Output:

-1

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

Output:

0

LIKE Operator

The LIKE operator matches the particular pattern with each row of the field and returns the matched values in the output. This operator is always used with the WHERE clause in the SQL statement.

Syntax of Like operator

Example of LIKE Operator

Let's take the following Student table:

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

Query 1: The following query shows the record of those students from the above Student table whose First_Name starts with a '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 students because their names begin with the B letter.

Query 2: 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

Equal Operator (=)

It is a type of comparison operator which shows the matched data from the given SQL table.

This operator is highly used by the database users in Structured Query Language.

This operator returns TRUE rows from the database table if the value of the column is the same as the value specified in the query.

Syntax of Equal operator:

Example of Equal Operator

Let's take the following Worker table:

Worker_Id Worker_Name Worker_City Worker_Salary Worker_Bonus
101 Anuj Ghaziabad 35000 2000
102 Tushar Lucknow 29000 3000
103 Vivek Kolkata 35000 2500
104 Shivam Goa 22000 3000

The following query shows the record of those workers from the worker table whose Worker_Salary is 35000:

Output:

Worker_Id Worker_Name Worker_City Worker_Salary Worker_Bonus
101 Anuj Ghaziabad 35000 2000
103 Vivek Kolkata 35000 2500

Next TopicSQL Minus





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