sql-agent

A guide for writing, improving, and explaining SQL queries, which are instructions for reading or changing data in a database. It covers PostgreSQL, MySQL, SQLite, and BigQuery.

In plain words
What is it for?
It helps write queries, debug performance, interpret query plans, choose indexes, join tables, and design database schemas.
Why use it?
It helps avoid database-specific syntax mistakes and identifies common causes of slow queries or unclear data models.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/chandrudp29/skillhub/sql-agent
Any agent
npx skills add chandrudp29/skillhub --skill sql-agent
Clone the repo
git clone --depth 1 https://github.com/chandrudp29/skillhub

Made for: Claude Code, Codex.

Per session 35 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,055 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00035 $0.01055
Opus 5 $0.00017 $0.00528
Sonnet 5 $0.00007 $0.00211
Haiku 4.5 $0.00003 $0.00105

Measured 2d ago against content hash 95876066caf7, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

sql-agent scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/sql-agent/SKILL.md · 132 lines

How it starts

The opening of the file, as written. The whole thing — 132 lines — stays where its author put it; the contents beside it link to each section on GitHub.

SQL Agent

Writes correct, performant SQL and explains what it does. Works with PostgreSQL, MySQL, SQLite, and BigQuery.

When to Use

  • "Write a query to get X"
  • "This query is slow — help me optimize it"
  • "What does this query plan mean?"
  • "Design a schema for X"
  • "How do I join these tables?"

Writing Queries

Step 1 — Clarify before writing

Ask if not obvious:

  • What database? (PostgreSQL, MySQL, SQLite, BigQuery — syntax differs)
  • Approximate row counts? (matters for optimization)
  • What's the expected output format?
  • Are there existing indexes?

Step 2 — Write the query with explanation

Always provide:

  1. The query itself
  2. A plain-English explanation of what it does
  3. What indexes it will use (or should have)

Format:

-- Gets the top 10 customers by revenue in the last 30 days
-- Uses: idx_orders_customer_id, idx_orders_created_at
SELECT 
    c.id,
    c.name,
    SUM(o.total_amount) AS revenue_30d
FROM customers c
INNER JOIN orders o ON c.id = o.customer_id
WHERE o.created_at >= NOW() - INTERVAL '30 days'
  AND o.status = 'completed'
GROUP BY c.id, c.name
ORDER BY revenue_30d DESC
LIMIT 10;

Step 3 — Check for common problems

Before finalizing any query:

N+1 patterns: Is this query inside a loop? Rewrite to fetch in batch.

SELECT *: Specify columns. SELECT * on wide tables fetches data never used.

Missing WHERE clause on updates/deletes: Catastrophic. Always confirm the WHERE clause is intentional.

Implicit type coercion: WHERE id = '123' vs WHERE id = 123 — coercion kills index usage.

NULL behavior: NULL != NULL in SQL. WHERE col != 'value' excludes NULLs. Use IS NULL / IS NOT NULL explicitly.

Optimizing Slow Queries

Step 1 — Get the query plan

EXPLAIN ANALYZE [your query here];

Key things to look for:

  • Seq Scan on a large table = missing index
  • Hash Join on large tables = may need index on join key
  • Nested Loop with high estimated rows = investigate
  • actual rows >> estimated rows = stale statistics, run ANALYZE table_name

Read the full file on GitHub · 132 lines

Files

What ships with it

1 file beside SKILL.md in the same directory: the scripts, references and assets a skill reads on demand. Not counted in the per-session cost; read them before you install if any of them is executable.

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 132 lines · 35 tokens per session scan A 95876066caf7

Subscribe to this mod's changes

sql-agent is a skill published in the GitHub repository chandrudp29/skillhub (13 stars, last pushed 2mo ago), licensed MIT. It adds 35 tokens to every session and 1,055 once invoked, about $0.0002 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.