loading

SQL Like

The SQL LIKE Operator

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:

  •  The percent sign % represents zero, one, or multiple characters
  •  The underscore sign _ represents one, single character

You will learn more about wildcards in the next chapter.

Example

Select all customers that starts with the letter “a”:

				
					SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
				
			

Syntax

				
					SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
				
			

Demo Database

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

The _ Wildcard

A single character is represented by the wildcard _.

Any character or number will do, but each _ only stands for a single character.

Example

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

The % wildcard represents any number of characters, even zero characters.

Example

Return all customers from a city that contains the letter ‘L’:

				
					SELECT * FROM Customers
WHERE city LIKE '%L%';
				
			

Starts With

Add the % at the end of the letter or phrase to retrieve records that begin with that particular letter or phrase.

Example

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.

Example

Return all customers that starts with ‘a’ or starts with ‘b’:

				
					SELECT * FROM Customers
WHERE CustomerName LIKE 'a%' OR CustomerName LIKE 'b%';
				
			

Ends With

Put the % at the start of the letter or phrase to retrieve records that finish with that particular letter or word.

Example

Return all customers that ends with ‘a’:

				
					SELECT * FROM Customers
WHERE CustomerName LIKE '%a';
				
			

Tip: You can also combine “starts with” and “ends with”:

Example

Return all customers that starts with “b” and ends with “s”:

				
					SELECT * FROM Customers
WHERE CustomerName LIKE 'b%s';
				
			

Contains

Add the % before and after the letter or phrase to return records that contain that particular letter or phrase.

Example

Return all customers that contains the phrase ‘or’

				
					SELECT * FROM Customers
WHERE CustomerName LIKE '%or%';
				
			

Combine Wildcards

Any wildcard can be used in conjunction with other wildcards, such as % and _.

Example

Return all customers that starts with “a” and are at least 3 characters in length:

				
					SELECT * FROM Customers
WHERE CustomerName LIKE 'a__%';
				
			

Example

Return all customers that have “r” in the second position:

				
					SELECT * FROM Customers
WHERE CustomerName LIKE '_r%';
				
			

Without Wildcard

In the event that no wildcard is used, the phrase must exactly match in order to provide a result.

Example

Return all customers from Spain:

				
					SELECT * FROM Customers
WHERE Country LIKE 'Spain';
				
			
Share this Doc

SQL Like

Or copy link

Explore Topic