writing-sql

writing-sql is a skill for Claude Code, Codex from rileyhilliard/claude-essentials. It costs 50 tokens per session (557 once invoked), scanned A, original, MIT.

A collection of advanced SQL patterns for writing, reviewing, and speeding up database queries, especially in PostgreSQL.

In plain words
What is it for?
It helps with multi-column statistics, indexes, text searches, aggregation, NULL handling, pagination, unexpected duplicate rows, and other query performance issues.
Why use it?
It helps catch query problems that can return wrong results or make searches and reports slower than expected.

Skill for Claude CodeCodex

Part of the ce plugin — 17 skills, 1 command, 4 agents shipped together

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/rileyhilliard/claude-essentials/writing-sql
Any agent
npx skills add rileyhilliard/claude-essentials --skill writing-sql
Clone the repo
git clone --depth 1 https://github.com/rileyhilliard/claude-essentials

Made for: Claude Code, Codex.

Or install ce, the plugin that ships this one along with the rest of its 17 skills, 1 command, 4 agents.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for writing-sql

README.md
[![agentmods](https://agentmods.dev/badge/skills/rileyhilliard/claude-essentials/writing-sql.svg)](https://agentmods.dev/skills/rileyhilliard/claude-essentials/writing-sql)
Your own site
<a href="https://agentmods.dev/skills/rileyhilliard/claude-essentials/writing-sql"><img src="https://agentmods.dev/badge/skills/rileyhilliard/claude-essentials/writing-sql.svg" alt="Measured on agentmods" height="20"></a>
Per session 50 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 557 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.00050 $0.00557
Opus 5 $0.00025 $0.00279
Sonnet 5 $0.00010 $0.00111
Haiku 4.5 $0.00005 $0.00056

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

Security

Grade A, and why

writing-sql 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 4d 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.

plugins/ce/skills/writing-sql/SKILL.md · 38 lines

How it starts

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

Writing SQL

Core principle: Every query you write has an execution plan. Write SQL with that plan in mind, not just the result.

This skill covers only what Claude's defaults miss. Topics already handled by managing-databases/postgres-querying.md (LATERAL, CTE materialization, DISTINCT ON, EXPLAIN red flags, ROWS vs RANGE) are not repeated here.

Topic Navigation

Task Load
Planner picking bad plans on multi-column WHERE extended-statistics.md
LIKE search not using index, text pattern matching index-internals.md
Composite index column ordering, INCLUDE clause index-internals.md
Type mismatch between param and column silently bypassing index index-internals.md
Conditional aggregation, pivot queries, subtotals aggregation-patterns.md
GROUPING SETS, ROLLUP, CUBE aggregation-patterns.md
NULL edge cases in aggregates and window functions aggregation-patterns.md
Paginating large result sets query-anti-patterns.md
NOT IN returning zero rows unexpectedly query-anti-patterns.md
DISTINCT added to fix duplicate rows query-anti-patterns.md
Correlated subqueries in SELECT list query-anti-patterns.md
Functions in WHERE bypassing indexes query-anti-patterns.md

Iron Rules

These apply to every query, no exceptions:

  • Never paginate with OFFSET on large tables. Use keyset pagination.
  • Never use NOT IN when the subquery column is nullable. Use NOT EXISTS.
  • Always match parameter types to column types exactly. Implicit casts bypass indexes.
  • Replace CASE WHEN inside aggregates with FILTER. It's cleaner and semantically correct.
  • When a multi-column WHERE produces bad estimates, add extended statistics before adding hints or rewrites.

Read the full file on GitHub · 38 lines

Files

What ships with it

4 files 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. 4d ago First seen · 38 lines · 50 tokens per session scan A 23cc891a10bf

Subscribe to this mod's changes

writing-sql is a skill published in the GitHub repository rileyhilliard/claude-essentials (128 stars, last pushed 17d ago), licensed MIT. It adds 50 tokens to every session and 557 once invoked, about $0.0003 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.

Related

Other skills, from other repositories

safe-sql-execution

Use whenever code will build, return, fetch, or execute SQL that runs against a user's real Postgres database — even when the request reads like an ordinary feature or bug fix and never says "security," "injection," or "SafeSqlFragment." This covers: writing or editing any pg-meta function, query builder, or endpoint…

supabase/supabase · 221 tokens

ecto-patterns

Ecto patterns — schemas, changesets, queries, migrations, Multi, associations, preloads, upserts. Use when editing Repo calls, Ecto.Query, or schema fields. Skip for Ash.

oliver-kriska/claude-elixir-phoenix · 45 tokens

pg-pool-cloudsql-reconnect

Fix Node.js pg-pool not reconnecting after CloudSQL connection reset (ECONNRESET). Use when: (1) Retries fail with same ECONNRESET error despite retry logic, (2) "Connection terminated" or "connection already closed" errors persist across retries, (3) "remaining connection slots are reserved" errors in CloudSQL, (4)…

divinevideo/divine-mobile · 117 tokens

nw-database-technology-selection

Database comparison catalogs, RDBMS vs NoSQL selection criteria, CAP/ACID/BASE theory, OLTP vs OLAP, and technology-specific characteristics.

nWave-ai/nWave · 38 tokens

database-patterns

DB schema design and query tuning: normalization, indexing, N+1, transactions, EXPLAIN. Triggers: schema, index, slow query, N+1, PostgreSQL, MySQL, EXPLAIN, deadlock, query plan.

softspark/ai-toolkit · 55 tokens

software-database-design

Designs database schemas, migrations, and data models for PostgreSQL, MySQL, MongoDB, and Redis. Use when planning tables, relationships, indexes, or ORM-backed schema changes.

vasilyu1983/AI-Agents-public · 43 tokens