SQL and querying skills

SQL and Querying Skills-md for AI Agents

SQL and querying skills are SKILL.md files that help Claude, Codex and other AI agents turn a question into a query that returns the right rows: joining tables at the right grain, nesting subqueries and common table expressions, and computing across rows with window functions. They give the agent decision rules, thresholds and verification steps, not vague advice.

8 of 8 skills published, version 1.0.0.

Where to start: begin with sql-joins, then subqueries-and-ctes and window-functions. For correctness, add null-handling-in-sql and conditional-aggregation. For speed, use query-optimization to find why a query is slow, database-indexing to design the index, and efficient-pagination for fast paging.

01

SQL Joins

sql-joins · foundations · 2,959 words · start here

Writes, reviews and debugs SQL joins that return the right rows at the right grain, across PostgreSQL, MySQL, SQLite and SQL Server, and avoids silent inner joins, fan-out and the NOT IN NULL trap.

02

Subqueries and CTEs

subqueries-and-ctes · foundations · 2,964 words

Structures, rewrites and debugs SQL subqueries and common table expressions across PostgreSQL, MySQL, SQLite and SQL Server, from EXISTS and IN to recursive CTEs, and avoids the NOT IN NULL trap and runaway recursion.

03

Window Functions

window-functions · foundations · 2,645 words

Writes, reviews and debugs SQL window functions across PostgreSQL, MySQL, SQLite and SQL Server: running totals, moving averages, ranking and top-N-per-group, with explicit frames that avoid the default RANGE trap.

04

Database Indexing

database-indexing · performance · 3,030 words

Designs, reviews and debugs SQL indexes so queries run fast without slowing writes, across PostgreSQL, MySQL, SQLite and SQL Server: column order, covering, partial and expression indexes, with the selectivity and write-cost calls behind each one.

05

Query Optimisation

query-optimization · performance · 2,860 words

Diagnoses and fixes slow SQL by reading the execution plan and fixing the dominant bottleneck, across PostgreSQL, MySQL, SQLite and SQL Server: EXPLAIN reading, sargable rewrites, cardinality errors and N+1, before reaching for an index.

06

Efficient Pagination

efficient-pagination · performance · 2,963 words

Builds fast, correct SQL pagination that stays quick on deep pages, across PostgreSQL, MySQL, SQLite and SQL Server: keyset (seek) pagination with row-value cursors, the matching index, and when OFFSET is still fine.

07

NULL Handling in SQL

null-handling-in-sql · foundations · 3,477 words

Handles NULL correctly across PostgreSQL, MySQL, SQLite and SQL Server, from IS NULL versus = NULL and the NOT IN NULL trap to NULL-safe comparison, COALESCE and how aggregates, GROUP BY and ORDER BY treat missing values.

08

Conditional Aggregation

conditional-aggregation · foundations · 2,927 words

Counts and sums subsets in one grouped scan and pivots rows to columns across PostgreSQL, MySQL, SQLite and SQL Server, with the FILTER clause, the CASE fallback and the ELSE rule that keeps counts and averages correct.

What SQL and querying skills do for an AI agent

A general model can write SQL, but left to itself it will drop the unmatched rows a stray WHERE filter was never meant to remove, double-count a total across a one-to-many join, or read the default window frame as a plain running total when it is not. These skills give the agent the discipline a careful analyst uses: state the grain of the result before writing the query, put each filter in the clause that keeps the rows you want, and frame a window explicitly rather than trusting a default. Each one is a plain Markdown file the agent loads when the work calls for it.

Where to start

Begin with sql-joins, the base of almost every multi-table query. Add subqueries-and-ctes for breaking a long query into readable steps and testing existence safely, and window-functions for running totals, ranking and row-to-row change. Together they answer the questions you ask of relational data most often, and each one hands off to the next instead of repeating it.

What the agent needs from you

Point the agent at the tables and say what one row of the result should represent, which columns join them and how the tables relate on those columns, and which SQL engine you run. The skills ask for anything missing rather than guessing, because the grain of the data and the join cardinality decide whether a query is correct.

Frequently asked questions

They are SKILL.md files that turn a general model into a careful SQL writer for one task each. The agent loads the file, follows its workflow and decision rules, and runs the checks it names, such as verifying the row count against the grain, before trusting a query.

No. The skills carry the judgement, such as when a LEFT JOIN quietly becomes an inner join or when a running total needs an explicit frame. You supply the tables and the question; the agent writes the query and explains what it returns.

The core workflow is engine-agnostic, so it holds for PostgreSQL, MySQL, SQLite and SQL Server. Each skill ends with a dialect appendix for the syntax and limits that differ by engine, kept apart from the portable logic.

sql-joins, because joining tables is the base of most queries. Install subqueries-and-ctes and window-functions alongside it so the handoffs between them work.

Sources and more data skills

Each skill lists its own sources with the date it was last checked, drawn from the official documentation of PostgreSQL, MySQL, SQLite and SQL Server and from practitioner writing on query performance. Browse the other Data subcategories as they are published, or see the full Skills-md library.