w3resource

MySQL NOT BETWEEN AND operator

NOT BETWEEN AND operator

MySQL NOT BETWEEN AND operator checks whether a value is not present between a starting and a closing expression.

Syntax:

expr NOT BETWEEN min AND max

If expr is not greater than or equal to min and expr is not less than or equal to max, BETWEEN returns 1, otherwise, it returns 0.

MySQL Version: 8.0

Example: MySQL NOT BETWEEN AND operator

The following MySQL statement will fetch the rows from the table publisher which established before 1968 or after 1975. The NOT operator is used to exclude the publishers which ware established within the given period.

Code:

SELECT pub_name,country,pub_city,estd
FROM publisher
WHERE YEAR(estd)  NOT BETWEEN 1968 AND 1975;

Sample table: publisher


Output:

mysql> SELECT pub_name,country,pub_city,estd
    -> FROM publisher
    -> WHERE YEAR(estd)  NOT BETWEEN 1968 AND 1975;
+------------------------------+---------+-----------+------------+
| pub_name                     | country | pub_city  | estd       |
+------------------------------+---------+-----------+------------+
| BPP Publication              | India   | Mumbai    | 1985-10-01 | 
| Ultra Press Inc.             | UK      | London    | 1948-07-10 | 
| Summer Night Publication     | USA     | New York  | 1990-12-10 | 
| Pieterson Grp. of Publishers | UK      | Cambridge | 1950-07-15 | 
| Novel Publisher Ltd.         | India   | New Delhi | 2000-01-01 | 
+------------------------------+---------+-----------+------------+
5 rows in set (0.04 sec)

Example: MySQL NOT BETWEEN AND operator with MONTH()

The following MySQL statement will fetch the rows from the table publisher which was established before the month February or after the month August.

Code:

SELECT pub_name,country,pub_city,estd 
FROM publisher 
WHERE MONTH(estd)  NOT BETWEEN '02' and '08';

Sample table: publisher


Example : MySQL NOT BETWEEN AND operator with logical AND

The following MySQL statement will fetch the rows from the table publisher which was established before the month May or after September and year of establishment is not in the period 1950 to 1975.

Code:

SELECT pub_name,country,pub_city,estd
FROM publisher 
WHERE MONTH(estd) NOT BETWEEN '05' and '09'
AND YEAR(estd) NOT BETWEEN 1950 AND 1975; 

Sample table: publisher


Output:

mysql> SELECT pub_name,country,pub_city,estd 
    -> FROM publisher 
    -> WHERE MONTH(estd)  NOT BETWEEN '02' and '08';
+--------------------------+-----------+-----------+------------+
| pub_name                 | country   | pub_city  | estd       |
+--------------------------+-----------+-----------+------------+
| Jex Max Publication      | USA       | New York  | 1969-12-25 | 
| BPP Publication          | India     | Mumbai    | 1985-10-01 | 
| New Harrold Publication  | Australia | Adelaide  | 1975-09-05 | 
| Mountain Publication     | USA       | Houstan   | 1975-01-01 | 
| Summer Night Publication | USA       | New York  | 1990-12-10 | 
| Novel Publisher Ltd.     | India     | New Delhi | 2000-01-01 | 
+--------------------------+-----------+-----------+------------+
6 rows in set (0.03 sec)

Example : MySQL NOT BETWEEN AND operator on a date range

The following MySQL statement will fetch the rows from the table publisher which 'established date' before 1st January 1950 or after 31st December 1975.

Code:

SELECT pub_name,country,pub_city,estd 
FROM publisher             
WHERE estd NOT BETWEEN '1950-01-01' AND '1975-12-31';

Relational Algebra Expression:

Relational Algebra Expression: MySQL NOT BETWEEN AND operator on a date range.

Relational Algebra Tree:

Relational Algebra Tree: MySQL NOT BETWEEN AND operator on a date range.

Sample table: publisher


Output:

mysql> SELECT pub_name,country,pub_city,estd 
    -> FROM publisher             
    -> WHERE estd NOT BETWEEN '1950-01-01' AND '1975-12-31';
+--------------------------+---------+-----------+------------+
| pub_name                 | country | pub_city  | estd       |
+--------------------------+---------+-----------+------------+
| BPP Publication          | India   | Mumbai    | 1985-10-01 | 
| Ultra Press Inc.         | UK      | London    | 1948-07-10 | 
| Summer Night Publication | USA     | New York  | 1990-12-10 | 
| Novel Publisher Ltd.     | India   | New Delhi | 2000-01-01 | 
+--------------------------+---------+-----------+------------+
4 rows in set (0.03 sec)

Slideshow of MySQL Comparison Function and Operators

Previous: LIKE
Next: NOT EQUAL OPERATOR(<>,!=)



Follow us on Facebook and Twitter for latest update.