JournalSQLFiltering and sets3 min
For Those Who Do Not Know LIKE and the Difference Between LIKE and IN
Of course, the first question you ask yourself is what wildcards are and what they mean.
- LIKE
- NOT LIKE
- wildcards
- IN
Wildcards
Of course, the first question you ask yourself is what wildcards are and what they mean.
They are something like a playing card in Uno, for example, or another game.
The advantage of that card is that it can take multiple values and act as several things at the same time.
Using them in SQL means specifying a pattern of values that we can use in the WHERE clause to check whether values match that condition or not.
Types of Wildcards
We have two types of wildcards (as far as I know):
- The percent sign
%: matches zero, one, or more characters, numbers, or symbols. - The underscore
_: matches a single character, number, or symbol.
Example
Suppose we have a people table like this:
id | name
---|-----
1 | Marwan
2 | Salah
3 | Yehia
4 | Islam
5 | Mahmoud
6 | Mayar
7 | Samir
8 | Israa
9 | MahmoodUsing LIKE
To use these wildcards, we must use the LIKE operator in the WHERE clause instead of other operators like =, >=, etc.
If I ask you to get all people whose name starts with "Ma", you would write:
SELECT *
FROM people
WHERE name LIKE 'Ma%'The output will be:
id | name
---|-----
1 | Marwan
5 | Mahmoud
6 | Mayar
9 | MahmoodNow, what if I ask you to get all people named Mahmoud/Mahmood regardless of the second-to-last letter?
You would write:
SELECT *
FROM people
WHERE name LIKE 'Mahmo_d'Don't forget that the underscore _ matches only a single character. Or you could also write:
SELECT *
FROM people
WHERE name LIKE 'Mahm%d'That way, between Mahm and the last letter d, I leave room for any characters in the middle if I'm sure the output will be accurate.
Using NOT LIKE
It is the same idea as LIKE but the opposite—more precisely, it checks if the column we are comparing does not match any of those patterns.
If I ask you to get the people whose names do not end with the letter d or the letter r, you would write:
SELECT *
FROM people
WHERE name NOT LIKE '%d'
OR name NOT LIKE '%r'And the output will be:
id | name
---|-----
1 | Marwan
2 | Salah
3 | Yehia
4 | Islam
8 | IsraaThe Difference Between LIKE and IN, and When to Use Them
The difference between them is huge, of course.
We use LIKE when we want to match a pattern or range of string patterns that I define and check which values match that pattern. The way it works is by comparing character by character—or more accurately, matching part of a word.
However, I use IN when I know the exact values I want to compare against, and I have a specific list of values; if it finds a match, the WHERE condition is satisfied.
Example: If I ask you to get the people whose names are Mahmoud or Mahmood, you would write:
SELECT *
FROM people
WHERE name IN ('Mahmoud', 'Mahmood')Expected output:
id | name
---|-----
5 | Mahmoud
9 | MahmoodA Quick Tip
Never use LIKE instead of =, for two reasons:
LIKEonly compares strings/text, and it compares these strings based on character representation, which makes a difference if we compare, for example, Latin characters with their English equivalents such asäandae[Reference].- With
=, we can compare all data types, whether text (string), whole numbers (Integer), decimal numbers (float,double), or others.