Sub topic 11
Negating with NOT LIKE, NOT IN, and NOT BETWEEN
This chapter assumes you can write LIKE, IN, and BETWEEN predicates, and that you know how NULL makes a comparison evaluate to unknown. Negating a predicate looks like a simple flip of true and false, but SQL has a third result, unknown, and negation does not flip it. Most surprises come from that.
What negation does
Each of the three predicates has a negated form:
| Predicate | Negated form | Equivalent expansion |
|---|---|---|
| `x LIKE p` | `x NOT LIKE p` | no direct expansion, it is the complement of the pattern match |
| `x IN (a, b)` | `x NOT IN (a, b)` | `x <> a AND x <> b` |
| `x BETWEEN a AND b` | `x NOT BETWEEN a AND b` | `x < a OR x > b` |
NOT can also go in front of the whole predicate, as in NOT (x IN (a, b)). Both spellings mean the same thing. The infix form (x NOT IN) reads more naturally and is the usual style.
Note that NOT IN becomes a chain joined by AND, while NOT BETWEEN becomes a pair joined by OR. This follows De Morgan's laws: negating an OR of conditions gives an AND of negated conditions, and the reverse.
Sample data
The examples below use this employees table. Two rows contain NULL on purpose.
| id | name | department | salary |
|---|---|---|---|
| 1 | Anna | Sales | 45000 |
| 2 | Bruno | Support | 72000 |
| 3 | Carla | NULL | 52000 |
| 4 | Dev | Marketing | NULL |
| 5 | Eli | Legal | 61000 |
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name VARCHAR(50) NOT NULL,
department VARCHAR(50),
salary INTEGER
);
INSERT INTO employees (id, name, department, salary) VALUES
(1, 'Anna', 'Sales', 45000),
(2, 'Bruno', 'Support', 72000),
(3, 'Carla', NULL, 52000),
(4, 'Dev', 'Marketing', NULL),
(5, 'Eli', 'Legal', 61000);Three-valued logic and NOT
A WHERE clause keeps a row only when the condition is true. A comparison with NULL yields unknown, and NOT of unknown is still unknown:
| Condition | NOT condition |
|---|---|
| true | false |
| false | true |
| unknown | unknown |
So a row whose value is NULL fails both the predicate and its negation. It disappears from x LIKE p and also from x NOT LIKE p. The two results do not add up to the whole table.
NOT LIKE
NOT LIKE returns rows whose text does not match the pattern.
SELECT id, name, department
FROM employees
WHERE department NOT LIKE 'S%';The result is Dev (Marketing) and Eli (Legal). Anna and Bruno match 'S%', so they are excluded. Carla has a NULL department. NULL LIKE 'S%' is unknown, so NOT LIKE is unknown, and Carla is silently dropped.
To keep rows with a missing value, test for it explicitly:
SELECT id, name, department
FROM employees
WHERE department NOT LIKE 'S%'
OR department IS NULL;This returns Carla, Dev, and Eli.
NOT LIKE accepts the same % and _ wildcards and the same ESCAPE clause as LIKE:
SELECT id, label
FROM promotions
WHERE label NOT LIKE '10!%%' ESCAPE '!';This excludes labels that start with the literal text 10%.
Case sensitivity carries over. On a database where LIKE is case-sensitive, 'anna' NOT LIKE 'A%' is true, because the lowercase a does not match. If you want to exclude names regardless of case, apply the same normalization you would for LIKE:
SELECT id, name
FROM employees
WHERE LOWER(name) NOT LIKE 'a%';NOT IN
NOT IN keeps rows whose value is not equal to any item in the list.
SELECT id, name, department
FROM employees
WHERE department NOT IN ('Sales', 'Support');The result is Dev and Eli. Carla's NULL department makes each comparison unknown, so she is dropped, just as with NOT LIKE.
The expansion shows why: department NOT IN ('Sales', 'Support') means department <> 'Sales' AND department <> 'Support'. With a NULL department, both parts are unknown, and unknown AND unknown is unknown.
The more serious problem arises when the list itself contains a NULL. Then x <> NULL is unknown for every row, and an AND chain containing an unknown can never be true. The query returns no rows at all. This applies to a subquery that returns a NULL as well:
-- Returns no rows if any orders.customer_id is NULL
SELECT id, name
FROM customers
WHERE id NOT IN (SELECT customer_id FROM orders);Two fixes are common. Filter the NULL values out of the subquery, or use NOT EXISTS, which does not have this problem:
SELECT c.id, c.name
FROM customers c
WHERE NOT EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = c.id
);Prefer NOT EXISTS for "rows with no match" queries. It stays correct if a nullable column later receives NULL values.
NOT BETWEEN
BETWEEN includes both endpoints, so NOT BETWEEN excludes both endpoints. The negation is the two open-ended outer ranges.
SELECT id, name, salary
FROM employees
WHERE salary NOT BETWEEN 50000 AND 70000;This is the same as salary < 50000 OR salary > 70000. It returns Anna (45000) and Bruno (72000). Someone earning exactly 50000 or 70000 is not returned. Carla (52000) and Eli (61000) fall inside the range. Dev has a NULL salary, so the comparison is unknown and Dev is dropped.
To include Dev as well, add the NULL test:
SELECT id, name, salary
FROM employees
WHERE salary NOT BETWEEN 50000 AND 70000
OR salary IS NULL;Date ranges
For dates and timestamps, the half-open style from the earlier chapter extends to the complement. To find orders outside March 2026, write the two outer ranges directly:
SELECT id, created_at
FROM orders
WHERE created_at < '2026-03-01'
OR created_at >= '2026-04-01';This avoids the end-of-day trap. created_at NOT BETWEEN '2026-03-01' AND '2026-03-31' would treat a timestamp such as 2026-03-31 15:00:00 as outside the range, because '2026-03-31' means midnight at the start of that day. Rows from the afternoon of March 31 would be wrongly returned as "not in March".
NOT, AND, and OR together
NOT has higher precedence than AND, and AND has higher precedence than OR. With the infix forms (NOT IN, NOT LIKE, NOT BETWEEN) this rarely matters, because the NOT belongs to the predicate. It matters when NOT is placed in front of a condition:
-- NOT applies only to the first comparison
SELECT id, name
FROM employees
WHERE NOT department = 'Sales' AND salary > 50000;This reads as (NOT department = 'Sales') AND salary > 50000. To negate a whole group, use parentheses:
SELECT id, name, department, salary
FROM employees
WHERE NOT (department IN ('Sales', 'Support')
AND salary BETWEEN 40000 AND 60000);By De Morgan's laws this equals department NOT IN ('Sales', 'Support') OR salary NOT BETWEEN 40000 AND 60000. Writing it out with NULL in mind helps: a NULL department or salary can make parts of the group unknown, and the overall result follows the three-valued rules. For example, unknown OR true is true, but unknown OR false is unknown.
Inside BETWEEN, the word AND belongs to the predicate. In salary NOT BETWEEN 50000 AND 70000 AND department = 'Sales', the first AND completes the range and the second joins a new condition. Parenthesize if the line is hard to read.
Negation and indexes
Negated predicates are usually poor candidates for an index lookup.
NOT INand<>match most of the table, so the optimizer typically scans everything.NOT LIKE 'A%'has no useful starting point, because the index is ordered by the value and the matching rows are scattered on both sides of theArange.NOT BETWEEN a AND bis better. It is two ranges,x < a OR x > b, and some optimizers can use an index for each range. Whether they do depends on the database and how selective the ranges are.
If a negated filter is slow, check the execution plan before rewriting. When the excluded set is small and the kept set is large, a scan is often the correct plan anyway.
Common mistakes
- Assuming
x LIKE pandx NOT LIKE ptogether cover every row. Rows withNULLare in neither. - Using
NOT IN (subquery)on a nullable column and getting an empty result. - Writing
NOT BETWEENon timestamps with a date as the upper bound. - Forgetting parentheses when negating a group of conditions.
- Expecting
NOT LIKEto ignore case on a database whereLIKEis case-sensitive.
Exercise
Using the sample employees table, what does SELECT name FROM employees WHERE salary NOT BETWEEN 50000 AND 70000; return, and why is Dev not included?
Show answer
It returns Anna (45000) and Bruno (72000). Carla (52000) and Eli (61000) are inside the range. Dev has a NULL salary, so both salary < 50000 and salary > 70000 are unknown, and the row is dropped. Adding OR salary IS NULL would include Dev.
Exercise
Rewrite WHERE id NOT IN (SELECT customer_id FROM orders) so that it still works when orders.customer_id contains NULL values.
Show answer
Use NOT EXISTS: SELECT c.id, c.name FROM customers c WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customerid = c.id); An alternative is to add WHERE customerid IS NOT NULL inside the subquery.
Exercise
Write a query that returns orders whose created_at is outside March 2026, without using NOT BETWEEN.
Show answer
SELECT id, createdat FROM orders WHERE createdat < '2026-03-01' OR created_at >= '2026-04-01'; This is the complement of the half-open range and handles late timestamps on March 31 correctly.