(SQL Tutorials for Citizen Data Scientist)
SQL LIKE Operator
In this tutorial you will learn how to retrieve the data based on a partial match.
Pattern Matching
So far, you’ve seen the conditions that identify an exact string, e.g. WHERE name='Lois Lane'
. But in SQL you can perform partial or pattern matching too using the LIKE
operator.
The LIKE
operator provides a measure of pattern matching by allowing you to specify wildcards for one or more characters. You can use the following two wildcard characters:
- The percent sign (
%
) — Matches any number of characters, even zero characters. - The underscore (
_
) — Matches exactly one character
Here’re some examples that show how to use the LIKE
operator with wildcards.
Statement | Meaning | Values Returned |
---|---|---|
WHERE name LIKE 'Da%' |
Find names beginning with Da | David, Davidson |
WHERE name LIKE '%th' |
Find names ending with th | Elizabeth, Smith |
WHERE name LIKE '%on%' |
Find names containing the on | Davidson, Toni |
WHERE name LIKE 'Sa_' |
Find names beginning with Sa and is followed by at most one character | Sam |
WHERE name LIKE '_oy' |
Find names ending with oy and is preceded by at most one character | Joy, Roy |
WHERE name LIKE '_an_' |
Find names containing an and begins and ends with at most one character | Dana, Hans |
WHERE name LIKE '%ar_' |
Find names containing ar, begins with any number of characters, and ends with at most one character | Richard, Karl |
WHERE name LIKE '_ar%' |
Find names containing ar, begins with at most one character, and ends with any number of characters | Karl, Mariya |
Let’s put the statements we’ve discussed above into real use by searching some records.
Consider we’ve an employees table in our database with the following records:
+--------+------------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+------------------+------------+--------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 5000 | 4 | | 2 | Tony Montana | 2002-07-15 | 6500 | 1 | | 3 | Sarah Connor | 2005-10-18 | 8000 | 5 | | 4 | Rick Deckard | 2007-01-03 | 7200 | 3 | | 5 | Martin Blank | 2008-06-24 | 5600 | NULL | | 6 | simons bistro | 2009-04-01 | 6000 | 1 | +--------+------------------+------------+--------+---------+
Now, let’s say you want to find out all the employees whose name begins with S letter.
Example
SELECT * FROM employees
WHERE emp_name LIKE 'S%';
After executing the query, you’ll get the output something like this:
+--------+------------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+------------------+------------+--------+---------+ | 3 | Sarah Connor | 2005-10-18 | 8000 | 5 | | 6 | simons bistro | 2009-04-01 | 6000 | 1 | +--------+------------------+------------+--------+---------+
In MySQL nonbinary string (CHAR
, VARCHAR
, TEXT
) comparisons are case-insensitive by default, whereas binary strings (BINARY
, VARBINARY
, BLOB
) comparisons are case-sensitive.
This means that if you search with WHERE name LIKE 'S%'
, you get all column values that start with S or s (as you can see we’ve got both “Sarah” and “simons”). However, if you want to make this search case sensitive you can use the BINARY
operator as follow:
Example
-- Syntax for MySQL Database
SELECT * FROM employees
WHERE BINARY emp_name LIKE 'S%';
Now, this statement will return only those employees whose name starts with capital S letter:
+--------+------------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+------------------+------------+--------+---------+ | 3 | Sarah Connor | 2005-10-18 | 8000 | 5 | +--------+------------------+------------+--------+---------+
Beginners Guide to SQL – SQL LIKE Operator
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.