Special Operators and Clauses

The operators and clauses that make filtering and presenting results practical. IN and the NOT IN trap, in which one NULL in a list returns no rows and a natural query about non-smokers loses 3,497 never-smokers; BETWEEN and the closed intervals that count 33 people in two BMI classes at once; LIKE, GLOB and ESCAPE on drug names, where a search for statins also finds an antifungal; CASE, COALESCE and NULLIF for recoding survey answers; DISTINCT and how it treats NULL; ORDER BY with NULLs and ties; and LIMIT, OFFSET and the dialects' versions of top-N, where 427 people tie for oldest. On the NHANES 2017-2018 database in SQLite.

x NOT IN (...) drops every row where x is NULL and keeps no row at all when the list holds a NULL; on the NHANES 2017-2018 smoking questions in SQLite 3.49.1 it loses all 3,497 never-smokers. BETWEEN includes both ends, so adjacent BMI classes written with it count the 33 people at exactly 30.0 twice; half-open intervals do not. LIKE '%STATIN' also finds the antifungal nystatin, and its escape and case rules differ by dialect. CASE without ELSE leaves missing answers NULL, NULLIF turns missing-value codes into NULL, and a COALESCE stand-in is safe only if its value cannot occur. SQLite sorts NULLs first and PostgreSQL last, and ties at the top-coded age of 80 make "the five oldest" 427 people.

NHANES asks adults two questions about smoking: whether they have smoked at least 100 cigarettes in their life (smq020: 1 yes, 2 no) and, if so, whether they smoke now (smq040: 1 every day, 2 some days, 3 not at all). Someone who has never smoked 100 cigarettes is not asked the second question, so their smq040 is NULL. A query for the people who do not currently smoke is easy to write:

SELECT COUNT(*) FROM smoking WHERE smq040 NOT IN (1, 2);   -- 1,338

It returns the 1,338 former smokers and none of the 3,497 people who never smoked, because for them smq040 NOT IN (1, 2) is unknown, not true. The query is syntactically fine and the answer is wrong by a factor of more than three.

This article covers the operators that go beyond the comparisons of Simple SQL Queries, IN, BETWEEN, LIKE and the conditional expressions CASE, COALESCE and NULLIF, and the clauses that shape a result: DISTINCT, ORDER BY and LIMIT. Several of them interact with NULL in ways that change answers, and several differ between SQL dialects; both are pointed out as they arise. The examples run on the NHANES database described in Relational Databases.

IN and NOT IN

x IN (a, b, c) is true when x equals any value in the list; it is shorthand for x = a OR x = b OR x = c. The NHANES race and Hispanic origin codes 1 and 2 are Mexican American and other Hispanic, so

SELECT COUNT(*) FROM person WHERE race IN (1, 2);   -- 2,187

counts the 2,187 Hispanic participants, the same as the longer race = 1 OR race = 2. The list can also be a subquery that produces the values, WHERE seqn IN (SELECT seqn FROM ...), covered in Subqueries.

NOT IN negates the test, and inherits the three-valued logic of the comparisons behind it. When x is NULL, every comparison x = a is unknown, so x IN (...) and x NOT IN (...) are both unknown and the row is dropped either way. That is what happened to the never-smokers above. pandas' nullable missing value pd.NA follows the same three-valued logic, while a comparison with NaN gives False (and != gives True), as The pandas Package shows. The repair is to say what should happen to them:

SELECT COUNT(*) FROM smoking
WHERE smq040 NOT IN (1, 2) OR smq040 IS NULL;   -- 5,703

This returns 5,703 people: 1,338 former smokers, 3,497 never-smokers and 868 adolescents aged 12 to 17, who were not asked the adult questions at all (their smq020 is also NULL). Whether those 868 belong in the answer is again a decision; a query that classifies people from both questions, with CASE below, makes the decision visible.

A NULL inside the list is worse. For a value not in the list, x NOT IN (1, 2, NULL) means x <> 1 AND x <> 2 AND x <> NULL, and the last term is unknown, so the whole condition can never be true:

SELECT COUNT(*) FROM smoking WHERE smq040 NOT IN (1, 2, NULL);   -- 0

IN with a NULL in the list still finds the matches (1,021 current smokers for IN (1, 2, NULL)), because one true term makes an OR true. The SQLite documentation gives the full table of results for IN and NOT IN, which agrees with this reasoning (SQLite, n.d.-a). Nobody writes NULL into a literal list on purpose, but a subquery that returns a NULL among its values has the same effect, and Subqueries shows a real case in which NOT IN returns nothing while NOT EXISTS gives the right answer.

BETWEEN

x BETWEEN a AND b is equivalent to x >= a AND x <= b: both ends are included (SQLite, n.d.-a). age BETWEEN 18 AND 39 includes 18-year-olds and 39-year-olds, 1,974 people in all.

Closed intervals are natural for integers and a trap for measurements cut into classes. The usual BMI classes are overweight from 25 to under 30 and obesity class 1 from 30 to under 35. Written with BETWEEN,

SELECT COUNT(*) FROM exam WHERE bmi BETWEEN 25 AND 30;   -- 1,990
SELECT COUNT(*) FROM exam WHERE bmi BETWEEN 30 AND 35;   -- 1,289

the two classes share their boundary. BMI is recorded to one decimal, and 33 people have a BMI of exactly 30.0; they are counted in both classes, so the classes overlap and their counts cannot be added. Half-open intervals avoid this: bmi >= 25 AND bmi < 30 gives 1,957 and bmi >= 30 AND bmi < 35 gives 1,269, and every person falls in exactly one class. The same choice of closed side arises with pd.cut in pandas, discussed in Enriching Data.

Pattern matching: LIKE and GLOB

LIKE compares text with a pattern in which % matches any sequence of characters, including none, and _ matches exactly one character. Every other character matches itself. Drug names in NHANES are stored in capitals, so the statins, the cholesterol-lowering drugs whose generic names share an ending, can be found with:

SELECT drug_id, name FROM drug WHERE name LIKE '%STATIN' ORDER BY name;
drug_id name
d04105 ATORVASTATIN
d05348 EZETIMIBE; SIMVASTATIN
d03183 FLUVASTATIN
d00280 LOVASTATIN
d01233 NYSTATIN
d07637 PITAVASTATIN
d00348 PRAVASTATIN
d04851 ROSUVASTATIN
d00746 SIMVASTATIN

The pattern does what it says and not what was meant. Nystatin is an antifungal, not a statin; it matches because its name happens to end in the same letters. The combination product ezetimibe with simvastatin is a statin and is found only because simvastatin comes last in its name. Changing the pattern to '%STATIN%' adds two more products containing nystatin, NYSTATIN TOPICAL and NYSTATIN; TRIAMCINOLONE TOPICAL. Excluding the name NYSTATIN explicitly, '%STATIN' counts 1,217 prescriptions to 1,210 people. With '%STATIN%' the same exclusion gives 1,225 prescriptions to 1,216 people, and every one of the extra eight is one of the two topical nystatin products, which the exclusion of the exact name does not catch. Pattern matching on names is a quick first pass, and its results need reading; a reliable classification of drugs needs a table of drug classes keyed by drug identifier, which is a join, not a pattern.

The single-character wildcard is useful when the length of a code matters. ICD-10-CM codes have a three-character category, optionally followed by a dot and more characters. icd10 LIKE '___' finds the 92 reason codes that are bare categories, and icd10 LIKE 'E11._' finds the type 2 diabetes codes with exactly one character after the dot (E11.2, E11.4, E11.8 and the survey's own E11.P, "Prevent diabetes"), while 'E11%' also matches E11 itself and E11.2P.

To match a literal % or _, the pattern must say which character escapes it, with an ESCAPE clause: '100%' LIKE '100\%' ESCAPE '\' is true, and '1000' LIKE '100\%' ESCAPE '\' is false. SQLite, like standard SQL, has no default escape character: in SQLite 3.49.1, 'a\b' LIKE 'a\b' is true, the backslash matching itself. PostgreSQL's documentation states the standard's rule, that omitting ESCAPE means there is no escape character, and then departs from it: PostgreSQL, like MySQL, treats backslash as the escape character when no ESCAPE clause is given, MySQL unless its NO_BACKSLASH_ESCAPES mode is set (MySQL, n.d.-a; PostgreSQL Global Development Group, n.d.-a). A pattern containing a backslash can therefore mean one thing there and another in SQLite.

Case sensitivity, and the dialects

Whether LIKE ignores case depends on the dialect. In SQLite, LIKE is case-insensitive for the letters of the ASCII alphabet only: 'atorvastatin' LIKE 'ATORVASTATIN' is true, but the documentation notes that 'æ' LIKE 'Æ' is false (SQLite, n.d.-a). In PostgreSQL, LIKE is case-sensitive and ILIKE is the case-insensitive version (PostgreSQL Global Development Group, n.d.-a). In MySQL the answer depends on the collation: comparisons ignore case unless an operand uses a case-sensitive collation or is a binary string, so 'abc' LIKE 'ABC' is true under the default collation (MySQL, n.d.-a). SQLite's GLOB operator matches case-sensitively with the Unix file wildcards * and ?: 'atorvastatin' GLOB 'ATORVASTATIN' is false and 'ATORVASTATIN' GLOB '*STATIN' true.

For patterns beyond these wildcards, the dialects provide regular expressions in different ways. The SQL standard defines SIMILAR TO, which PostgreSQL implements alongside its POSIX operator ~ (PostgreSQL Global Development Group, n.d.-a); MySQL has REGEXP. SQLite parses REGEXP but defines no function to carry it out, so it raises an error unless the application supplies one (SQLite, n.d.-a); from Python, sqlite3 can register a function built on the re module.

Conditional expressions

CASE

CASE chooses a value by conditions, like if ... elif ... else. It is the standard way to recode survey answers into categories. The two smoking questions combine into a three-category status:

SELECT CASE
         WHEN smq020 = 2 THEN 'never'
         WHEN smq020 = 1 AND smq040 = 3 THEN 'former'
         WHEN smq020 = 1 AND smq040 IN (1, 2) THEN 'current'
       END AS smoking_status,
       COUNT(*) AS people
FROM smoking
GROUP BY smoking_status
ORDER BY people DESC;
smoking_status people
never 3,497
former 1,338
current 1,021
NULL 868

The WHEN conditions are tried in order and the first true one wins. When none is true and there is no ELSE, the result is NULL, which is how the 868 adolescents who were not asked end up in a group of their own. Leaving out the ELSE here is deliberate: a missing answer stays visibly missing, instead of being filed under a category it does not belong to. (GROUP BY, which forms one row per status, is the subject of Aggregate Functions.)

The second form, CASE x WHEN v1 THEN r1 WHEN v2 THEN r2 ... END, compares one expression with a list of values and suits code tables:

SELECT CASE health
         WHEN 1 THEN 'excellent' WHEN 2 THEN 'very good' WHEN 3 THEN 'good'
         WHEN 4 THEN 'fair' WHEN 5 THEN 'poor'
         ELSE 'refused or unknown'
       END AS self_rated_health,
       COUNT(*) AS people
FROM questionnaire
GROUP BY self_rated_health
ORDER BY MIN(health);

This gives 2,303, 2,501, 2,847, 1,335 and 261 people from excellent to poor and 7 who refused or did not know. Two details of NULL handling apply to CASE. A WHEN condition that is NULL counts as not true, so CASE WHEN NULL THEN 'yes' ELSE 'no' END is 'no'. And because the second form compares with =, it cannot match NULL: CASE NULL WHEN NULL THEN 'equal' ELSE 'not equal' END is 'not equal'; testing for NULL needs the first form with IS NULL (SQLite, n.d.-a). In a real code table, a separate lookup table joined to the data is often better than a long CASE, since it keeps the labels in one place; the race table of this database is one.

COALESCE and NULLIF

COALESCE(a, b, ...) returns its first argument that is not NULL, and NULLIF(a, b) returns NULL when a = b and a otherwise. They convert between NULL and ordinary values in the two directions.

NULLIF is the tool for codes that stand for missing values. The examination file records a diastolic blood pressure of 0 for 394 of the 19,959 readings. The survey's documentation allows such values ("Diastolic BP can be zero") without explaining them in the data file's notes, and the earlier articles on these data treat them as missing when averaging (Cleaning Data). NULLIF(dbp, 0) turns those zeros into NULL, so that averages and other aggregates skip them; Aggregate Functions shows by how much the mean changes. The same idea, with CASE because there are two codes, cleans the number of days a medicine has been taken: the column holds 282 entries coded 77777 (refused) or 99999 (don't know), and its largest raw value is 99999 days, about 274 years. Mapping the codes to NULL leaves a largest value of 21,900 days, 60 years.

COALESCE goes the other way, replacing NULL with a value, and it is easy to misuse. COALESCE(income_ratio, -1) turns the missing income ratio of person 93706 into -1, a number that any average or comparison will then treat as real. It is safe for display ("not recorded") and for genuine defaults, and unsafe as a way of making NULLs disappear before analysis, which is imputation by accident.

DISTINCT

SELECT DISTINCT removes duplicate rows from a result, turning SQL's multiset back into a set (Relational Databases). It applies to whole rows, so SELECT DISTINCT race, sex FROM person returns the 12 combinations that occur, where SELECT DISTINCT race returns 6.

For DISTINCT, NULLs count as equal to each other, unlike everywhere else: SELECT DISTINCT smq040 FROM smoking returns four rows, 1, 2, 3 and one NULL. The SQLite developers found this to be the behavior of every engine they tested and adopted it for compatibility, while noting that it sits oddly with NULLs being distinct in a UNIQUE column (SQLite, n.d.-b). Counting is different again: COUNT(DISTINCT smq040) is 3, because COUNT of a column ignores NULLs.

COUNT(DISTINCT ...) is often the right count when a table has more rows than the entity being counted: the prescription table has 14,434 rows but COUNT(DISTINCT seqn) is 4,045 people. A DISTINCT added to make duplicates disappear from a join is sometimes a sign that the join was wrong, a point taken up in Join Queries.

ORDER BY

Rows come back in no particular order unless ORDER BY asks for one. It takes a list of expressions, each sorted ascending (ASC, the default) or descending (DESC); the second expression breaks ties in the first, and so on. The expressions need not appear in the SELECT list, and they may refer to its aliases or to its columns by position (ORDER BY 2), though the position form breaks silently when the list is edited.

Where NULLs go

SQLite treats NULL as smaller than every other value, so NULLs come first in ascending order and last in descending order (SQLite, n.d.-c). Sorting people by income ratio, ascending, starts with the 1,231 people whose ratio is NULL. PostgreSQL and Oracle do the opposite: by default they sort NULLs as if larger than any other value, last in ascending order and first in descending order (PostgreSQL Global Development Group, n.d.-b; Oracle, n.d.), so the same query returns different first rows on different systems. The standard clauses NULLS FIRST and NULLS LAST settle it explicitly; SQLite supports them (SQLite, n.d.-c), from version 3.30.0 of 2019 (SQLite, n.d.-d), and ORDER BY income_ratio NULLS LAST starts with the 91 people whose recorded ratio is 0.

Ties

When rows tie on every ORDER BY expression, their order is undefined (SQLite, n.d.-c). NHANES top-codes age at 80, and 427 people are recorded as 80. A query for "the five oldest people" is therefore not a well-defined question:

SELECT seqn, age FROM person ORDER BY age DESC LIMIT 5;

On the table as built, SQLite reads the rows in order of their key and returns persons 93768, 93785, 93800, 93816 and 93869. After an index on age is created, the same query is answered by reading the index backwards and returns persons 102890, 102883, 102871, 102856 and 102828. Both answers are correct; any five of the 427 are. A query whose result must be reproducible needs a tie-breaker that makes the order total, usually the key: ORDER BY age DESC, seqn.

LIMIT and OFFSET

LIMIT n keeps the first n rows of the sorted result, and OFFSET m skips m rows first, which is how results are shown a page at a time: LIMIT 5 OFFSET 5 gives rows 6 to 10. Without ORDER BY, "first" has no meaning, and a limited query returns whichever rows the plan meets first. With ties, even an ORDER BY may not be enough, as the age example shows.

The syntax is one of the least standardized parts of SQL:

Dialect First five rows
SQLite, PostgreSQL, MySQL ... ORDER BY x LIMIT 5
SQL Server SELECT TOP (5) ... ORDER BY x
SQL standard, PostgreSQL, Oracle, Db2 ... ORDER BY x FETCH FIRST 5 ROWS ONLY

The standard form has a variant that handles ties honestly, FETCH FIRST 5 ROWS WITH TIES, which returns the first five rows and every row tied with the fifth; PostgreSQL and Oracle support it (PostgreSQL Global Development Group, n.d.-c; Oracle, n.d.), and SQL Server has TOP (5) WITH TIES (Microsoft, n.d.). SQLite has no such clause, but a subquery gives the same result: find the value in fifth place, then keep every row at least that large.

SELECT COUNT(*) FROM person
WHERE age >= (SELECT age FROM person ORDER BY age DESC LIMIT 1 OFFSET 4);   -- 427

The answer to "the five oldest people, with ties" is 427 people, which is the honest answer to a question the top-coding makes unanswerable. Window functions, in Aggregate Functions, give a more general way to number and rank rows with ties.

OFFSET paging has a cost worth knowing: to return rows 10,001 to 10,010 the database must still produce and discard the first 10,000. For large results, paging by key (WHERE seqn > last_seen ORDER BY seqn LIMIT 10) avoids that.

The written order of clauses

With these clauses the full single-table query reads:

SELECT DISTINCT columns
FROM table
WHERE conditions
ORDER BY expressions
LIMIT n OFFSET m;

The clauses must be written in this order. They take effect in a different one: FROM, WHERE, then SELECT and DISTINCT, then ORDER BY, then LIMIT and OFFSET (SQLite, n.d.-c). That is why ORDER BY can sort by a column alias defined in SELECT, and why LIMIT applies after sorting, not before. Aggregate Functions adds GROUP BY and HAVING to the sequence.

Worked problems

The three problems turn three warnings of the article into exact statements: what closed intervals overcount, why the subquery for ties is correct, and when replacing NULL by a value can be undone. The review-pass block of sql-operators-and-clauses.py checks each on SQLite 3.49.1.

Problem 1 (what closed classes overcount). The BMI classes written with BETWEEN held 1,990 and 1,289 people. Find exactly how much their sum overstates the number of people in the two classes together, and show that half-open classes overstate nothing.

Approach. For two sets of rows $A$ and $B$, inclusion and exclusion gives $|A \cup B| = |A| + |B| - |A \cap B|$: the sum counts the rows in both sets twice. The question is what $A \cap B$ is for each way of writing the classes.

Solution. With BETWEEN, $A$ is $25 \le \text{bmi} \le 30$ and $B$ is $30 \le \text{bmi} \le 35$, so $A \cap B$ is $\text{bmi} = 30$ exactly: 33 people. The union is $25 \le \text{bmi} \le 35$, which holds 3,246 people, and indeed $1{,}990 + 1{,}289 - 33 = 3{,}246$. With half-open classes, $25 \le \text{bmi} < 30$ and $30 \le \text{bmi} < 35$, no value satisfies both, so $A \cap B$ is empty and the counts add: $1{,}957 + 1{,}269 = 3{,}226$, the number with $25 \le \text{bmi} < 35$. The two unions differ by the 20 people at exactly 35.0, who belong to the next class up. The argument needs no assumption about the data: adjacent closed classes always share their boundary, half-open ones never do, and the size of the error is the number of values that fall exactly on a boundary. Values recorded to one decimal make that number large; a continuous measurement recorded to full precision would make it small but not zero.

Problem 2 (the subquery for "first k with ties"). The article found the five oldest people, with ties, by keeping every row whose age is at least the fifth largest age. Prove that this returns exactly the first $k$ rows in descending order together with every row tied with the $k$th, which is what FETCH FIRST k ROWS WITH TIES returns, and find where it differs.

Proof. Sort the non-NULL values in descending order, $v_1 \ge v_2 \ge \cdots \ge v_n$, and suppose $n \ge k$. The subquery ORDER BY x DESC LIMIT 1 OFFSET k - 1 returns $v_k$. A row is kept when its value is at least $v_k$. Every one of the first $k$ rows has a value at least $v_k$, so all are kept. A later row is kept exactly when its value equals $v_k$, since no later value exceeds it, and those are the rows tied with the $k$th. Which $k$ rows count as "first" when there are ties does not matter, because the kept set depends only on $v_k$. The same set is the rows with RANK() OVER (ORDER BY x DESC) <= k: a row's rank is one more than the number of rows with a strictly larger value, and that number is below $k$ exactly when the value is at least $v_k$.

Checks. For age the three forms give 427 rows, the whole tied group at 80. For height, the fifth and fourth tallest people are both 195.5 cm, so the five tallest with ties are just five people; the seventh and eighth are both 194.6 cm, so the seven tallest with ties are eight. The subquery and RANK() agree in all three cases.

Where it differs. When the table has fewer than $k$ rows, the subquery returns no row, its value is NULL, and the comparison with NULL keeps nothing, while FETCH FIRST k ROWS WITH TIES would return every row. Asking for the ten largest drug names ending in VASTATIN, of which there are eight, returns 0 rows by the subquery. NULLs in the column are a second edge: the subquery for age needs no filter, since no age is NULL, but for a nullable column such as height it should exclude them (WHERE height IS NOT NULL), as the script's check does. Without that filter SQLite's descending sort still puts NULLs last, so the result is the same there, but PostgreSQL sorts them first and the subquery would return NULL and keep nothing.

Problem 3 (when COALESCE can be undone). COALESCE(x, c) replaces NULL by a value $c$ and NULLIF(y, c) replaces $c$ by NULL. When does NULLIF(COALESCE(x, c), c) give back $x$, and what goes wrong when it does not?

Solution. Consider a row. If $x$ is NULL, COALESCE gives $c$ and NULLIF turns it back into NULL, correctly. If $x$ is not NULL and $x \ne c$, both functions leave it alone. If $x = c$, COALESCE leaves it and NULLIF turns it into NULL, wrongly. So the round trip returns $x$ on every row exactly when the value $c$ never occurs in the column. On the income ratio, which is never negative, $c = -1$ is safe: the round trip gives back exactly the 1,231 NULLs. With $c = 0$ it is not: 91 people have a recorded ratio of 0, and the round trip makes 1,322 NULLs, losing 91 real values and raising the mean from 2.38 to 2.40. The value used as a stand-in damages the analysis in the other direction too. Averaged with the NULLs as $-1$, the mean income ratio is 1.93 instead of 2.38, the effect warned of in the section on COALESCE. A stand-in value is safe only when it cannot occur and is never averaged; a NULL meets both conditions without effort.

Open questions

  • How accurate is the name pattern for statins? NHANES publishes a drug information file (RXQ_DRUG) that assigns each drug identifier to therapeutic categories. Joining it to drug would give the precision and recall of LIKE '%STATIN' and its variants against a class table, the comparison this article could only describe.
  • Does keyset paging stay correct while the table changes? Paging through person by OFFSET and by key while rows are inserted and deleted between page requests, with seeded random changes over 30 runs, would count the rows each method skips or repeats.
  • When does SQLite use an index for LIKE? SQLite can use an index for a pattern with a fixed prefix only under conditions on case sensitivity and collation. Running EXPLAIN QUERY PLAN for prefix patterns such as 'ATOR%' on drug names with and without PRAGMA case_sensitive_like, and for suffix patterns such as '%STATIN', would show which queries in this article an index could speed up.

Where it leads

IN, BETWEEN, LIKE and CASE extend the conditions of a query, COALESCE and NULLIF move values to and from NULL, and DISTINCT, ORDER BY and LIMIT shape the result; at each step NULLs and ties decide whether the answer is the one meant. Aggregate Functions goes from rows to summaries: counts, sums and means, what each does with NULL, and GROUP BY, which the CASE example above used ahead of its explanation.

In Python

import re
import sqlite3
import pandas as pd

con = sqlite3.connect("nhanes.db")

# a REGEXP operator for SQLite, implemented in Python
con.create_function("regexp", 2, lambda pattern, s: s is not None and re.search(pattern, s) is not None)
con.execute("SELECT name FROM drug WHERE name REGEXP '(^|; )[A-Z]+VASTATIN'").fetchall()

# the same filter in pandas, for comparison
drugs = pd.read_sql("SELECT * FROM drug", con)
drugs[drugs.name.str.endswith("STATIN") & (drugs.name != "NYSTATIN")]

SQLite turns X REGEXP Y into a call regexp(Y, X), so the function receives the pattern first (SQLite, n.d.-a). The numbers in this article are computed by tools/experiments/data-science/sql-operators-and-clauses.py in the site's repository.

References

Reviewed: verified, 23 Nov 2026 · citations checked, 23 Nov 2026 · editorial review, 23 Nov 2026 · 4 revisions