Pattern Matching with LIKE
= needs the whole value, exactly. But you often know only a part of it — names starting with A, anything containing “kumar”. like compares text against a pattern, and the pattern is built from just two special characters.
1The two wildcards
Including none at all. Stands for a run of any length.
One, and it must be there. A single underscore.
* and ?. SQL uses % and _. And % here has nothing to do with percentages or with the % remainder operator in Python.2Starting with something
'A%' reads as: an A, then anything. Only Anjali qualifies.
3Ending with something
Anything, then an a. Put the % at the front for “ends with” and at the back for “starts with” — the wildcard marks the part you do not care about.
4Containing something
A wildcard at both ends means the letter can be anywhere:
Ten of the twelve names contain an a somewhere. Only Bhim and Tom do not.
5Counting characters with _
Four underscores and nothing else means a name of exactly four characters:
Ravi, Bhim, Pema and Nima. Tom, with three letters, does not match — each _ must be filled by exactly one character, so the count has to be right.
The two wildcards can be combined:
Starts with S, ends with n, anything in between. Sohan matches; Singtam would too, if it were a name in this column.
6LIKE works on dates too
A date is stored as 'YYYY-MM-DD', and like is happy to treat that as text — which gives you a neat way to match a year, a month or a day without any date functions at all.
Everyone born in 2008 — the year is at the front:
Everyone born in March, whatever the year — the month sits between two hyphens:
And everyone born on the 20th of a month — the day is at the end:
The underscore is useful here too, because the parts of a date have a fixed width. Two digits, a hyphen, two digits — so '2010-__-__' is another way to say “born in 2010”:
'%03%' rather than '%-03-%' and you would also match a day of 03, or the 03 inside a year like 2003. Including the hyphens pins the digits to the month position, which is what makes the pattern mean what you intended.where year(dob) = 2008 or where monthname(dob) = "March", in the Date Functions lesson. They read better. like is the tool that works in both syllabuses, and it is worth knowing that a date can be pattern-matched at all.7Patterns at a glance
| Pattern | Means | Matches |
|---|---|---|
| 'A%' | Starts with A | Anjali, Asha |
| '%a' | Ends with a | Veena, Karma, Diana |
| '%a%' | Contains an a anywhere | Sohan, Farhan, Ravi |
| '____' | Exactly four characters | Ravi, Bhim, Pema |
| '_o%' | Second character is o | Sohan, Tom |
| 'S%n' | Starts with S, ends with n | Sohan |
| '%' | Anything at all | Every row with a value |
not like, in the same way as not in and not between. So where name not like 'A%' gives every student whose name does not begin with A.8Recap
Any number of characters, including none.
Exactly one character, which must be present.
Starts with. '%a' ends with. '%a%' contains.
It is a piece of text, like any other value.
Which pattern finds names ending in 'a'?
What does like '____' (four underscores) match?
Which finds every student whose name contains the letters 'ar' anywhere?