Sub topic 11
Negating with NOT LIKE, NOT IN, and NOT BETWEEN
Assume you know basic SELECT statements, tables, columns, comparison operators, and AND and OR. This chapter focuses on excluding rows with NOT LIKE, NOT IN, and NOT BETWEEN, including the effects of NULL.
Excluding text patterns with NOT LIKE
NOT LIKE keeps rows whose text does not match a pattern. The same wildcards apply as with LIKE:
%matches any sequence of characters, including an empty sequence._matches exactly one character.
SELECT id, name
FROM employees
WHERE name NOT LIKE 'A%';This excludes names that start with A. A name such as Ben matches the condition, while Alice does not.
You can also negate a more specific pattern:
SELECT id, product_code
FROM products
WHERE product_code NOT LIKE 'AB_-%';This excludes codes beginning with AB, followed by one character, a hyphen, and any remaining text.
NOT LIKE does not match a NULL value. A NULL means that the value is unknown or missing. Comparing an unknown value produces UNKNOWN, not TRUE, so the row fails the WHERE filter.
SELECT id, name
FROM employees
WHERE name IS NULL
OR name NOT LIKE 'A%';Use the explicit IS NULL test when missing names should also be included.
Excluding values with NOT IN
NOT IN excludes rows whose value appears in a list.
SELECT id, name, department
FROM employees
WHERE department NOT IN ('Sales', 'Marketing');This is logically similar to writing:
WHERE department <> 'Sales'
AND department <> 'Marketing'NOT IN is useful when the exclusion list is known and short. It can also use a subquery:
SELECT id, name
FROM customers c
WHERE c.id NOT IN (
SELECT customer_id
FROM orders
WHERE status = 'Cancelled'
);This example intends to find customers who have not cancelled an order. However, it can fail if the subquery returns even one NULL customer ID.
The NOT IN and NULL trap
SQL uses three-valued logic. A predicate can be TRUE, FALSE, or UNKNOWN. The WHERE clause keeps only rows for which the condition is TRUE.
If the list contains NULL, SQL cannot establish that a value is different from every item:
WHERE department NOT IN ('Sales', NULL)For a department such as Support, SQL evaluates part of the comparison as unknown. The final result becomes UNKNOWN, so the row is removed.
Prefer NOT EXISTS for subqueries when the inner column might contain NULL:
SELECT c.id, c.name
FROM customers c
WHERE NOT EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = c.id
AND o.status = 'Cancelled'
);NOT EXISTS checks whether a matching row exists and is not affected by unrelated NULL values in orders.customer_id.
| Predicate | Includes boundary values? | What happens for `NULL`? |
|---|---|---|
| `NOT LIKE pattern` | Not applicable | `UNKNOWN`, so excluded |
| `NOT IN (values)` | Not applicable | `UNKNOWN`, so excluded |
| `NOT BETWEEN low AND high` | Excludes both boundaries | `UNKNOWN`, so excluded |
Excluding ranges with NOT BETWEEN
NOT BETWEEN keeps values outside an inclusive range:
SELECT id, name, salary
FROM employees
WHERE salary NOT BETWEEN 50000 AND 70000;This is equivalent to:
WHERE salary < 50000
OR salary > 70000;The endpoints are excluded. A salary of exactly 50000 or 70000 does not satisfy NOT BETWEEN.
A NULL salary also does not satisfy this condition. Add an explicit test if missing salaries belong in the result:
WHERE salary IS NULL
OR salary NOT BETWEEN 50000 AND 70000;For timestamps, use a half-open range when excluding a time period. A half-open range includes its lower endpoint and excludes its upper endpoint:
SELECT id, created_at
FROM orders
WHERE created_at < '2026-03-01'
OR created_at >= '2026-04-01';This excludes every timestamp from March 1, 2026 through March 31, 2026, including times late on March 31.
Combining negated predicates
Parentheses make the intended logic clear. Without them, AND is evaluated before OR.
SELECT id, name, department, salary
FROM employees
WHERE department NOT IN ('Sales', 'Marketing')
AND (name NOT LIKE 'A%' OR salary NOT BETWEEN 40000 AND 60000);The query first excludes two departments. It then keeps employees whose name does not start with A or whose salary falls outside the stated range.
Logical model
Index considerations
A negated predicate often gives the database less opportunity to use an ordinary index efficiently than a positive, selective predicate. For example, name NOT LIKE 'A%' describes most names if only a few start with A. The optimizer may choose a table scan.
A pattern such as NOT LIKE '%abc%' is especially difficult to search with a normal B-tree index because the pattern starts with %. Check the execution plan for important queries. Use database-specific indexes or search features when substring searches are common.
Exercise
Why can this query return no rows when orders.customer_id contains NULL, and what is a safer replacement?
Show answer
NOT IN becomes UNKNOWN when its subquery produces NULL. Replace it with a correlated NOT EXISTS query that tests the matching customer ID.