The SQL LIKE operator is used to search for specific patterns in text data. Unlike the equality operator (=), which matches an exact value, the LIKE operator allows you to search for partial matches.
The LIKE operator is commonly used in search features where users type only part of a name, city, product, or email address. SQL LIKE Operator practice questions with solutions help to understand the concepts.
For example:
- Search students whose names start with R
- Find cities ending with a
- Search products containing Laptop
- Filter customers whose email starts with john
The LIKE operator works together with special wildcard characters to perform flexible text searches.
What is the SQL LIKE Operator?
The LIKE operator compares a text value against a search pattern.
Basic Syntax
SELECT column_name
FROM table_name
WHERE column_name LIKE 'pattern';
Wildcards Used with LIKE
| Wildcard | Meaning | Example |
|---|---|---|
% | Represents zero or more characters | 'A%' |
_ | Represents exactly one character | 'A_' |
Examples
Names Starting with A
SELECT *
FROM students
WHERE name LIKE 'A%';
Names Ending with a
SELECT *
FROM students
WHERE name LIKE '%a';
Names Containing h
SELECT *
FROM students
WHERE name LIKE '%h%';
Sample Table Used Throughout This Chapter
students
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
| 106 | Ankit | 22 | Python | Delhi | 82 |
| 107 | Sneha | 21 | SQL | Noida | 89 |
Why Use LIKE?
The LIKE operator is useful for:
- Searching partial text
- Customer search
- Product search
- Employee search
- Email filtering
- Report generation
- Search boxes in applications
1. SQL Query to Display Students Whose Names Start with R
Problem Statement
Write an SQL query to display students whose names start with the letter R.
SQL Solution
SELECT *
FROM students
WHERE name LIKE 'R%';
Sample Output
| id | name | city | marks |
|---|---|---|---|
| 101 | Rahul | Delhi | 88 |
| 105 | Rohit | Faridabad | 90 |
Explanation
The % wildcard represents zero or more characters.
The pattern:
R%
means:
- Starts with R
- Followed by any number of characters
Therefore:
- Rahul ✅
- Rohit ✅
Concepts Covered
- LIKE Operator
%Wildcard- Pattern Matching
2. SQL Query to Display Students Whose Names Start with A
Problem Statement
Write an SQL query to display students whose names begin with A.
SQL Solution
SELECT *
FROM students
WHERE name LIKE 'A%';
Sample Output
| id | name | city | marks |
|---|---|---|---|
| 102 | Amit | Noida | 91 |
| 106 | Ankit | Delhi | 82 |
Explanation
The query searches for names beginning with the letter A.
The % wildcard matches any characters after A.
Concepts Covered
- LIKE
- Wildcards
- String Search
3. SQL Query to Display Students Whose Names End with a
Problem Statement
Write an SQL query to display students whose names end with the letter a.
SQL Solution
SELECT *
FROM students
WHERE name LIKE '%a';
Sample Output
| id | name | city | marks |
|---|---|---|---|
| 103 | Neha | Delhi | 95 |
| 104 | Priya | Gurgaon | 84 |
Explanation
The pattern:
%a
means:
- Any number of characters
- Ending with a
Therefore:
- Neha ✅
- Priya ✅
Concepts Covered
- LIKE
- Ending Characters
%Wildcard
4. SQL Query to Display Students Whose Names Contain the Letter “h”
Problem Statement
Write an SQL query to display students whose names contain the letter “h” anywhere in the name.
Sample Table
| id | name | course | city | marks |
|---|---|---|---|---|
| 101 | Rahul | Python | Delhi | 88 |
| 102 | Amit | Java | Noida | 91 |
| 103 | Neha | SQL | Delhi | 95 |
| 104 | Priya | Python | Gurgaon | 84 |
| 105 | Rohit | Java | Faridabad | 90 |
| 106 | Ankit | Python | Delhi | 82 |
| 107 | Sneha | SQL | Noida | 89 |
SQL Solution
SELECT *
FROM students
WHERE name LIKE '%h%';
Sample Output
| id | name | city | marks |
|---|---|---|---|
| 101 | Rahul | Delhi | 88 |
| 103 | Neha | Delhi | 95 |
| 105 | Rohit | Faridabad | 90 |
| 107 | Sneha | Noida | 89 |
Explanation
The pattern:
%h%
means:
- Any number of characters before h
- Followed by the letter h
- Followed by any number of characters
Therefore, SQL returns all names that contain h anywhere.
Concepts Covered
- LIKE Operator
%Wildcard- Searching Text
- Pattern Matching
5. SQL Query to Display Cities Starting with “D”
Problem Statement
Write an SQL query to display students who belong to cities starting with the letter “D”.
SQL Solution
SELECT *
FROM students
WHERE city LIKE 'D%';
Sample Output
| id | name | city | marks |
|---|---|---|---|
| 101 | Rahul | Delhi | 88 |
| 103 | Neha | Delhi | 95 |
| 106 | Ankit | Delhi | 82 |
Explanation
The pattern:
D%
means:
- Starts with D
- Followed by zero or more characters
Since Delhi starts with D, all students from Delhi are displayed.
Concepts Covered
- LIKE
- City Search
%Wildcard
6. SQL Query to Display Cities Ending with “a”
Problem Statement
Write an SQL query to display students who belong to cities ending with the letter “a”.
Sample Table
| id | name | city | course | marks |
|---|---|---|---|---|
| 101 | Rahul | Delhi | Python | 88 |
| 102 | Amit | Noida | Java | 91 |
| 103 | Neha | Delhi | SQL | 95 |
| 104 | Priya | Gurgaon | Python | 84 |
| 105 | Rohit | Faridabad | Java | 90 |
| 106 | Ankit | Delhi | Python | 82 |
| 107 | Sneha | Noida | SQL | 89 |
SQL Solution
SELECT *
FROM students
WHERE city LIKE '%a';
Sample Output
| id | name | city | marks |
|---|---|---|---|
| 102 | Amit | Noida | 91 |
| 107 | Sneha | Noida | 89 |
Explanation
The pattern:
%a
means:
- Any number of characters
- Ending with the letter a
Only Noida ends with a, so students from Noida are returned.
Concepts Covered
- LIKE
- Ending Pattern
%Wildcard
7. SQL Query to Display Courses Containing “Py”
Problem Statement
Write an SQL query to display students enrolled in courses that contain “Py”.
SQL Solution
SELECT *
FROM students
WHERE course LIKE '%Py%';
Sample Output
| id | name | course | marks |
|---|---|---|---|
| 101 | Rahul | Python | 88 |
| 104 | Priya | Python | 84 |
| 106 | Ankit | Python | 82 |
Explanation
The pattern:
%Py%
searches for the text Py anywhere in the course name.
All records where the course is Python are displayed.
Concepts Covered
- LIKE
- Pattern Search
- Text Matching
8. SQL LIKE with ORDER BY
Problem Statement
Write an SQL query to display students whose names start with A, sorted alphabetically.
SQL Solution
SELECT *
FROM students
WHERE name LIKE 'A%'
ORDER BY name ASC;
Sample Output
| id | name | city |
|---|---|---|
| 102 | Amit | Noida |
| 106 | Ankit | Delhi |
Explanation
SQL performs the following steps:
- Filters names beginning with A.
- Sorts the matching records alphabetically using
ORDER BY.
This type of query is useful in search results and reporting.
Concepts Covered
- LIKE
- ORDER BY
- ASC
- Sorting Results
9. SQL LIKE with Multiple Conditions
Problem Statement
Write an SQL query to display students whose:
- Name starts with R
- Marks are greater than 85
SQL Solution
SELECT *
FROM students
WHERE name LIKE 'R%'
AND marks > 85;
Sample Output
| id | name | marks |
|---|---|---|
| 101 | Rahul | 88 |
| 105 | Rohit | 90 |
Explanation
The query first searches for names beginning with R.
Then it applies the second condition:
marks > 85
Only students satisfying both conditions are displayed.
Concepts Covered
- LIKE
- WHERE
- AND
- Multiple Conditions
10. SQL LIKE Using “_” (Single Character Wildcard)
Problem Statement
Write an SQL query to display student names where the second character is “m”.
SQL Solution
SELECT *
FROM students
WHERE name LIKE '_m%';
Sample Output
| id | name |
|---|---|
| 102 | Amit |
Explanation
The underscore (_) wildcard represents exactly one character.
Pattern:
_m%
means:
- First character can be anything.
- Second character must be m.
- Remaining characters can be anything.
Only Amit matches this pattern.
Concepts Covered
- LIKE
_Wildcard- Pattern Matching
- Single Character Search
11. SQL Query to Display Names Containing the Letter “a”
Problem Statement
Write an SQL query to display students whose names contain the letter “a” anywhere in the name.
Sample Table
| id | name | course | city | marks |
|---|---|---|---|---|
| 101 | Rahul | Python | Delhi | 88 |
| 102 | Amit | Java | Noida | 91 |
| 103 | Neha | SQL | Delhi | 95 |
| 104 | Priya | Python | Gurgaon | 84 |
| 105 | Rohit | Java | Faridabad | 90 |
| 106 | Ankit | Python | Delhi | 82 |
| 107 | Sneha | SQL | Noida | 89 |
SQL Solution
SELECT *
FROM students
WHERE name LIKE '%a%';
Sample Output
| id | name | city |
|---|---|---|
| 101 | Rahul | Delhi |
| 102 | Amit | Noida |
| 103 | Neha | Delhi |
| 104 | Priya | Gurgaon |
| 106 | Ankit | Delhi |
| 107 | Sneha | Noida |
Explanation
The pattern:
%a%
means:
- Any number of characters
- Followed by a
- Followed by any number of characters
All names containing the letter a are displayed.
Concepts Covered
- LIKE
%Wildcard- Pattern Search
12. SQL Query to Display Cities Containing “Del”
Problem Statement
Write an SQL query to display students whose city contains the text “Del”.
SQL Solution
SELECT *
FROM students
WHERE city LIKE '%Del%';
Sample Output
| id | name | city |
|---|---|---|
| 101 | Rahul | Delhi |
| 103 | Neha | Delhi |
| 106 | Ankit | Delhi |
Explanation
The query searches for cities containing Del anywhere in the text.
Since Delhi contains “Del”, all students from Delhi are returned.
Concepts Covered
- LIKE
- Partial Text Search
%Wildcard
13. SQL LIKE with ORDER BY
Problem Statement
Write an SQL query to display students whose course starts with P, sorted by marks in descending order.
SQL Solution
SELECT *
FROM students
WHERE course LIKE 'P%'
ORDER BY marks DESC;
Sample Output
| id | name | course | marks |
|---|---|---|---|
| 101 | Rahul | Python | 88 |
| 104 | Priya | Python | 84 |
| 106 | Ankit | Python | 82 |
Explanation
The query performs two operations:
- Finds all courses starting with P.
- Sorts the matching records by marks from highest to lowest.
Concepts Covered
- LIKE
- ORDER BY
- DESC
- Pattern Matching
14. SQL LIKE Using Multiple Wildcards
Problem Statement
Write an SQL query to display student names that:
- Start with R
- End with t
SQL Solution
SELECT *
FROM students
WHERE name LIKE 'R%t';
Sample Output
| id | name |
|---|---|
| 105 | Rohit |
Explanation
Pattern:
R%t
means:
- Starts with R
- Ends with t
- Any number of characters may appear between them
Only Rohit satisfies this condition.
Concepts Covered
- LIKE
- Multiple Wildcards
- Text Matching
15. Real-World SQL LIKE Search Example
Problem Statement
An online education portal allows users to search for courses containing the word Java.
Write an SQL query to display all matching courses.
SQL Solution
SELECT *
FROM students
WHERE course LIKE '%Java%';
Sample Output
| id | name | course | city |
|---|---|---|---|
| 102 | Amit | Java | Noida |
| 105 | Rohit | Java | Faridabad |
Explanation
This query searches for courses containing the word Java.
The % wildcard allows additional characters before or after the search term.
Real-world applications include:
- Product Search
- Customer Search
- Employee Search
- Course Search
- Website Search Boxes
Concepts Covered
- LIKE
- Real-world Search
- Partial Matching
%Wildcard
Chapter Summary
In this chapter, you learned how to use the SQL LIKE operator to search for records using text patterns instead of exact values. The LIKE operator is one of the most frequently used SQL operators because it powers search functionality in websites, business applications, dashboards, and reporting systems.
You explored the use of wildcard characters:
%→ Matches zero or more characters_→ Matches exactly one character
You also learned how to combine the LIKE operator with WHERE, AND, and ORDER BY clauses to perform advanced searches.
Throughout this chapter, you covered:
- Introduction to the
LIKEOperator - Using the
%Wildcard - Using the
_Wildcard - Searching Names, Cities, and Courses
- Pattern Matching
- LIKE with WHERE
- LIKE with ORDER BY
- Multiple Conditions with LIKE
- Real-world Search Examples
Understanding the LIKE operator is essential for building flexible and user-friendly SQL queries.
Key Takeaways
- The
LIKEoperator searches for text patterns. %matches zero or more characters._matches exactly one character.LIKEis commonly used with theWHEREclause.LIKEcan search at the beginning, middle, or end of text.LIKEworks well withORDER BYfor sorted search results.- Multiple conditions can be combined using
ANDorOR. - Pattern matching is widely used in search systems.
LIKEimproves query flexibility compared to the=operator.- It is one of the most commonly asked SQL interview topics.
Frequently Asked Questions (FAQs)
1. What is the SQL LIKE operator?
The LIKE operator is used to search for records that match a specific text pattern.
Example:
SELECT *
FROM students
WHERE name LIKE 'A%';
2. What does % mean in SQL LIKE?
The % wildcard represents zero or more characters.
Example:
SELECT *
FROM students
WHERE city LIKE 'D%';
This matches:
- Delhi
- Dubai
- Dehradun
3. What does _ mean in SQL LIKE?
The _ wildcard represents exactly one character.
Example:
SELECT *
FROM students
WHERE name LIKE '_m%';
This matches names where the second character is “m”.
4. Can LIKE be combined with WHERE?
Yes.
Example:
SELECT *
FROM students
WHERE course LIKE 'P%';
The WHERE clause filters records using the specified pattern.
5. Can LIKE be combined with ORDER BY?
Yes.
Example:
SELECT *
FROM students
WHERE name LIKE 'R%'
ORDER BY marks DESC;
The records are filtered first and then sorted.
6. What is the difference between = and LIKE?
=searches for an exact value.LIKEsearches using patterns.
Example using =:
SELECT *
FROM students
WHERE city = 'Delhi';
Example using LIKE:
SELECT *
FROM students
WHERE city LIKE 'Del%';
7. Where is the LIKE operator used?
The LIKE operator is commonly used in:
- Website Search Boxes
- E-commerce Product Search
- Employee Search
- Customer Search
- CRM Software
- Student Management Systems
- Banking Applications
- Hospital Databases
- Inventory Systems
- Analytics Dashboards
8. Can LIKE be used with numbers?
Although LIKE is mainly intended for text data, it can also be used on numeric columns that are stored as text or automatically converted by the database.
However, for numeric comparisons, operators such as =, >, <, >=, and <= are generally recommended.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
