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 patternMatches
%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
4 rows

Names with "Tea" anywhere in them (a % on each side):

SELECT name FROM product WHERE name LIKE '%Tea%';
name
Green Tea
1 row

Names that end in s:

SELECT name FROM product WHERE name LIKE '%s';
name
Potato Chips
Cashew Nuts
2 rows

_ counts characters. Three _ mean "exactly three characters", and only Pen fits:

SELECT name FROM product WHERE name LIKE '___';
name
Pen
1 row

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%';
namephone
Malee Srisuk089-111-2222
Tawan Rungruang085-123-4567
2 rows

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
4 rows

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'
101
1 row

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 '\'
10
1 row

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
4 rows
SELECT name FROM product WHERE name GLOB 'c*';
name
no rows

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
6 rows

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;
namephone
no rows

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;
namephone
Anan WongsaNULL
Siriporn KaewdeeNULL
2 rows
SELECT name, phone FROM customer WHERE phone IS NOT NULL;
namephone
Somchai Jaidee081-234-5678
Malee Srisuk089-111-2222
Pim Chaiya086-555-0101
Narin Thongdee082-777-8888
Kanya Boonmee084-246-1357
Wichai Saelim083-999-0000
Tawan Rungruang085-123-4567
Lalita Meesuk087-654-3210
8 rows

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 = NULLNULL <> 11 = 1NULL IS NULL'' IS NULL0 IS NULL
NULLNULL1100
1 row

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';
namephone
Malee Srisuk089-111-2222
Pim Chaiya086-555-0101
Narin Thongdee082-777-8888
Kanya Boonmee084-246-1357
Wichai Saelim083-999-0000
Tawan Rungruang085-123-4567
Lalita Meesuk087-654-3210
7 rows

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';
namephone
Malee Srisuk089-111-2222
Anan WongsaNULL
Pim Chaiya086-555-0101
Narin Thongdee082-777-8888
Kanya Boonmee084-246-1357
Wichai Saelim083-999-0000
Siriporn KaewdeeNULL
Tawan Rungruang085-123-4567
Lalita Meesuk087-654-3210
9 rows

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 0NULL AND 1NULL OR 1NULL OR 0NOT NULL
0NULL1NULLNULL
1 row

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 + 5NULL * 0'Tel: ' || NULL5 > NULL
NULLNULLNULLNULL
1 row

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)
1NULLNULL1
1 row

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;
namephone
Somchai Jaidee081-234-5678
Malee Srisuk089-111-2222
Anan Wongsano phone
Pim Chaiya086-555-0101
Narin Thongdee082-777-8888
Kanya Boonmee084-246-1357
Wichai Saelim083-999-0000
Siriporn Kaewdeeno phone
Tawan Rungruang085-123-4567
Lalita Meesuk087-654-3210
10 rows
SELECT coalesce(NULL, NULL, 'third', 'fourth');
coalesce(NULL, NULL, 'third', 'fourth')
third
1 row

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;
namephone
Somchai Jaidee081-234-5678
Malee Srisuk089-111-2222
Anan Wongsa-
Pim Chaiya086-555-0101
Narin Thongdee082-777-8888
Kanya Boonmee084-246-1357
Wichai Saelim083-999-0000
Siriporn Kaewdee-
Tawan Rungruang085-123-4567
Lalita Meesuk087-654-3210
10 rows
  1. Show the customers whose names contain dee.
  2. Show the products whose names are exactly 6 characters long.
  3. Show the customers who have no phone number, with their city.
  4. 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
3 rows
name
Coffee
1 row
namecity
Anan WongsaBangkok
Siriporn KaewdeePhuket
2 rows
namephone
Somchai Jaidee081-234-5678
Malee Srisuk089-111-2222
Anan Wongsa-
Pim Chaiya086-555-0101
Narin Thongdee082-777-8888
Kanya Boonmee084-246-1357
Wichai Saelim083-999-0000
Siriporn Kaewdee-
Tawan Rungruang085-123-4567
Lalita Meesuk087-654-3210
10 rows

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