Skip to content
Educora
Beginner16 min4 / 22

LIKE, IN, BETWEEN and IS NULL

Learn to search by pattern (LIKE), match a list of values (IN), pick a range (BETWEEN) and handle missing data (NULL).

Check yourself
In this lesson you will learn
  • Search text with LIKE and the % and _ wildcards
  • Write shorter conditions with IN and BETWEEN
  • Understand what NULL means and use IS NULL and COALESCE

When you type only part of a surname into a search box, the website still finds the right person. And how do you find “students from Sumqayıt, Quba or Naxçıvan”, “products priced between 10 and 55” or “students without an email”? SQL has four special operators for this: LIKE, IN, BETWEEN and IS NULL.

LIKE: searching by pattern

LIKE compares text with a pattern. A pattern has two special symbols: % stands for any number of characters (even zero), and _ stands for exactly one character.

PatternWhat it matchesExample value
'A%'starts with AAysel
'%ova'ends with ovaAbbasova
'%set%'contains setChess set
'_u%'second letter is uMurad
'____'exactly four charactersQuba
SQL
SELECT first_name, last_name
FROM students
WHERE last_name LIKE '%ova'
ORDER BY id;
▸ Expected output
first_name | last_name
Aysel | Məmmədova
Leyla | Hüseynova
Günay | Abbasova
Səbinə | Nəsirova
Fidan | Cəfərova
Students whose surname ends in “ova”.
SQL
SELECT first_name
FROM students
WHERE first_name LIKE '_u%'
ORDER BY id;
▸ Expected output
first_name
Murad
Tural
_ is exactly one character, % is everything else.

IN: a list of values

Writing city = 'Sumqayıt' OR city = 'Quba' OR city = 'Naxçıvan' is long and tiring. The IN operator says the same thing briefly: the condition is true if the value is in the list in brackets. NOT IN does the opposite and picks values that are not in the list.

SQL
SELECT first_name, city
FROM students
WHERE city IN ('Sumqayıt', 'Quba', 'Naxçıvan')
ORDER BY id;
▸ Expected output
first_name | city
Elvin | Sumqayıt
Səbinə | Quba
Fidan | Naxçıvan
Orxan | Sumqayıt
SQL
SELECT name, category
FROM products
WHERE category NOT IN ('Electronics', 'Stationery')
ORDER BY id;
▸ Expected output
name | category
Backpack | Accessories
Desk lamp | Home
Water bottle | Accessories
Chess set | Games

BETWEEN: a range

The condition x BETWEEN a AND b is the same as x >= a AND x <= b, so both ends are included. Below, the Backpack, which costs exactly 55, is in the result. The smaller value always comes first: BETWEEN 55 AND 10 returns nothing.

SQL
SELECT name, price
FROM products
WHERE price BETWEEN 10 AND 55
ORDER BY price;
▸ Expected output
name | price
Water bottle | 12
Desk lamp | 34.99
Chess set | 42
Backpack | 55
SQL
SELECT id, customer_id, order_date
FROM orders
WHERE order_date BETWEEN '2025-02-01' AND '2025-03-31'
ORDER BY order_date;
▸ Expected output
id | customer_id | order_date
3 | 2 | 2025-02-02
4 | 3 | 2025-02-10
5 | 4 | 2025-03-05
6 | 5 | 2025-03-18
Orders from February and March. If the dates also contained a time (2025-03-31 14:00), the afternoon of 31 March would fall outside the range.

NULL: no data

Definition
NULL

A special marker meaning that a value is missing or unknown. NULL is not zero and not an empty string ('') — it simply means “no data”.

Any comparison or calculation with an unknown value gives an unknown result — NULL. Even NULL = NULL is not true but NULL: we can't know whether two unknown values are equal. And WHERE keeps only the rows where the condition is true.

SQL
SELECT NULL = NULL AS a,
       NULL + 5    AS b,
       5 > NULL    AS c;
▸ Expected output
a | b | c
NULL | NULL | NULL
SQL
-- wrong: the condition is never true
SELECT COUNT(*) AS found
FROM students
WHERE email = NULL;
▸ Expected output
found
0
COUNT(*) counts the rows found. With = NULL nothing is found, although two students have no email.
SQL
-- right: IS NULL
SELECT first_name, last_name, email
FROM students
WHERE email IS NULL
ORDER BY id;
▸ Expected output
first_name | last_name | email
Elvin | Quliyev | NULL
Tural | İsmayılov | NULL

IS NOT NULL picks the rows that do have a value. To show something else instead of NULL in the result, use COALESCE(a, b, ...): it returns the first value in the list that is not NULL. SQLite and MySQL also have the short form IFNULL(a, b), and SQL Server has ISNULL(a, b); COALESCE works in all of them.

SQL
SELECT first_name,
       COALESCE(email, 'no email') AS contact
FROM students
WHERE grade = 11
ORDER BY id;
▸ Expected output
first_name | contact
Elvin | no email
Tural | no email
Fidan | fidan@example.com
Worked example

Find the students who live in Baku or Sumqayıt, are 14–15 years old and have an email.

Show solution
We join three conditions with AND:
the city is in the list — city IN ('Bakı', 'Sumqayıt');
the age is in the range — age BETWEEN 14 AND 15;
there is an email — email IS NOT NULL.
Result: Aysel, Leyla, Kamran and Orxan. Elvin is also from Sumqayıt, but he is 17 and has no email.
SQL
SELECT first_name, city, age
FROM students
WHERE city IN ('Bakı', 'Sumqayıt')
  AND age BETWEEN 14 AND 15
  AND email IS NOT NULL
ORDER BY id;
▸ Expected output
first_name | city | age
Aysel | Bakı | 15
Leyla | Bakı | 14
Kamran | Bakı | 15
Orxan | Sumqayıt | 14
Exercise

Show the name and price of products that cost between 30 and 150 (inclusive) and belong to the Electronics, Home or Games category. Sort by price from low to high.

Exercise · SQL
SELECT name, price
FROM products
-- use BETWEEN and IN
ORDER BY price;
▸ Expected output
name | price
Desk lamp | 34.99
Chess set | 42
Headphones | 120.5
Exercise

Show the first name, last name and email of students whose last name ends in ov and who have an email. Sort by first_name.

Exercise · SQL
SELECT first_name, last_name, email
FROM students
-- add the two conditions
ORDER BY first_name;
▸ Expected output
first_name | last_name | email
Kamran | Həsənov | kamran@example.com
Rəşad | Kərimov | rashad@example.com

Key points

  • LIKE searches by pattern: % is any number of characters, _ is exactly one.
  • IN (...) replaces a long chain of ORs; NOT IN picks values that are not in the list.
  • BETWEEN a AND b includes both ends; the smaller value comes first.
  • NULL means “no data”; comparing with it gives NULL.
  • Test for missing values with IS NULL / IS NOT NULL and replace them with COALESCE.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
Which pattern finds names that start with A?