This article will discusses how to use the LIKE operator for ltering rows.
SELECT col1, col2, ... coln
FROM table
WHERE col3 LIKE "%some-string%"
-- The lesson queries are reproduced below for convenient copy/paste into the terminal.
-- Query 1
SELECT * from Actors WHERE FirstName LIKE "Jen%";
-- Query 2
SELECT * from Actors where FirstName LIKE "Jennifer%";
-- Query 3
SELECT * from Actors where FirstName LIKE "%";
-- Query 4SELECT * from Actors WHERE FirstName LIKE "_enn%";
-- Query 5
SHOW DATABASES LIKE "M%";
-- Query 6
SHOW TABLES LIKE "A%";
This Articles Contents
SELECT * from table WHERE column LIKE “Jen%”;
Say, you have forgotten the full name of a particular actor but remember that the name starts with the string “Jen”. You can check if there is an actor with a name that has “Jen” as the prefix by executing the following query in the terminal.
SELECT * from Actors WHERE FirstName LIKE "Jen%";
Note that we use the string “Jen%” and not “Jen”. In fact, if you used the latter, no rows will be matched.
The % symbol is a wildcard character that matches all strings. It can match zero or more characters. For instance, the following query returns one row and the wildcard character matches zero characters.
SELECT * from Actors where FirstName LIKE "Jennifer%";
As a final example, executing the following query will match all the rows in the table.
SELECT * from Actors where FirstName LIKE "%";
SELECT * from table WHERE FirstName LIKE “_enn%”;
We can use the underscore _ character to match exactly one character. For instance, the expression LIKE “_enn%” will match the string “Jennifer”.
SELECT * from Actors WHERE FirstName LIKE "_enn%";
SHOW DATABASES LIKE “M%”;
The LIKE clause can also be used with the SHOW command. For example:
SHOW DATABASES LIKE "M%";
Note that the LIKE statement is case sensitive. It shows different results for patterns “%M” and “%m”. We can also use LIKE when listing tables as the following example demonstrates:
SHOW TABLES LIKE "A%";