Careeroza
LIKE & Wildcards
The SQL LIKE Operator
LIKE searches for a pattern in a text column. It is used with two wildcards:
| Wildcard | Meaning | Example | Matches |
|---|---|---|---|
% | zero, one or many characters | 'Gr%' | Grocery, Green Tea |
_ | exactly one character | '_ondon' | London |
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;Starts With
Example
Customers whose name starts with "G":
SELECT * FROM Customers
WHERE CustomerName LIKE 'G%';Result: 1 row
| CustomerID | CustomerName | ContactName | City | Country |
|---|---|---|---|---|
| 7 | Golden Spoon | Mei Chen | NULL | Singapore |
Ends With
Example
Products ending in "kg":
SELECT ProductName FROM Products
WHERE ProductName LIKE '%kg';Result: 2 rows
| ProductName |
|---|
| Basmati Rice 5kg |
| Espresso Beans 1kg |
Contains
Example
Customers with "or" anywhere in the name:
SELECT CustomerName FROM Customers
WHERE CustomerName LIKE '%or%';Result: 3 rows
| CustomerName |
|---|
| Blue Harbor Foods |
| Fjord Supplies |
| Harbor Lights |
Single Character
Example
Cities with exactly six letters that end in "ondon":
SELECT DISTINCT City FROM Customers
WHERE City LIKE '_ondon';Result: 1 row
| City |
|---|
| London |
More Patterns
| Pattern | Finds values that... |
|---|---|
LIKE 'a%' | start with "a" |
LIKE '%a' | end with "a" |
LIKE '_r%' | have "r" in the second position |
LIKE 'a_%' | start with "a" and are at least 2 characters long |
LIKE 'a%o' | start with "a" and end with "o" |
Note: Case sensitivity depends on the database: MySQL and SQL Server are usually case-insensitive, PostgreSQL is case-sensitive (use ILIKE), SQLite is case-insensitive for ASCII letters.
SQL Server also supports character lists: [abc], ranges [a-f] and negation [^abc]. To search for a literal % or _, use ESCAPE: LIKE '50!%' ESCAPE '!'.