agentleFS
khgouldykhgouldy/skills/skills/data/reviewing-sql/SKILL.md

reviewing-sql

Skill1 starsChanged 16 days agoMITView on GitHub

Use when writing or reviewing analytical SQL and correctness matters — catch join fan-out, NULL-semantics traps, GROUP BY grain mistakes, and window/dedup bugs before they ship wrong numbers. Triggers include "review this query", "the totals look too high/low", duplicated rows after a join, or any non-trivial aggregation or window function.

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Check row counts at each step: If a join changes your row count and you didn't expect it to, stop — that's fan-out.

    line 48

≈ “Common” is an estimate: at least 1 in 5 of the 364,980 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Reviewing SQL

Analytical SQL fails quietly: the query runs, returns a number, and the number is wrong. There's no stack trace — just a metric that's inflated by a fan-out join or deflated by a NULL no one accounted for. Review for the handful of traps that produce plausible wrong answers.

The traps, in order of how often they bite

  1. Join fan-out (the #1 cause of inflated metrics). Joining to a table that has more than one matching row per key multiplies your rows — every downstream SUM/COUNT is now too high. Before trusting an aggregate after a join, confirm the grain: is the join one-to-one, or did it fan out? Aggregate the many-side to one row per key before joining, or COUNT(DISTINCT ...).
  2. NULL semantics.
    • NULL = NULL is not true — it's unknown. Equality and IN silently drop NULLs; use IS [NOT] DISTINCT FROM or COALESCE.
    • NOT IN (subquery) returns zero rows if the subquery contains a single NULL. Prefer NOT EXISTS.
    • Aggregates skip NULLs: AVG ignores them, COUNT(col) ≠ COUNT(*).
  3. GROUP BY grain mistakes. The grain of the result = the GROUP BY columns. Selecting a column that isn't in the group (and isn't aggregated) either errors or, worse, picks an arbitrary value. Be explicit about one row per what.
  4. Window function pitfalls. ROW_NUMBER() for dedup needs a deterministic ORDER BY or you get nondeterministic results. Mind the frame: the default frame for SUM() OVER (ORDER BY ...) is a running total, not the group total — add ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING if you meant the whole partition.
  5. Dedup that drops real rows. DISTINCT and GROUP BY collapse rows you may have wanted. QUALIFY ROW_NUMBER() OVER (PARTITION BY key ORDER BY ts DESC) = 1 is the clean "latest row per key" — verify the partition and order are right.
  6. Filtering an outer join in the WHERE clause. A WHERE predicate on the right table of a LEFT JOIN silently turns it into an inner join. Put the predicate in the ON clause if you want to keep the unmatched left rows.

How to actually verify

  • Check row counts at each step. If a join changes your row count and you didn't expect it to, stop — that's fan-out.
  • Spot-check the grain with select key, count(*) ... group by key having count(*) > 1.
  • Reconcile against a known total. Tie the result back to a number you trust from the source; a 2× discrepancy is usually fan-out, a slight shortfall is usually NULLs.

Pairs with

  • diagnosing-bugs — when a number is wrong, reproduce and trace it rather than guessing.
  • data-quality-checks — codify these checks as standing tests, not one-off reviews.

Original skill — distilled from Kevin's analytics-engineering practice.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.