sql-query-reviewer

Reviews SQL queries for correctness, performance and safety - index use, N+1 patterns, injection risks and locking. Use when the user asks to review, optimise or explain a SQL query, a slow endpoint, or an EXPLAIN plan.

Install

mkdir -p ~/.claude/skills/sql-query-reviewer
# save the file below as ~/.claude/skills/sql-query-reviewer/SKILL.md

SKILL.md

sql-query-reviewer/SKILL.md
---
name: sql-query-reviewer
description: Reviews SQL queries for correctness, performance and safety - index use, N+1 patterns, injection risks and locking. Use when the user asks to review, optimise or explain a SQL query, a slow endpoint, or an EXPLAIN plan.
---

# SQL Query Reviewer

## Correctness

- `NULL` handling: `NOT IN` with a nullable subquery, `=` vs `IS NULL`, outer joins filtered in `WHERE` (turns them inner).
- Aggregates: every non-aggregated column is grouped; `COUNT(col)` vs `COUNT(*)`.
- Time zones and half-open ranges: `created_at >= :start AND created_at < :end`.

## Performance

1. Ask for `EXPLAIN (ANALYZE, BUFFERS)` (Postgres) or `EXPLAIN ANALYZE` (MySQL) when available.
2. Look for sequential scans on large tables, sorts spilling to disk, and nested loops with high row estimates.
3. Index advice: equality columns first, then range; no functions on indexed columns in `WHERE`; covering indexes for hot reads.
4. Pagination: keyset (`WHERE id > :last ORDER BY id LIMIT n`) instead of large `OFFSET`.
5. Spot N+1 queries in the calling code and suggest one query with `IN`/join.

## Safety

- Parameters, never string concatenation.
- `UPDATE`/`DELETE` without a selective `WHERE` → stop and warn.
- Long transactions holding locks; migrations that rewrite big tables.

## Output

Findings ordered by impact, each with the problem, the fix as SQL, and how to verify.

More Data skills