SQL LIKE Operator

The SQL-like operator is used in WHERE the clause that compares a column value to similar values using wildcard operations. LIKE operator is used to filtering records and pattern matching for columns that are of VARCHAR data type. It can be used with SELECT, INSERT, UPDATE or DELETE statement in SQL.

We can also use the NOT operator with like when we want to negate the results of LIKE operator. It excludes rows from the result set that contains a specified pattern.

We can use two wildcards with LIKE Operator.

WildcardDescription
Percent Sign(%)Matches any string of any length (including zero length)
Underscore(_)Matches on a Single Character or number

Syntax of SQL Like Operator

expression [NOT] LIKE pattern [ ESCAPE 'escape_character' ];

The below table gives some examples of SQL Like operators.

LIKE OperatorDescription
WHERE column LIKE ‘b%’Returns all the values that start with “b”
WHERE column LIKE ‘%b’Returns all the values that end with “b”
WHERE column LIKE ‘%b%’Returns all the values for a column that starts and ends with any value, but have ‘b’ in between them
WHERE Column LIKE ‘_a%’Returns all the values for a column with the letter ‘a’ in the second position.
WHERE column LIKE ‘b%a’Returns all the value for the column that starts with the letter b and ends with letter a

Examples used in this tutorial are based on this Oracle SQL file that you can find on my GitHub page.

Using percent(%) sign Wildcard

LIKE Operator

Let’s see an example where we will use a percentage sign(%) wild card to select the data.

SELECT * FROM OT.CUSTOMERS_DATA WHERE NAME LIKE 'As%';
CUS_IDNAMEADDRESSWEBSITECRDT_LMT
110Assurant101 N Falahee Rd, Jackson, MIhttp://www.assurant.com650
111Assurano102 N Falahee Rd, Miami, FLhttp://www.assurano.com800

As we can see, two records in the table start with the letter As. Now we will select the data from the customer’s data table whose name ends with y using a percentage sign.

SELECT * FROM OT.CUSTOMERS_DATA WHERE NAME LIKE '%y';
CUS_IDNAMEADDRESSWEBSITECRDT_LMT
109DTE Energy8110 Jackson Rd, Ann Arbor, MIhttp://www.dteenergy.com200
112State Country700 Hidden Ridge, Irving, TXhttp://www.StateCountry.com800

Let’s see how we use the % wildcard multiple times within the same string.

SELECT * FROM OT.CUSTOMERS_DATA WHERE NAME LIKE '%y%';
CUS_IDNAMEADDRESSWEBSITECRDT_LMT
103Reynolds American1610 Betrayal Crt, Bangalore, Karhttp://www.reynoldsamerican.com5000
105Henry Schein1615 Crackers Crt, Bangalore – India, Karhttp://www.henryschein.com5000
109DTE Energy8110 Jackson Rd, Ann Arbor, MIhttp://www.dteenergy.com200
112State Country700 Hidden Ridge, Irving, TXhttp://www.StateCountry.com800

Here, we are trying to find the names in the customer data table which have y in between.

NOT LIKE Operator

Let’s look at some examples using NOT LIKE operator. It is going to return the results from CUSTOMERS_DATA table whose name column does not have y at the end.

SELECT * FROM OT.CUSTOMERS_DATA WHERE NAME NOT LIKE '%y';
CUS_IDNAMEADDRESSWEBSITECRDT_LMT
102Unum Group1608 Amadeus St, Bangalore, Karhttp://www.unum.com3500
103Reynolds American1610 Betrayal Crt, Bangalore, Karhttp://www.reynoldsamerican.com5000
104Group 1 Automotive1614 Crackers Rd, Bangalore – India, Karhttp://www.group1auto.com2300
105Henry Schein1615 Crackers Crt, Bangalore – India, Karhttp://www.henryschein.com5000
106Norfolk Southern1618 Footloose St, Bangalore – India, Karhttp://www.nscorp.com900
107Reinsurance Group of America1619 Footloose Rd, Bangalore – India, Karhttp://www.rgare.com900
108Public Service Enterprise Group1621 Gargon! Blvd, Bangalore – India, Karhttp://www.pseg.com600
110Assurant101 N Falahee Rd, Jackson, MIhttp://www.assurant.com650
111Assurano102 N Falahee Rd, Miami, FLhttp://www.assurano.com800

Let’s look at another example where we will select the data from the customer’s data table whose names do not have 'y in between.

SELECT * FROM OT.CUSTOMERS_DATA WHERE NAME NOT LIKE '%y%';
CUS_IDNAMEADDRESSWEBSITECRDT_LMT
102Unum Group1608 Amadeus St, Bangalore, Karhttp://www.unum.com3500
104Group 1 Automotive1614 Crackers Rd, Bangalore – India, Karhttp://www.group1auto.com2300
106Norfolk Southern1618 Footloose St, Bangalore – India, Karhttp://www.nscorp.com900
107Reinsurance Group of America1619 Footloose Rd, Bangalore – India, Karhttp://www.rgare.com900
108Public Service Enterprise Group1621 Gargon! Blvd, Bangalore – India, Karhttp://www.pseg.com600
110Assurant101 N Falahee Rd, Jackson, MIhttp://www.assurant.com650
111Assurano102 N Falahee Rd, Miami, FLhttp://www.assurano.com800

As we see from the above query, we are getting results from customers’ data tables whose name columns do not have y in between.

Using Underscore(_) wildcard

Let’s see how the underscore _ wildcard works in the Oracle LIKE condition. In this example, we are only trying to match the last character in the column name.

SELECT * FROM OT.CUSTOMERS_DATA WHERE NAME LIKE 'Assuran_';
CUS_IDNAMEADDRESSWEBSITECRDT_LMT
110Assurant101 N Falahee Rd, Jackson, MIhttp://www.assurant.com650
111Assurano102 N Falahee Rd, Miami, FLhttp://www.assurano.com800

When we use the underscore wildcard, we need to make sure that the length of the column to be compared is of the same length. Otherwise, Oracle won’t give us any results from the query.

Now, let’s take a look at another example where we will use an underscore wildcard to filter columns at the start and end of the string.

SELECT * FROM OT.CUSTOMERS_DATA WHERE NAME LIKE '_ssuran_';
CUS_IDNAMEADDRESSWEBSITECRDT_LMT
110Assurant101 N Falahee Rd, Jackson, MIhttp://www.assurant.com650
111Assurano102 N Falahee Rd, Miami, FLhttp://www.assurano.com800

Conclusion

In this blog post, we looked into how SQL LIKE operation can be used alongside WHERE operators in the Oracle database. We also looked into the percent(%) and underscore(_) wildcard.