Up until now, we’ve done searches where we’ve known the exact criteria we are looking for. But what if we are querying our database and don’t know exactly what we are looking for? Perhaps you remember the first part of the name brand but can’t quite recall it exactly. Good news! That’s not a problem! We can get close enough by returning similar or like results.
Let’s do another search in our online retailer database for a specific pair of pants.
Our memory is a little fuzzy, but we can recall that the brand name started with the letter ‘B.’ For cases with uncertainty, we can use what’s known as a wildcard, which is represented with a percentage sign (%). Let’s see what the following SQL query pulls up and then break down the syntax:
SELECT * FROM Inventory WHERE Brand LIKE ’B%’;
Our result set:
Anything without a ‘B’ in the first position of the Brand has been excluded from our result set, and we are left with only brand names that begin with ‘B.’
In the syntax, we use the
WHERE as we normally do, but note that where we used an equal sign before is now replaced with a
LIKE. This signals that we’re not looking for an exact match but instead similar or like matches. The percentage sign, or wildcard, comes after the ‘B’ and will return any result where the value in the Brand field begins with a ‘B’ followed by any amount of text. In this case, both “Banana Brand” and “Boga” match.
Close enough for jazz! Bananas Brand was what we were looking for!
Explore on your own with an SQL Fiddle of today’s lesson!
Share with friends