M
PRO
last year
Matecountry asked

Is the range of the SQL "NOT BETWEEN" operator inclusive (includes the edge values of the given range) or exclusive (excludes the edges of the given range)?

Abhay Jajodia
Expert
last year
Abhay Jajodia answered

In SQL, when you use the NOT BETWEEN operator, it will filter out all values that fall within the given range, including the start and end numbers.

So, in the following example:

SELECT first_name, country, age  
FROM Customers  
WHERE age NOT BETWEEN 22 AND 28;

This query is filtering out customers whose ages are 22 through 28, not 23 through 27.

By using NOT BETWEEN 22 AND 28, it means ages 22, 23, 24, 25, 26, 27, and 28 will be excluded.

If your intention is to exclude just from 22 through 27, you'd phrase it like:

SELECT first_name, country, age  
FROM Customers  
WHERE age NOT BETWEEN 22 AND 27;

If you meant to exclude up to but not including 28, then the range should definitely stop at 27 for your query.

Hope this helps clear things up! Let me know if you have more questions.

SQL
This question was asked as part of the Learn SQL Basics course.