/sql:sql-subqueries-and-existsfrom sql
Guides correct subquery use and owns the deep dive the foundation defers — the `NOT IN` + NULL trap, where a single NULL anywhere in the list or subquery collapses `NOT IN` to zero rows because `NOT IN` expands to a chain of `not equal to` comparisons AND'd together and the NULL conjunct is forever UNKNOWN, so `NOT EXISTS` is the safe portable anti-join (EXISTS never returns UNKNOWN). Auto-invokes when writing or editing subqueries, `IN (SELECT ...)`, `NOT IN`, `EXISTS`/`NOT EXISTS`, correlated subqueries, `ANY`/`SOME`/`ALL`, scalar subqueries in SELECT/WHERE, or on "NOT IN returns nothing" / "my orphan/anti-join query is empty" / "subquery returned more than one row" symptoms.
Ships in sql. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
"Note that if the left-hand expression yields null, or if there are no equal right-hand values and at least one right-hand row yields null, the result of the
NOT INconstruct will be null, not true." — PostgreSQL — Subquery Expressions
"When using a subquery, consider using not exists instead of not in ... EXISTS never returns unknown." — modern-sql.com — Three-Valued Logic
A subquery is a SELECT nested inside another statement. Most of them are harmless; one shape — NOT IN over a column that can be NULL — is a silent, result-destroying footgun, and it is the reason this skill exists. The foundation skill sql-relational-and-null-discipline states the three-valued-logic rules and flags the NOT IN trap (its §5), then explicitly defers the mechanism and the rewrite to here. This skill owns that deep dive. Everything below assumes the set semantics and TRUE/FALSE/UNKNOWN logic established in the foundation.
A scalar subquery stands in for a single value (in SELECT, WHERE, or an expression). It has two failure modes, neither obvious at write time:
more than one row returned by a subquery used as an expression. The subquery "cannot return more than one row" (PostgreSQL — Subquery Expressions).-- RISKY — errors the day two rows match (or worse on SQLite, see §7)
SELECT name, (SELECT price FROM prices p WHERE p.sku = i.sku) AS price
FROM items i;
-- SAFER — guarantee one row with an aggregate, or correlate on a key that is unique
SELECT name, (SELECT MAX(price) FROM prices p WHERE p.sku = i.sku) AS price
FROM items i;
Portability wart: SQLite does not error on a multi-row scalar subquery — it silently returns "the first row of the result" (SQLite — Expressions), and without ORDER BY that row is undefined (foundation §1). The stricter engines fail loudly; SQLite hides the bug. Route dialect specifics to sql-standard-vs-dialect-map.
Row-constructor subqueries lift the same idea to multiple columns: (a, b) = (SELECT x, y FROM ...) and (a, b) IN (SELECT x, y FROM ...) compare a row of values at once. They obey the identical rules — the = form still requires the subquery to return at most one row, and the IN/NOT IN forms still carry the NULL behavior of §3, now triggered by a NULL in any compared column. They are standard SQL but unevenly supported (route spellings to sql-standard-vs-dialect-map); prefer an explicit EXISTS with an AND-ed join condition when portability matters.
IN and EXISTS — Two Spellings of a Semi-JoinA semi-join keeps a row from the outer table when at least one matching row exists in the subquery — the match multiplies nothing and adds no columns. Both IN (SELECT ...) and EXISTS (...) express it.
-- Customers who have placed at least one order — semi-join, two spellings:
SELECT * FROM customers c WHERE c.id IN (SELECT customer_id FROM orders);
SELECT * FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);
EXISTS tests only whether rows come back, never their contents: "If it returns at least one row, the result of EXISTS is 'true'; if the subquery returns no rows, the result of EXISTS is 'false'." Because "the result depends only on whether any rows are returned, and not on the contents of those rows, ... a common coding convention is to write all EXISTS tests in the form EXISTS(SELECT 1 WHERE ...)" (PostgreSQL — Subquery Expressions). The SELECT 1 is idiomatic — SELECT * is equivalent but noisier. For the positive membership test, IN and EXISTS are interchangeable in correctness. The divergence appears only under negation — §3.
NOT IN + NULL Trap (the centerpiece)This is the canonical SQL footgun, and the reason the foundation routes here. A single NULL anywhere in a NOT IN list or subquery collapses the entire result to zero rows — silently, with no error.
-- WRONG — if ANY customer_id in orders is NULL, this returns NOTHING.
-- "Find customers with no orders" silently becomes "find no customers."
SELECT * FROM customers c
WHERE c.id NOT IN (SELECT customer_id FROM orders);
-- RIGHT — NOT EXISTS is null-safe and the portable anti-join.
SELECT * FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);
NOT IN is the negation of IN, and IN is = ANY. So x NOT IN (a, b, NULL) is NOT (x = a OR x = b OR x = NULL), which by De Morgan is the conjunction:
x <> a AND x <> b AND x <> NULL
modern-sql.com states the mechanism exactly: "An = ANY predicate is only false (so that the negation becomes true) if all comparisons are false. This is however, not possible if there is a NULL comparison, which inevitably yields unknown" (modern-sql.com — 3VL). The conjunct x <> NULL is UNKNOWN for every x (foundation §3), and something AND UNKNOWN can never be TRUE — at best UNKNOWN. WHERE keeps only TRUE rows, so every row is dropped. The spec puts it flatly: "the result of the NOT IN construct will be null, not true" (PostgreSQL — Subquery Expressions), and "the result of not in predicates that contain a null value is never true" (modern-sql.com — 3VL).
NOT EXISTS is immuneEXISTS asks "did any row come back?", not "is this value equal to that one?" — there is no comparison to go UNKNOWN. "EXISTS never returns unknown" (modern-sql.com — 3VL); SQLite confirms cross-engine that "rows containing NULL values are not handled any differently from rows without NULL values" (SQLite — Expressions). NOT EXISTS is therefore the correct, portable anti-join (keep outer rows with no match) and the recommended fix: "consider using not exists instead of not in" (modern-sql.com — 3VL).
IN is forgiving: a NULL in the list only matters when there is no real match, and even then IN can still return TRUE on a genuine match, so positive membership "usually works." NOT IN is unforgiving: with any NULL present it can never be TRUE. (If you must keep NOT IN, add WHERE customer_id IS NOT NULL to the subquery — but NOT EXISTS is cleaner and needs no such guard.) One safe NOT IN case: over an empty set, NOT IN is TRUE for every row (SQLite — Expressions).
WHERE id IN (SELECT customer_id FROM orders) is uncorrelated.WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id) is correlated — c.id comes from the outer customers c.EXISTS/NOT EXISTS anti/semi-joins are correlated by nature (the join predicate lives inside the subquery). "Conceptually re-evaluated per row" is the semantics, not the execution plan — every serious optimizer rewrites a correlated EXISTS into a single semi-join/anti-join. Whether and how it does so is a plan question; route plan reasoning to sql-explain-and-set-based-thinking. Correlated subqueries in the FROM clause (LATERAL, correlated derived tables) are a distinct construct owned by sql-lateral-and-correlated-derived.
ANY / ALL — and Why <> ALL Inherits the Same TrapANY (synonym SOME) and ALL compare a value against every row of a subquery with a chosen operator. Two equivalences matter:
"
SOMEis a synonym forANY.INis equivalent to= ANY." — PostgreSQL "NOT INis equivalent to<> ALL." — PostgreSQL
= ANY is just IN; "The result of ANY is 'true' if any true result is obtained" (PostgreSQL). And because <> ALL is literally NOT IN, it carries the identical NULL trap. "The result of ALL is 'true' if all rows yield true" (PostgreSQL) — but a NULL right-hand row makes its <> comparison UNKNOWN, so ALL is never TRUE and the row is dropped.
-- WRONG — same footgun wearing a different hat: <> ALL ≡ NOT IN
SELECT * FROM customers c
WHERE c.id <> ALL (SELECT customer_id FROM orders);
-- RIGHT — the anti-join, NULL-safe
SELECT * FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);
Other operators (> ALL, < ANY, etc.) are legitimate and have no special NULL hazard beyond ordinary three-valued logic — but anyone reaching for <> ALL to "avoid NOT IN" has escaped nothing. Use NOT EXISTS.
When a subquery and a join express the same thing, choose by intent:
EXISTS/IN. An INNER JOIN used for membership can multiply outer rows when the subquery side has duplicates — a classic accidental fan-out (join cardinality is owned by sql-joins). EXISTS cannot duplicate: it is a pure existence test.NOT EXISTS (§3) over NOT IN (NULL-unsafe) and over LEFT JOIN ... WHERE right.key IS NULL (correct but easy to misplace the filter — see sql-joins).GROUP BY when you want exactly one value.On plans: modern optimizers usually produce equivalent plans for IN, EXISTS, and the matching join, but not always, and NOT IN over a nullable column can force a notably worse plan than NOT EXISTS. Plan inspection and set-based reasoning belong to sql-explain-and-set-based-thinking; window-function alternatives to top-N-per-group subqueries belong to sql-window-functions. Pick for correctness and readability first; profile before contorting for plans.
EXISTS, IN, NOT IN, ANY/SOME, and ALL are all standard SQL and present in PostgreSQL, MySQL, SQLite, Oracle, and SQL Server. The semantics in this skill — NOT IN collapsing on a NULL, NOT EXISTS being NULL-immune, scalar 0-rows→NULL — hold across all of them. The deviations to watch:
| Behavior | PostgreSQL | SQLite | MySQL | Oracle |
|---|---|---|---|---|
NOT IN (…NULL…) → empty result | yes | yes | yes | yes |
NOT EXISTS unaffected by NULL | yes | yes | yes | yes |
| Scalar subquery, >1 row | error | silent first row | error | error |
| Scalar subquery, 0 rows → NULL | yes | yes | yes | yes |
NOT IN over an empty set → TRUE | yes | yes | yes | yes |
The scalar >1 row row is the one real wart: PostgreSQL/MySQL/Oracle/SQL Server raise a runtime error, SQLite silently takes an undefined first row (SQLite — Expressions) — arguably worse, because it returns a plausible wrong answer instead of failing. Dialect spellings and edge cases route to sql-standard-vs-dialect-map.
NOT IN Meets a NULLNo error fires; someone downstream pays:
... WHERE id NOT IN (SELECT parent_id FROM child). It worked in dev and in every test. In production one parent_id is NULL, the query returns zero orphans, the cleanup does nothing, and the bug ships — discovered weeks later when the orphans pile up. The NOT EXISTS rewrite they didn't write is the bug.NOT IN flipped from "most rows" to "no rows." They stare at a valid query that returns nothing and no error to grep for.Disciplined subquery code is empathy: the NOT EXISTS you wrote instead of NOT IN, and the aggregate or unique-key guard on the scalar subquery, are gifts to whoever debugs this at 3 a.m.
sql-relational-and-null-discipline — the policy root: three-valued logic, = NULL/<> traps, why UNKNOWN is dropped by WHERE. It flags the NOT IN trap (§5) and defers the deep dive to here.sql-joins — semi/anti-join via join, accidental fan-out, LEFT JOIN ... IS NULL anti-join, ON vs WHERE placement.sql-lateral-and-correlated-derived — correlated subqueries and LATERAL in the FROM clause.sql-window-functions — window alternatives to top-N-per-group and "compare to the group" subqueries.sql-explain-and-set-based-thinking — plan implications of subquery vs join, correlated execution, and NOT IN vs NOT EXISTS plans.sql-standard-vs-dialect-map — dialect spellings and the scalar-subquery >1 row divergence.High-frequency subquery anti-patterns in LLM-generated SQL, each with wrong/right code and a primary-source citation:
Source provenance for every claim in this skill:
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub ctoth/sql-skills-plugin --plugin sql32 skills in this plugin
/sql-aggregation-and-groupingGuides aggregation correctness — every column in the SELECT list of a grouped query must be either named in GROUP BY or wrapped in an aggregate (the functional-dependency rule), because a non-grouped, non-aggregated column has no single value per group. Bans the "ambiguous groups" antipattern that errors on standard-conformant engines but silently returns an arbitrary row's value on MySQL with ONLY_FULL_GROUP_BY disabled. Auto-invokes when writing or editing GROUP BY/HAVING, aggregate functions (SUM/COUNT/AVG/MIN/MAX), FILTER, LISTAGG/STRING_AGG/GROUP_CONCAT/ARRAY_AGG, ROLLUP/CUBE/GROUPING SETS, percentile/WITHIN GROUP, or any multi-level subtotal report, and on "only_full_group_by" / "must appear in the GROUP BY clause" / "subtotal" / "rollup" requests./sql-constraints-and-integrityGuides database-enforced data integrity — push the rules into the schema as declarative constraints rather than re-checking them in every application that writes. Covers PRIMARY KEY (= UNIQUE + NOT NULL, one per table) vs UNIQUE (many per table, and by default permits multiple NULLs — the SQL:2023 NULLS NOT DISTINCT lever fixes the duplicate-"unique"-emails trap), CHECK constraints and the pitfall that a CHECK passes when it evaluates to UNKNOWN/NULL (so pair it with NOT NULL), NOT. Auto-invokes when writing or editing CREATE TABLE/ALTER TABLE constraints, FOREIGN KEY/REFERENCES, CHECK/UNIQUE/NOT NULL/DEFAULT, or on "enforce this in the DB or the app" / "why are there orphaned rows" / "why did duplicate emails get in" decisions.Other plugins' skills for the same job