![]() |
VOOZH | about |
The SQL LIKE operator is used to search for a specific pattern within a column’s text data. It works with wildcard characters to match partial strings, making it useful for flexible filtering.
Example: First, we create a demo SQL database and table, on which we will use the LIKE Operator command.
Query:
SELECT * FROM Employees
WHERE EmpName LIKE 'A%';
Output:
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE column_name LIKE pattern;
Wildcards are used with the LIKE operator to search for specific patterns in strings. Wildcard characters substitute one or more characters in the string. There are four wildcard characters in SQL:
| Pattern | Meaning |
|---|---|
| 'a%' | Match strings that start with 'a' |
| '%a' | Match strings that end with 'a' |
| 'a%t' | Match strings that contain the start with 'a' and end with 't'. |
| '%wow%' | Match strings that contain the substring 'wow' in them at any position. |
| '_wow%' | Match strings that contain the substring 'wow' in them at the second position. |
| '_a%' | Match strings that contain 'a' at the second position. |
| 'a_ _%' | Match strings that start with 'a and contain at least 2 more characters. |
Let's understand LIKE operator in SQL with examples. First, we will create a demo SQL database and table, on which we will use the SQL LIKE Operator command.
Retrieve SupplierID, Name and Address from suppliers table, where supplier name starts form Ca.
SELECT SupplierID, Name, Address
FROM Supplier
WHERE Name LIKE 'Ca%';
Output:
Retrieve entire table, where address contains Kungsgatan.
SELECT *
FROM Supplier
WHERE Address LIKE '%Kungsgatan%';
Output:
Retrieve the SupplierID, Name and Address of suppliers whose Name has "afé" starting from the second position.
SELECT SupplierID, Name, Address
FROM Supplier
WHERE Name LIKE '_afé%';
Output:
Retrieve suppliers from Madrid with names starting with "C":
SELECT SupplierID, Name, Address
FROM Supplier
WHERE Address LIKE '%Madrid%' AND Name LIKE 'C%';
Output:
To retrieve all suppliers whose name does not contain "Co"
SELECT SupplierID, Name, Address
FROM Supplier
WHERE Name NOT LIKE '%Co%';
Output:
Note: Case sensitivity with LIKE depends on the database, use BINARY in MySQL, COLLATE in others and ILIKE in PostgreSQL for case-insensitive matching.