4th Mar 2021 Updated: 30th Oct 2024 7 minutes read What Do the Operators LIKE and NOT LIKE Do? Andrew Bone SQL Text Functions Table of Contents Syntax of LIKE Operator Using Wildcards with LIKE The Underscore _ Wildcard The Percent % Wildcard NOT LIKE Operator Is LIKE Case-Sensitive? Using LIKE and NOT LIKE Beyond WHERE Clause Where to Find More Practice The SQL LIKE and NOT LIKE operators are used to find matches between a string and a given pattern. They are part of standard SQL and work across all database types, making it essential knowledge for all SQL users. Gaining an understanding of these operators will allow you to write better queries and demonstrate to potential employers that you have the skills required to filter through expansive data sets. The SQL LIKE is a SQL operator that checks whether or not a string contains a specified pattern. A simple example of this is when you try to find if a name column contains any four-letter name starting with J (such as “John”). The LIKE operator is often used in the WHERE clause of SELECT, DELETE, and UPDATE statements to filter data based on patterns. Becoming proficient in using the LIKE operator will allow you to parse through large databases with ease, and retrieve exactly the data you need. If you want to advance your SQL skills, try out our interactive course SQL Practice Set, where you will practice SQL JOINs, aggregations with GROUP BY, and other advanced topics in 88 hands-on practical exercises. Syntax of LIKE Operator Let’s examine how we can use the LIKE operator to filter the data returned, thereby retrieving only the desired records. Here is the syntax of the LIKE operator: SELECT column_1, column_2, ... column_n FROM table_name WHERE column_1 LIKE pattern Let’s apply this syntax to a practical example. Imagine we have a table called person_info containing information about people’s first name, last name, and age. FirstNameLastNameAge TimSamuels46 JohnPeterson23 TomWaters42 SarahHenderson48 PaulJohnson34 AndrewWade39 SarahSmith30 LynnPeterson27 Let’s use the LIKE operator to extract the information for people with the last name “Peterson”. SELECT FirstName, LastName, Age FROM person_info WHERE LastName LIKE 'Peterson' Executing this SQL query would yield the following result set: FirstNameLastNameAge JohnPeterson23 LynnPeterson27 This is a simple use case for the LIKE operator. For simple cases like this, we could have also written: SELECT FirstName, LastName, Age FROM person_info WHERE LastName = 'Peterson' So, what is the difference between using LIKE and equals? Equals = is a comparison operator that operates on numbers and strings. When comparing strings, the equals operator compares whole strings. In comparison, LIKE compares character by character through the use of wildcards, which will be discussed in detail in this article. Using Wildcards with LIKE We will look at two wildcard characters: percent % and underscore _. Wildcard characters are used to substitute for one or more characters in a pattern string: The percent % wildcard substitutes for one or more characters in a string. The underscore _ wildcard substitutes for exactly one character in a string. The Underscore _ Wildcard Let’s look at the underscore _ wildcard first and apply it to our person_info table. Imagine we want to retrieve, from the table person_info, the first names of the persons with the following conditions: The FirstName must start with the letter “T”, The third letter of FirstName must be “m”, and The second letter FirstName can be anything. We can use the following query: SELECT FirstName FROM person_info WHERE FirstName LIKE 'T_m'; The result of this query is: SELECT FirstName FROM person_info WHERE FirstName LIKE 'T_m'; The result of this query is: FirstName Tim Tom Notice how the second letter of the name can be anything. Our SQL query is ignoring that letter and looking for the pattern we have specified. Let’s look at another example. This time, we will substitute two characters with two underscore _ wildcard characters. SELECT FirstName, LastName FROM person_info WHERE LastName LIKE 'Wa__'; Executing this query retrieves a single record from our table. FirstNameLastName AndrewWade Notice how Andrew Wade matches the pattern but not Tom Waters. By placing two underscores after Wa, we explicitly specify that the LastName we are looking for is 4 characters long. The underscore wildcard can be placed anywhere in the pattern you are looking for. _om, T_m, or To_, are all valid patterns. The Percent % Wildcard Let’s now look at the percent % wildcard in detail and apply it to our person_info table. The percent % wildcard is used to substitute for multiple characters. As an example, imagine we want to find all the people whose last name ends in “son”. To achieve this, we can simply write the following query: SELECT FirstName, LastName FROM person_info WHERE LastName LIKE '%son'; The result of this query is: FirstNameLastName JohnPeterson SarahHenderson PaulJohnson LynnPeterson Notice how the number of characters before “son” does not matter with this wildcard. If the pattern you are looking for is embedded in the middle of another string, you can use the percent wildcard at the beginning and at the end of the pattern. For example, to find all employees whose last names contain “er”, use the following query with the pattern '%er%'. SELECT FirstName, LastName FROM person_info WHERE LastName LIKE '%er%'; Executing this query yields the following result set: FirstNameLastName JohnPeterson TomWaters SarahHenderson LynnPeterson Let’s combine the use of both _ and %. Say we want to find all the first names whose second character is the letter “a”. We can do this with the following query: SELECT FirstName, LastName FROM person_info WHERE FirstName LIKE '_a%' Executing this query results in this set: FirstNameLastName SarahHenderson PaulJohnson PaulaSmith The LIKE operator is most commonly used in conjunction with the WHERE clause. However, it can be used anywhere you use an expression in SQL. NOT LIKE Operator SQL NOT LIKE operator behaves as you might expect, essentially returning the opposite of what the LIKE operator would. Let’s substitute LIKE with NOT LIKE in one of our earlier examples and see the effect. SELECT FirstName, LastName, Age FROM person_info WHERE LastName NOT LIKE 'Peterson'; Here is the result set: FirstNameLastNameAge TimSamuels46 TomWaters42 SarahHenderson48 PaulJohnson34 AndrewWade39 PaulaSmith30 As you can see, using NOT LIKE negates the pattern you specify. This is similar to other NOT operators you see in SQL. In this case, the NOT LIKE operator retrieves data for all persons whose last name is not Peterson. Is LIKE Case-Sensitive? For the above examples, the name “Peterson” was used with a capital letter. Depending on the variant of SQL you are using, the LIKE operator may be case-sensitive. MySQL, PostgreSQL, and Oracle are all case-sensitive by default. SQL Server can be a little trickier. By default, SQL Server is not case-sensitive. However, it is possible to create a case-sensitive SQL Server database and even make specific table columns case-sensitive. Using LIKE and NOT LIKE Beyond WHERE Clause It is common to see LIKE and NOT LIKE used in combination with a WHERE clause. However, the LIKE operator can be used in any valid SQL statement, such as SELECT, INSERT INTO, UPDATE, or DELETE. Let’s see this with an example executed against our person_info table. Consider the following SQL query: SELECT FirstName, FirstName LIKE 'T%' AS starts_with_t FROM person_info; The SELECT statement here does not just retrieve the data from the FirstName column. Paired with LIKE, the second column selected in this query returns a Boolean value based on FirstName for each record in the table. The pattern we’ve specified is any string starting with the letter “T”, so this query checks every FirstName to see if it starts with the letter “T” and returns true (1) if it does, false (0) otherwise. Executing this query generates the following result set: FirstNamestarts_with_t Tim1 John0 Tom1 Sarah0 Paul0 Andrew0 Paula0 Lynn0 Where to Find More Practice We've explored a variety of scenarios where the LIKE operator can enhance your SQL querying skills. Mastering the SQL LIKE operator is crucial, especially for job seekers in SQL-dependent roles, as it's often a topic in SQL interviews. The key to proficiency with the LIKE operator and its wildcard characters is consistent practice. To truly elevate your understanding, we recommend the SQL Practice Set course on LearnSQL.com, featuring 88 targeted exercises. Don’t wait to boost your skills—enroll in the course today and move closer to SQL mastery! Tags: SQL Text Functions