Use Sophia to knock out your gen-ed requirements quickly and affordably. Learn more
×

LIKE to Search Data

Author: Sophia

what's covered
This tutorial explores using the LIKE operator within the WHERE clause of a SELECT statement to search specific patterns of text in three parts:
  1. Getting Started
  2. The % Operator
  3. The _ Operator

1. Getting Started

There are special operators that allow us to check if a value is in a range. The LIKE operator in a WHERE clause allows you use wildcards to check if an attribute’s value matches a specific string pattern. There are two different wildcards that can be used with the LIKE operator:

  • The percent sign (%) represents zero, one, or multiple characters or numbers.
  • The underscore (_) represents a single character or number.
These symbols can be used in combination with specific characters or numbers. If neither one of these signs is used with a LIKE clause, the LIKE clause would simply work in the same way as an equal (=) operator. If just a (%) is used in the LIKE clause, it would return all rows of data.

It is important to note that the string text is case sensitive with the content that is not in the wildcards, so the matches will only display if the query entry is the same as what is in the table data.

If we have a string ‘abc’ in a table, the following search terms will return the associated results when compared against 'abc':

  • ‘abc’ would return true, as the string matches exactly.
  • ‘a%’ would return true, as it looks for the letter ‘a’ and 0 or more characters displayed afterward.
  • ‘_b_’ would return true, as it looks for one character, then the letter b and the one more character.
  • ‘c’ would return false, as it is only looking at the letter c.
  • ‘_b’ would return false, as it looks for one character and then the letter b.
  • ‘%c’ would return true, as it looks for any number of characters and then the letter c at the end.

2. The % Operator

As shown above, the % searches for zero, one, or more characters as a replacement. It is the most common operator used in the LIKE clause. If we wanted to query our customer table and list all of the customers whose name starts with the letter L, we would use the % wildcard after the letter L:


SELECT * 
FROM customer
WHERE first_name like 'L%';

Query Result Example

If we wanted to list all of the customers who have their email in the domain gmail.com, we would have the % wildcard operator before @gmail.com:


SELECT * 
FROM customer
WHERE email like '%@gmail.com';

Query Result Example

This is the most common way to use the % operator to search for content. In the following tutorial, we’ll dive into more complex ways to work with wildcards.


3. The _ Operator

The underscore (_) operator matches a single wildcard character, unlike the % operator that has 0 or more. This allows us to query data to look for a specific length of data. For example, if we wanted to look for customers that have the state starting with C and having two characters, we can do the following:


SELECT * 
FROM customer
WHERE state LIKE 'C_';


Query Result Example

Another example is looking customers that live in a country that has three characters and starts with the letter U. If we used the % operator, it would search for 0 or more characters after U:


SELECT * 
FROM customer
WHERE country LIKE 'U%';

This would return some extra rows of countries, like the United Kingdom:


Query Result Example

If we changed the LIKE clause to use the two extra underscores to get 3 characters, it should look like:


SELECT * 
FROM customer
WHERE country LIKE 'U__';

Query Result Example

Video Transcript

try it
Your turn! Open the SQL tool by clicking on the LAUNCH DATABASE button below. Then enter in one of the examples above and see how it works. Next, try your own choices for the LIKE clause.

summary
The LIKE clause allows us to match text strings using the wildcards % and _ . You can use them in combination with one another to improve searches for specific content.

Source: Authored by Vincent Tran