SQL LIKE Operator
The LIKE operator in SQL is used to search for a specific pattern in a column's text data. It is commonly used with the WHERE clause to filter rows that match a pattern using wildcard characters.
Wildcards Used with LIKE
Employee Table
Example: Using % Wildcard
Find employees whose city starts with 'A'.
Output:
Example: Using _ Wildcard
Find employees whose names have 'n' as the second latter.
Output:
Wildcards Used with LIKE
Wildcard | Description |
---|---|
% | Matches zero, one or multiple characters |
_ | Matches exactly one character |
These wildcards let you write flexible queries to find data based on patterns, rather than exact matches.
Syntax
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE column_name LIKE pattern;
Employee Table
ID | EmpName | City | Salary |
---|---|---|---|
1 | Tom | ABC | 7000 |
2 | Enna | XYZ | 5000 |
3 | Genny | ABC | 8000 |
Example: Using % Wildcard
Find employees whose city starts with 'A'.
SELECT * FROM employee
WHERE City LIKE 'A%';
Output:
ID | EmpName | City | Salary |
---|---|---|---|
1 | Tom | ABC | 7000 |
3 | Genny | ABC | 8000 |
Explanation: A% matches all cities that begin with the letter 'A', followed by any characters.
Example: Using _ Wildcard
Find employees whose names have 'n' as the second latter.
SELECT * FROM employee
WHERE EmpName LIKE '_n%';
Output:
ID | EmpName | City | Salary |
---|---|---|---|
2 | Enna | XYZ | 5000 |
Explanation: _n% matches names where the second letter is 'n'.
Key Point
- Search names, emails, cities with partial matches
- Case-insensitive in some databases like MySQL (by default)
- Combine with NOT for inverse matches:
WHERE column_name NOT LIKE 'A%';