REQUEST COMPLIMENTARY SQLS*PLUS LICENCE
SQL condition LIKE
The SQL condition LIKE allows you to use wildcards to match the pattern in the query. The LIKE condition is used in the WHERE clause of the SELECT, INSERT, UPDATE or DELETE operator.
Syntax for LIKE condition in SQL
expression_id LIKE pattern_id [ ESCAPE 'escapecharacter_id' ]
where:
- expression_id – Symbolic expression such as a field or column
- pattern_id – Symbolic expression that contains a comparison with the template. Substitution characters that you can select:
- ESCAPE ‘escapecharacter_id’ – It’s optional. This allows you to match templates with literal wildcards such as % or _.
Using % wildcard in LIKE condition
Let’s look at how the % wildcard works in the LIKE SQL condition. Remember that the % wildcard matches any string of any length (including zero length).
In this first example we want to find all entries in the customers table where the l_name of the client starts with ‘C’.
In this example we have a customer table with the following data:
Symbol | Explanation |
---|---|
% | Allows comparing any string of any length (including zero length) |
_ | Allows matching a single character |
custom_id | f_name | l_name | fav_website |
---|---|---|---|
4000 | Justin | Bieber | google.com |
5000 | Selena | Gomez | bing.com |
6000 | Mila | Kunis | yahoo.com |
7000 | Tom | Cruise | oracle.com |
8000 | Johnny | Depp | NULL |
9000 | Russell | Crowe | google.com |
Enter the following SQL statement.
SELECT *
FROM customs
WHERE l_name LIKE 'C%'
ORDER BY l_name;
Two entries will be selected. Here are the results that you should get.
custom_id | f_name | l_name | fav_website |
---|---|---|---|
7000 | Tom | Cruise | oracle.com |
9000 | Russell | Crowe | google.com |
In this example, records of the customers table are returned where last_name starts with ‘C’. As you can see, records by Cruise and Crowe names have been returned.
Since the LIKE condition is case insensitive, the next SQL statement will return the same results.
SELECT *
FROM customs
WHERE l_name LIKE 'c%'
ORDER BY l_name;
Using multiple % wildcards in LIKE condition
You can also use the % wildcard multiple times with the LIKE condition. Using the same table with the following data:
custom_id | f_name | l_name | fav_website |
---|---|---|---|
4000 | Justin | Bieber | google.com |
5000 | Selena | Gomez | bing.com |
6000 | Mila | Kunis | yahoo.com |
7000 | Tom | Cruise | oracle.com |
8000 | Johnny | Depp | NULL |
9000 | Russell | Crowe | google.com |
Let’s try to find all the last_name values from the customers table where last_name contains the letter ‘e’.
Enter the following SQL statement:
SELECT l_name
FROM customs
WHERE l_name LIKE '%e%'
ORDER BY l_name;
Three entries will be selected. Here are the results that you should get.
l_name |
---|
Bieber |
Gomez |
Depp |
In this example, the names Bieber, Gomez and Depp contain the letter ‘e’.
Using the wildcard _ in the condition LIKE
Next, let’s look at how the _ (underscore character) substitution works in the LIKE condition. Remember that the wildcard _ looks for exactly one character, unlike the % wildcard.
Using the table with the following data:
cat_id | cat_name |
---|---|
25 | Deli |
50 | Produce |
75 | Bakery |
100 | General Merchandise |
125 | Technology |
Let’s try to find all the records from the category table, where cat_id is 2 digits long and ends in ‘5’. Enter the following SQL statement.
SELECT *
FROM cats
WHERE cat_id LIKE '_5';
Two records will be selected. Here are the results you should get.
cat_id | cat_name |
---|---|
25 | Deli |
75 | Bakery |
In this example, there are 2 entries that will match the template – cat_id with values 25 and 75. Note that cat_id equal to 125 was not selected because the symbol _ matches only one character.
Using multiple wildcards _ in LIKE condition
If you want to match a three-digit value ending in “5”, you will need to use the _ double wildcard. You can modify your query as follows.
SELECT *
FROM cats
WHERE cat_id LIKE '__5';
Now you will return cat_id value equal to 125.
cat_id | cat_name |
---|---|
125 | Technology |
Using NOT operator with LIKE condition
Next, let’s look at an example of using the NOT operator with the LIKE condition. In this example, we have a table with the following data:
suppl_id | suppl_name | city_id | state_id |
---|---|---|---|
100 | Yandex | Moscow | Russian |
200 | Lansing | Michigan | |
300 | Oracle | Redwood City | California |
400 | Bing | Redmond | Washington |
500 | Yahoo | Sunnyvale | Washington |
600 | DuckDuckGo | Paoli | Pennsylvania |
700 | Qwant | Paris | France |
800 | Menlo Park | California | |
900 | Electronic Arts | San Francisco | California |
Let’s take a look at all the entries in the suppliers table where supplier_name does not contain literal ‘o’. Enter the following SQL statement.
SELECT *
FROM suppls
WHERE suppl_name NOT LIKE '%o%';
4 entries will be selected. Here are the results that you should get.
suppl_id | suppl_name | city_id | state_id |
---|---|---|---|
100 | Yandex | Moscow | Russian |
300 | Oracle | Redwood City | California |
400 | Bing | Redmond | Washington |
700 | Qwant | Paris | France |
In this example, there are four entries in the suppliers table where suppl_name does not contain literal ‘o’.
Using escape-symbols with LIKE condition
It is important to understand how to use the “escape-symbol” when it matches the template. You can screen % or _ and search for literal versions.
Let’s say you wanted to find % as a literal in the LIKE condition. You can do this with the escape-symbol. In our example we will use ! as an escape-symbol in the LIKE condition.
NOTE: You can only define an escape-character as one character. It is best to choose a character that will not appear in your data very often, for example ! or #.
In this example, we have a test table with the following data:
test1_id | test1_value |
---|---|
1 | 10% |
2 | 25% |
3 | 100 |
4 | 99 |
We could return all entries from the test table where test1_value contains literal %. Enter the following SQL statement.
SELECT *
FROM test1
WHERE test1_value LIKE '%!%%' escape_id '!
Here are the results you should get.
test1_id | test1_value |
---|---|
1 | 10% |
2 | 25% |
In this example, the ! character is defined as an escape-symbol. The first and last % values in the LIKE condition are treated as normal wildcards. !% is shielded by %, so it is treated as a literal value of %.
You can additionally change the above example and return only those test_values that start with 1 and contain a literal %. Enter the following SQL statement:
SELECT *
FROM test
WHERE test_value LIKE '1%!%%' escape '!
Here are the results you should get.
test1_id | test1_value |
---|---|
1 | 10% |
This example, this time, will only return one record. Because there is only one test_value that starts with 1 and contains literal %.
How to use the SQL LIKE Condition
MORE NEWS
PreambleNoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...
PreambleMongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...
5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...
The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...
PreambleMS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...
First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...
PreambleAtom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...
PreambleMongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...
FlexibilityOne of the most advertised features of MongoDB is its flexibility. Flexibility, however, is a double-edged sword. More flexibility means more...
PreambleSQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...
PreambleWriting an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...
PreambleOracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...
PreambleIBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...
What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....
PreambleIf you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...
PreambleStarting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...