5. LIKE and NULL
Searching text by a pattern, and the value that isn't there: NULL, and why it behaves so strangely.
LIKE: text that matches a pattern
= wants the whole text to match exactly. Often you know only part of it: names that start with C, names with "Tea" in them. LIKE compares text with a pattern (a template) in which two characters are special:
| In the pattern | Matches |
|---|---|
% | any run of characters, of any length, even none |
_ | exactly one character, whatever it is |
Products whose names start with C:
SELECT name FROM product WHERE name LIKE 'C%';| name |
|---|
| Coffee |
| Croissant |
| Cheesecake |
| Cashew Nuts |
Names with "Tea" anywhere in them (a % on each side):
SELECT name FROM product WHERE name LIKE '%Tea%';| name |
|---|
| Green Tea |
Names that end in s:
SELECT name FROM product WHERE name LIKE '%s';| name |
|---|
| Potato Chips |
| Cashew Nuts |
_ counts characters. Three _ mean "exactly three characters", and only Pen fits:
SELECT name FROM product WHERE name LIKE '___';| name |
|---|
| Pen |
The two can be mixed. Phone numbers that start with 08, then any one digit, then -1, then anything:
SELECT name, phone FROM customer WHERE phone LIKE '08_-1%';| name | phone |
|---|---|
| Malee Srisuk | 089-111-2222 |
| Tawan Rungruang | 085-123-4567 |
LIKE and capital letters
Unlike =, LIKE doesn't care about capitals, so 'c%' finds the same 4 products as 'C%':
SELECT name FROM product WHERE name LIKE 'c%';| name |
|---|
| Coffee |
| Croissant |
| Cheesecake |
| Cashew Nuts |
But only for the 26 letters of English (A to Z). Letters such as Ä and ä count as different. Thai has no capitals, so a Thai pattern simply matches the same characters:
SELECT 'กาแฟเย็น' LIKE '%กาแฟ%', 'Ä' LIKE 'ä', 'Coffee' LIKE 'coffee';| 'กาแฟเย็น' LIKE '%กาแฟ%' | 'Ä' LIKE 'ä' | 'Coffee' LIKE 'coffee' |
|---|---|---|
| 1 | 0 | 1 |
To search for a real % or _, choose an escape character with ESCAPE: the character after it is taken as it is. '%\%' ESCAPE '\' means "anything, ending in a real %":
SELECT '50%' LIKE '%\%' ESCAPE '\', '500' LIKE '%\%' ESCAPE '\';| '50%' LIKE '%\%' ESCAPE '\' | '500' LIKE '%\%' ESCAPE '\' |
|---|---|
| 1 | 0 |
A pattern with no % or _ in it is like = that ignores capitals: city LIKE 'bangkok' finds the 4 customers in Bangkok, which city = 'bangkok' in chapter 3 couldn't.
GLOB: patterns, capitals counted
GLOB is SQLite's other way of matching a pattern. It uses the signs of file names on a computer: * for any run of characters, ? for one character. And it does care about capitals:
SELECT name FROM product WHERE name GLOB 'C*';| name |
|---|
| Coffee |
| Croissant |
| Cheesecake |
| Cashew Nuts |
SELECT name FROM product WHERE name GLOB 'c*';| name |
|---|
The first finds 4 products, the second none. GLOB also takes a set of characters in square brackets. [A-D]* is "starts with A, B, C or D":
SELECT name FROM product WHERE name GLOB '[A-D]*';| name |
|---|
| Coffee |
| Croissant |
| Banana Bread |
| Cheesecake |
| Dried Mango |
| Cashew Nuts |
GLOB is SQLite's own; most other databases haven't got it. LIKE is everywhere.
NULL: no value
Two customers have no phone number in the shop's data. Their phone is NULL: not zero, not empty text, but "there is no value here", "we don't know". Let's look for them with =:
SELECT name, phone FROM customer WHERE phone = NULL;| name | phone |
|---|
Nothing. = NULL never matches anything. Think of NULL as "unknown": is an unknown phone number equal to an unknown? We can't say. The answer to that question isn't "yes" but "unknown" too, and WHERE keeps only the rows whose answer is yes.
To ask about NULL, use IS NULL and IS NOT NULL:
SELECT name, phone FROM customer WHERE phone IS NULL;| name | phone |
|---|---|
| Anan Wongsa | NULL |
| Siriporn Kaewdee | NULL |
SELECT name, phone FROM customer WHERE phone IS NOT NULL;| name | phone |
|---|---|
| Somchai Jaidee | 081-234-5678 |
| Malee Srisuk | 089-111-2222 |
| Pim Chaiya | 086-555-0101 |
| Narin Thongdee | 082-777-8888 |
| Kanya Boonmee | 084-246-1357 |
| Wichai Saelim | 083-999-0000 |
| Tawan Rungruang | 085-123-4567 |
| Lalita Meesuk | 087-654-3210 |
2 customers without a phone, 8 with. Comparing anything with NULL by = or <> gives NULL; only IS gives a real yes or no. And empty text '' and zero are values, not NULL:
SELECT NULL = NULL, NULL <> 1, 1 = 1, NULL IS NULL, '' IS NULL, 0 IS NULL;| NULL = NULL | NULL <> 1 | 1 = 1 | NULL IS NULL | '' IS NULL | 0 IS NULL |
|---|---|---|---|---|---|
| NULL | NULL | 1 | 1 | 0 | 0 |
NULL slips through "not equal" too. Customers whose phone is not 081-234-5678: you might expect 9, but the 2 without a phone aren't there, because NULL <> '081-234-5678' is unknown, not yes:
SELECT name, phone FROM customer WHERE phone <> '081-234-5678';| name | phone |
|---|---|
| Malee Srisuk | 089-111-2222 |
| Pim Chaiya | 086-555-0101 |
| Narin Thongdee | 082-777-8888 |
| Kanya Boonmee | 084-246-1357 |
| Wichai Saelim | 083-999-0000 |
| Tawan Rungruang | 085-123-4567 |
| Lalita Meesuk | 087-654-3210 |
7 rows. To include them, say so: phone <> '081-234-5678' OR phone IS NULL, or use IS NOT, which treats NULL as a value like any other:
SELECT name, phone FROM customer WHERE phone IS NOT '081-234-5678';| name | phone |
|---|---|
| Malee Srisuk | 089-111-2222 |
| Anan Wongsa | NULL |
| Pim Chaiya | 086-555-0101 |
| Narin Thongdee | 082-777-8888 |
| Kanya Boonmee | 084-246-1357 |
| Wichai Saelim | 083-999-0000 |
| Siriporn Kaewdee | NULL |
| Tawan Rungruang | 085-123-4567 |
| Lalita Meesuk | 087-654-3210 |
9 rows.
Never write = NULL or <> NULL: they match nothing. Use IS NULL and IS NOT NULL. And when a column may hold NULL, ask yourself what a condition like <> does with those rows.
Three answers: yes, no, unknown
With NULL around, a condition has three possible answers: true (1), false (0) and unknown (NULL). This is called three-valued logic. AND, OR and NOT follow common sense, if you read NULL as "might be either":
SELECT NULL AND 0, NULL AND 1, NULL OR 1, NULL OR 0, NOT NULL;| NULL AND 0 | NULL AND 1 | NULL OR 1 | NULL OR 0 | NOT NULL |
|---|---|---|---|---|
| 0 | NULL | 1 | NULL | NULL |
NULL AND 0is0: one side is already false, soANDis false whatever the unknown is.NULL AND 1isNULL: it depends on the unknown.NULL OR 1is1: one side is already true.NULL OR 0isNULL, andNOT NULL(the opposite of unknown) is still unknown.
Arithmetic and joining text with NULL give NULL too: an unknown plus 5 is still unknown, even an unknown times zero:
SELECT NULL + 5, NULL * 0, 'Tel: ' || NULL, 5 > NULL;| NULL + 5 | NULL * 0 | 'Tel: ' || NULL | 5 > NULL |
|---|---|---|---|
| NULL | NULL | NULL | NULL |
This is the trap of NOT IN promised in chapter 4. 2 NOT IN (1, NULL) asks "is 2 neither 1 nor the unknown?" and the database can't say, so the answer is NULL, and a WHERE with it keeps no row at all:
SELECT 1 IN (1, NULL), 2 IN (1, NULL), 2 NOT IN (1, NULL), 2 NOT IN (1, 3);| 1 IN (1, NULL) | 2 IN (1, NULL) | 2 NOT IN (1, NULL) | 2 NOT IN (1, 3) |
|---|---|---|---|
| 1 | NULL | NULL | 1 |
coalesce() and ifnull(): a value instead of NULL
When showing data, it's often nicer to show something in place of NULL. coalesce() takes several values and gives back the first one that isn't NULL:
SELECT name, coalesce(phone, 'no phone') AS phone FROM customer;| name | phone |
|---|---|
| Somchai Jaidee | 081-234-5678 |
| Malee Srisuk | 089-111-2222 |
| Anan Wongsa | no phone |
| Pim Chaiya | 086-555-0101 |
| Narin Thongdee | 082-777-8888 |
| Kanya Boonmee | 084-246-1357 |
| Wichai Saelim | 083-999-0000 |
| Siriporn Kaewdee | no phone |
| Tawan Rungruang | 085-123-4567 |
| Lalita Meesuk | 087-654-3210 |
SELECT coalesce(NULL, NULL, 'third', 'fourth');| coalesce(NULL, NULL, 'third', 'fourth') |
|---|
| third |
ifnull(a, b) is the same thing for exactly two values. (AS phone names the result's column; chapter 7 tells more.)
SELECT name, ifnull(phone, '-') AS phone FROM customer;| name | phone |
|---|---|
| Somchai Jaidee | 081-234-5678 |
| Malee Srisuk | 089-111-2222 |
| Anan Wongsa | - |
| Pim Chaiya | 086-555-0101 |
| Narin Thongdee | 082-777-8888 |
| Kanya Boonmee | 084-246-1357 |
| Wichai Saelim | 083-999-0000 |
| Siriporn Kaewdee | - |
| Tawan Rungruang | 085-123-4567 |
| Lalita Meesuk | 087-654-3210 |
- Show the customers whose names contain
dee. - Show the products whose names are exactly 6 characters long.
- Show the customers who have no phone number, with their city.
- Show every customer's name and phone, with
-where there is no phone.
Show the answer
SELECT name FROM customer WHERE name LIKE '%dee%';
SELECT name FROM product WHERE name LIKE '______';
SELECT name, city FROM customer WHERE phone IS NULL;
SELECT name, ifnull(phone, '-') AS phone FROM customer;| name |
|---|
| Somchai Jaidee |
| Narin Thongdee |
| Siriporn Kaewdee |
| name |
|---|
| Coffee |
| name | city |
|---|---|
| Anan Wongsa | Bangkok |
| Siriporn Kaewdee | Phuket |
| name | phone |
|---|---|
| Somchai Jaidee | 081-234-5678 |
| Malee Srisuk | 089-111-2222 |
| Anan Wongsa | - |
| Pim Chaiya | 086-555-0101 |
| Narin Thongdee | 082-777-8888 |
| Kanya Boonmee | 084-246-1357 |
| Wichai Saelim | 083-999-0000 |
| Siriporn Kaewdee | - |
| Tawan Rungruang | 085-123-4567 |
| Lalita Meesuk | 087-654-3210 |
Phai (bamboo), given as a gift of knowledge: free to use, learn from and pass on · every example on this page was run on Phai SQL and checked against SQLite when the page was made