Skip to content
Related Articles
Open in App
Not now

Related Articles

SQL Query to Match Any Part of String

Improve Article
Save Article
  • Last Updated : 10 Sep, 2021
Improve Article
Save Article

SQL Pattern Matching :

It is used for searching a string or a sub-string to find certain character or group of characters from a string. We can use LIKE Operator of SQL to search sub-string. The LIKE operator is used with the WHERE Clause to search a pattern in string of column. The LIKE operator is used in a conjunction with the two wildcards characters.

  1. Percentage sign( % ) : It represents zero, one or multiple characters of variable length.
  2.  Underscore ( _ ) : It represents one, single character of fixed length.

Example :

In this example we will create a schema for our database and named it as geeksforgeeks. After that we will create a table inside it with the name geeks_data and tried to search a sub string from the data of table.

Step 1: Create a database :

In order to create a database we need to use the CREATE operator.

CREATE DATABASE geeksforgeeks;

Step 2: Create a table inside the database :

In this step we will create the table geeks_data inside the geeksforgeeks database.

CREATE TABLE geeksforgeeks.geeks_data(id INT, 
                                    first_name VARCHAR(255),
                                    last_name VARCHAR(255),
                                    text_description  VARCHAR(255),
                                    PRIMARY KEY(id));

Step 3: Insert data into the table :

In order to insert the data inside the database we need to use INSERT operator.

INSERT INTO geeksforgeeks.geeks_data (id, first_name, last_name, text_description) 
VALUES (1, "Rahul", "Khanna", "I am a backend developer who also like to technical content writing");
id first_name last_name text_description
1 Rahul Khanna I am a backend developer who also like to technical content writing

Step 4: Searching the pattern using Like operator :

SELECT first_name FROM geeksforgeeks.geeks_data WHERE text_description LIKE '%backend%developer%';

Step 5: Output :

We will get the first_name where in the description backend developer is present.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!