RienSQL
LESSON 16 / 62 · Operators IS NULL

Operators IS NULL, BETWEEN, IN

Syntax and usage examples of SQL IS NULL, BETWEEN, IN operators

IS NULL, BETWEEN, IN Operators

We have already familiarized ourselves with the syntax of the WHERE operator and comparison operators, but in addition to them, we can use the following useful operators in conditional queries:

  • IS NULL
  • BETWEEN
  • IN

Let's look at their application.

IS NULL

The IS NULL operator allows you to check whether a field has no value.

For example, let's select all teachers who do not have a middle name:

SELECT * FROM Teacher
WHERE middle_name IS NULL;
idfirst_namemiddle_namelast_name
10YUrijKrylov
11AndrejEvseev

To use negation, i.e. if we want to find all records where the field is not equal to NULL, we must use the following syntax:

SELECT * FROM Teacher
WHERE middle_name IS NOT NULL;

BETWEEN

The BETWEEN min AND max operator allows you to find out if the checked value of the column is located in the interval between min and max, including the values min and max themselves.

It is identical to the condition:

... WHERE field >= min AND field <= max

This operator is used as follows:

SELECT * FROM Payments
WHERE unit_price BETWEEN 100 AND 500;

All records from the Payments table where the value of the unit_price field is from 100 to 500 will be returned as a result.

IN

The IN operator allows you to find out if the checked value of the column is included in a list of certain values.

SELECT * FROM FamilyMembers
WHERE status IN ('father', 'mother');
member_idstatusmember_namebirthday
1fatherHeadley Quincey1960-05-13T00:00:00.000Z
2motherFlavia Quincey1963-02-16T00:00:00.000Z
6fatherErnest Forrest1961-09-11T00:00:00.000Z
7motherConstance Forrest1968-09-06T00:00:00.000Z

Practice

Solve it in the editor. The query runs against a sample database in your browser.

Task 1Print records containing NULL
Print the first_name and last_name of the students from the table Student missing middle_name.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 2Looking for values in a specified range
Select room reservations (columns room_id, start_date, end_date) from the Reservations table whose total price (column total) is between 240 and 560 inclusive.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 3Looking for values in a specific list
Output information about students from the table Student, whose year of birth corresponds to one of the following: 2000, 2002 and 2004.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.