Skip to content
Reference notes from the interactive SQL course, which covers the same ground with a live SQL console, graded challenges and a final exam.
Filtering Data

IN, BETWEEN, and LIKE

Three shorthands that make filters readable — and their sharp edges.

IN, BETWEEN, and LIKE

Three operators that replace long chains of OR with something a human can read.

IN — one of a list

SQL
SELECT first_name, dept_id
FROM employees
WHERE dept_id IN (1, 2)
ORDER BY dept_id, first_name;

Identical to dept_id = 1 OR dept_id = 2, and it stays readable at ten values where the OR chain does not. It works on text too:

SQL
SELECT order_id, status, amount
FROM orders
WHERE status IN ('pending', 'cancelled')
ORDER BY order_id;

The NOT IN trap

NOT IN with a NULL anywhere in the list returns nothing at all:

SQL
SELECT COUNT(*) AS surprising_zero
FROM employees
WHERE dept_id NOT IN (1, 2, NULL);

Zero rows. x NOT IN (1, 2, NULL) expands to x <> 1 AND x <> 2 AND x <> NULL, and that last comparison is never true, so the whole AND chain can never be true. This bites hardest when the list comes from a subquery that happens to contain a NULL — the query returns nothing, no error is raised, and the report simply shows a blank page. NOT EXISTS is the safe form, and gets its own lesson later.

BETWEEN — an inclusive range

SQL
SELECT first_name, hired_on
FROM employees
WHERE hired_on BETWEEN '2020-01-01' AND '2021-12-31'
ORDER BY hired_on;

BETWEEN a AND b means >= a AND <= b. Both ends are included — the most common misreading, and the reason off-by-one bugs cluster here.

Two rules worth internalising:

  • The low value goes first. BETWEEN 100 AND 1 matches nothing, silently.
  • Do not use BETWEEN on timestamps. BETWEEN '2024-03-01' AND '2024-03-31' misses everything that happened during the last day, because a timestamp on the 31st at 09:00 is greater than the 31st at midnight. Use a half-open range instead — >= '2024-03-01' AND < '2024-04-01' — which is correct regardless of month length, leap years, or how many decimal places the timestamps carry.

Half-open on our date-only column, for comparison:

SQL
SELECT order_id, placed_at, amount
FROM orders
WHERE placed_at >= '2024-03-01' AND placed_at < '2024-04-01'
ORDER BY placed_at;

LIKE — pattern matching on text

Two wildcards:

PatternMatches
%any run of characters, including none
_exactly one character
SQL
SELECT first_name, email
FROM employees
WHERE email LIKE 'a%'
ORDER BY email;

More patterns:

SQL
SELECT name, category
FROM products
WHERE name LIKE '%a%'
ORDER BY name;
  • 'a%' — starts with a
  • '%plan' — ends with plan
  • '%wid%' — contains wid
  • '_at' — three characters ending in "at"

Case sensitivity, again

LIKE follows the same rules as =: case-sensitive on PostgreSQL, Oracle and SQLite (though SQLite's LIKE is case-insensitive for ASCII by default — a genuine inconsistency with its own =), usually insensitive on MySQL and SQL Server. PostgreSQL offers ILIKE for an explicitly case-insensitive match; the portable version is LOWER(col) LIKE LOWER('pattern').

Matching a literal %

To search for a percent sign, escape it: LIKE '100!%' ESCAPE '!'. Any character can be the escape character; \ is conventional but must still be declared with ESCAPE.

The performance note

LIKE 'abc%' can use an index — the database knows where to start looking. LIKE '%abc' and LIKE '%abc%' cannot: with an unknown prefix, every row must be examined. On a large table that is the difference between a millisecond and a minute, and it is why search boxes that do leading-wildcard matching are the first thing to fall over under load. Full-text search exists for this.

Common mistakes

  • NOT IN over a list containing NULL — returns nothing, silently.
  • Assuming BETWEEN is exclusive — it includes both ends.
  • BETWEEN on timestamps — loses the final day.
  • LIKE '%x%' on a large table — no index can help.
  • LIKE with no wildcard — LIKE 'Ada' is just = 'Ada', written slower.

Interview question

Why can NOT IN (SELECT …) return zero rows when you expect many?

If the subquery yields even one NULL, every comparison in the expanded AND chain becomes unknown and no row qualifies. Use NOT EXISTS, or filter the NULLs out of the subquery.

Check yourself

  1. Rewrite dept_id = 1 OR dept_id = 3 OR dept_id = 5 with IN.
  2. Write a March 2024 filter that is correct even if placed_at gains a time.
  3. Which of LIKE 'ab%', LIKE '%ab', LIKE '%ab%' can use an index?
All 43 lessons in SQL — The Interactive Visual Notebook
  1. Introduction to SQL
  2. Why SQL Matters
  3. What is an RDBMS?
  4. Tables, Rows, and Columns
  5. SQL Command Categories (DDL/DML/DCL/DQL)
  6. SELECT and FROM
  7. Aliases and Expressions
  8. DISTINCT and Sorting
  9. Limiting Results
  10. Query Execution Order
  11. WHERE and Comparison Operators
  12. Combining Conditions — AND, OR, NOT
  13. IN, BETWEEN, and LIKE
  14. Working with NULL
  15. Aggregate Functions
  16. GROUP BY
  17. HAVING — Filtering Groups
  18. Grouping in Practice
  19. INNER JOIN
  20. LEFT JOIN and Outer Joins
  21. Self Joins
  22. Cross Joins and Join Pitfalls
  23. UNION, INTERSECT, and EXCEPT
  24. Subqueries
  25. EXISTS and Correlated Subqueries
  26. Common Table Expressions (WITH)
  27. Recursive CTEs
  28. Window Functions — The OVER Clause
  29. Ranking Functions
  30. Frames and Running Totals
  31. LAG and LEAD
  32. Window Function Patterns
  33. What an Index Is
  34. Reading a Query Plan
  35. Designing Indexes
  36. Writing Queries an Index Can Use
  37. Transactions and ACID
  38. Isolation Levels
  39. Locks and Deadlocks
  40. Choosing Data Types
  41. Constraints and Keys
  42. Normalisation
  43. Designing a Schema