Table of Contents
ToggleBelow is a sample table of Indian people with their names, cities, and states. This table is used to demonstrate how SQL queries work with the SQL LIKE operator.
| ID | Name | City | State |
|---|---|---|---|
| 1 | Amit Kumar | Delhi | Delhi |
| 2 | Anjali Verma | Mumbai | Maharashtra |
| 3 | Manish Yadav | Mumbai | Maharashtra |
| 4 | Madhuri Joshi | Bhopal | Madhya Pradesh |
| 5 | Mohan | Jaipur | Rajasthan |
The SQL LIKE operator is used in the WHERE clause to search for specific patterns in a column’s data. It is highly useful for flexible and dynamic filtering of text data in databases.
To find all people whose names start with the letter “A” in our Indian people table, use the % wildcard at the end of the pattern.
SELECT * FROM IndianPeople
WHERE Name LIKE 'A%';
Output: The result will include all people whose names start with “A”:
| ID | Name | City | State |
|---|---|---|---|
| 1 | Amit Kumar | Delhi | Delhi |
| 2 | Anjali Verma | Mumbai | Maharashtra |
To find all people whose city contains the letter “M”, use the % wildcard at both ends of the pattern.
SELECT * FROM IndianPeople
WHERE City LIKE '%M%';
Output: The result will include people who live in cities that contain the letter “M”:
| ID | Name | City | State |
|---|---|---|---|
| 3 | Manish Yadav | Mumbai | Maharashtra |
| 4 | Madhuri Joshi | Bhopal | Madhya Pradesh |
You can use the underscore (_) wildcard to match a single character. For example, if we want to find all people whose names are 5 characters long and start with “M”:
SELECT * FROM IndianPeople
WHERE Name LIKE 'M____';
Output: The result will include all names that start with “M” and have exactly 5 characters.
| ID | Name | City | State |
|---|---|---|---|
| 5 | Mohan | Jaipur | Rajasthan |
