# SQL

> Use when: write or review correct, set-based SQL with safe parameters and sensible indexing.

- Skill: `kimtth/sql` (Agent Skill)
- Install (CLI): `npx skillmds@latest add kimtth/sql`
- Raw SKILL.md: https://api.skillmd.com/api/skills/kimtth/sql/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Data & Analytics
- Author: kimtth (https://skillmd.com/u/kimtth)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/kimtth/sql

---


Goal: correct, readable SQL that the planner can execute efficiently.

Use for:
- writing queries, joins, and aggregations
- reviewing query correctness and parameter safety
- replacing row-by-row logic with set-based queries

Workflow:
1. Express intent as a set operation, not a loop.
2. Parameterize all values; never concatenate input.
3. Select only the columns you need.
4. Join on indexed keys; understand the join type you want.
5. Filter early; push predicates into subqueries/CTEs sensibly.
6. Verify correctness on edge cases and read the query plan.

Patterns:
- explicit JOIN ... ON over comma joins
- CTEs for readability of multi-step queries
- window functions for ranking and running totals
- GROUP BY with the right aggregates and HAVING

Rules:
- always parameterize; SQL injection is unacceptable
- avoid SELECT *; name your columns
- know whether NULLs change your results
- check the plan before optimizing by guess

