In a WHERE clause, the LIKE operator is used to look for a given pattern in a column.
The LIKE operator is frequently combined with one of two wildcards:
You will learn more about wildcards in the next chapter.
Select all customers that starts with the letter “a”:
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1 |
Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
3 | Antonio Moreno TaquerÃa | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
4 |
Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |
A single character is represented by the wildcard _.
Any character or number will do, but each _ only stands for a single character.
Return all customers from a city that starts with ‘L’ followed by one wildcard character, then ‘nd’ and then two wildcard characters:
SELECT * FROM Customers
WHERE city LIKE 'L_nd__';
The % wildcard represents any number of characters, even zero characters.
Return all customers from a city that contains the letter ‘L’:
SELECT * FROM Customers
WHERE city LIKE '%L%';
Add the % at the end of the letter or phrase to retrieve records that begin with that particular letter or phrase.
Return all customers that starts with ‘La’:
SELECT * FROM Customers
WHERE CustomerName LIKE 'La%';
Tip: You can also combine any number of conditions using AND or OR operators.
Return all customers that starts with ‘a’ or starts with ‘b’:
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%' OR CustomerName LIKE 'b%';
Put the % at the start of the letter or phrase to retrieve records that finish with that particular letter or word.
Return all customers that ends with ‘a’:
SELECT * FROM Customers
WHERE CustomerName LIKE '%a';
Tip: You can also combine “starts with” and “ends with”:
Return all customers that starts with “b” and ends with “s”:
SELECT * FROM Customers
WHERE CustomerName LIKE 'b%s';
Add the % before and after the letter or phrase to return records that contain that particular letter or phrase.
Return all customers that contains the phrase ‘or’
SELECT * FROM Customers
WHERE CustomerName LIKE '%or%';
Any wildcard can be used in conjunction with other wildcards, such as % and _.
Return all customers that starts with “a” and are at least 3 characters in length:
SELECT * FROM Customers
WHERE CustomerName LIKE 'a__%';
Return all customers that have “r” in the second position:
SELECT * FROM Customers
WHERE CustomerName LIKE '_r%';
In the event that no wildcard is used, the phrase must exactly match in order to provide a result.
Return all customers from Spain:
SELECT * FROM Customers
WHERE Country LIKE 'Spain';
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.