It has been a while since I posted anything, so this one is going to be really simple and short just to warm up. This is going to be about wild cards.
And let's get started :)
Wild Cards
A wild card is basically a way of searching through strings for characters. Wild cards use the "LIKE" operator. Most known and used wild cards are "%" and "_". If you are a programmer you are probably familiar with regular expressions and these wild cards work like them. Here is briefly how they work.
"%": represents zero or more than one character.
"_": represents only one character.
If you do not know what the heck i am talking about here, relax this is normal. These examples will clear any confusion.
"%"
In the example below, we are telling sql to get the data from the table "customer" where the column "name" records start with the letter "m" and continue with any characters.
SELECT * FROM customers WHERE name LIKE "m%";
here is another example. Here we are basically telling sql to get the records from the column "name" that ends with the letters "ud" and can start with any characters.
SELECT * FROM customers WHERE name LIKE "%ud";
"_"
As i said the "_" will represent one character.
The query below will get the data that starts by any character followed by the pattern "_ohamed".
SELECT * FROM customers WHERE name LIKE "_ohamed";
Another example
So we are telling sql here to get any result from the column name that starts with the pattern "ahme" and ends with any character.
SELECT * FROM customers WHERE name LIKE "ahme_";
And, of course, we can combine the two of them to get more advanced.
This one is a little tricky. We are telling sql to get the data from the column email which starts with any number of characters and ends with only one character after "o". As you may have guessed this will retrieve any ".com" email.
SELECT * FROM customers WHERE email LIKE "%.co_";
Conclsuion
you can see how simple it is, but there is a lot to add to this if you want to dive deep in SQL, consider checking out my course on gumroad:)
Top comments (2)
Another important thing is escaping wildcards. I think it is
[]
, soI will cover this in the upcoming posts :)