sql-write
Write a SQL query that answers a specific question; parameterized, dialect-aware, readable, with the assumed schema documented inline.
- License 许可
- MIT
- 最近更新
- 2026-10-29
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- sql-write
- description
- Write a SQL query that answers a specific question; parameterized, dialect-aware, readable, with the assumed schema documented inline.
03 SKILL.md 正文
sql-write
Translate a question into a SQL query. Output the query, the schema assumptions, and how to verify the result.
Mode: analytical — judgment inside a fixed frame. Keep the output shape; every verdict cites its evidence; unknowns are marked [TBD] or [VERIFY], never filled in.
When to use
- A specific analytical or operational question needs SQL.
- A query is being copied around and needs to be canonical.
Inputs
- The question in English.
- Database dialect (Postgres, MySQL, SQLite, DuckDB, BigQuery, Snowflake, Redshift). Different dialects have different functions (
DATE_TRUNCvsDATE_FORMAT,INTERVAL '7 days'vsINTERVAL 7 DAY). - Schema: table and column names. If unknown, ask before guessing.
Procedure
- Restate the question. "How many active users in the last 7 days, by country" → exact rows, exact columns, exact grain.
- Identify the dialect. Different
DATE_TRUNC/ window-function syntax. - Sketch tables and joins before writing the query. Document the join keys.
- Use CTEs for readability over deeply-nested subqueries. Each CTE has one purpose; name it after the purpose.
- Parameterize. Use placeholders (
$1,?,:name) — never string-concatenate user input. - Sargable predicates. Keep indexed columns bare on one side of the comparison (
created_at >= now() - interval '7 days', notdate(created_at) = …); predicate order in the SQL text doesn't matter to the planner. - Aggregations — explicit
GROUP BY <columns>; don't rely on MySQL's loose grouping. UseHAVINGfor filtering aggregates,WHEREfor filtering rows. - Window functions when the question is "top-N per group" or "running sum / rank".
- NULL and time traps:
NOT IN (subquery)returns nothing if the subquery yields a NULL — useNOT EXISTS; state the time zone of every date boundary (date_trunc('day', ts AT TIME ZONE 'UTC')). - Running it: queries you execute yourself are read-only — explore with a
LIMITand against a replica where one exists. AnyINSERT/UPDATE/DELETE/DDL is shown to the user first, run only on explicit approval, inside a transaction.
Output
-- Active users in the last 7 days by country.
-- Dialect: Postgres 15. Grain: country code.
-- Tables: events(user_id, created_at, country_code).
-- Assumes country_code is indexed; (user_id, created_at) is indexed.
WITH recent_active AS (
SELECT DISTINCT user_id, country_code
FROM events
WHERE created_at >= now() - INTERVAL '7 days'
)
SELECT
country_code,
COUNT(*) AS active_users
FROM recent_active
GROUP BY country_code
ORDER BY active_users DESC;
Verification:
- Spot-check one country with a smaller window (
'1 day'); the count for the 7-day query should be ≥ the 1-day count for the same country. - Schema match: confirm
events.country_codeis non-null for the rows you care about; otherwise add anIS NOT NULLpredicate and discuss the bias.
Anti-patterns to avoid
SELECT *in production queries.- String-concatenated user input.
BETWEENwith timestamps when one side is exclusive in the intent.- Implicit type coercion in joins (
varchar = int).
04 同仓库更多 Skills
runbook
Write or refresh an operational runbook for an alert/symptom; structured as TL;DR + Pre-checks + Steps + Verification + Rollback + Escalation.
rfc
Draft a lightweight design RFC to docs/rfcs/<slug>.md; Summary, Motivation, Detailed design, Drawbacks, Alternatives, Unresolved questions. A proposal to debate, not a recorded decision (adr).
editorial-pass
Review and tighten prose in a Markdown artifact for a target tone (terse, formal, friendly) without dropping information; preserve quotations and load-bearing constraints.
architecture-review-facilitation
Run an architecture review as a process — entry criteria, reviewer checklist, decision log, and follow-up actions — pairing the design artifact with a red-team pass.
architecture-doc
Write a C4-style architecture document; System Context + Containers (+ Components when interesting) with mermaid diagrams. Describes structure, not a decision (adr) or proposal (rfc).
adr
Write an Architecture Decision Record (Michael Nygard format) to docs/adrs/NNNN-<slug>.md; frontmatter validated by the ADR artifact schema. Records a made decision, not a proposal to debate (rfc).