Skip to content

commandData and AI

SQL From Question

Turns a plain-language question into a SQL query that fits your real schema, explains each part and runs only read-only statements.

npx workspacetemplates add sql-from-question --tool claude-code

Writes 1 file to .claude/commands/

.claude/commands/sql-from-question.md14 lines · 1.1 KBFile
---
description: Turns a plain-language question into a SQL query that fits your real schema, explains each part and runs only read-only statements.
argument-hint: "[your question in plain words]"
---

Write SQL that answers this question: $ARGUMENTS

1. Find the schema before writing anything: migrations, `schema.sql`, ORM models or Prisma/Drizzle files. If a database connection is documented, use `\d` or `information_schema` to confirm. Note the SQL dialect.
2. Restate the question in one sentence and list the tables and columns you will use, with the join keys. If the question is ambiguous (which date, which status counts as paid), list the choices and pick the likeliest, saying so.
3. Write the query with explicit joins, readable aliases, and filters that handle nulls and time zones. Use CTEs for steps.
4. Explain each clause in one line each.
5. Warn about cost: missing indexes on filtered columns, huge tables, no LIMIT.

Only SELECT or EXPLAIN may be run, never INSERT, UPDATE, DELETE or DDL. Show the query and ask: "Run it read-only?" If yes, run it with a row limit and interpret the result in plain words.

What it does

Asking a database a question usually starts with guessing table and column names. This command reads the actual schema in your project first, so the query it writes uses columns that exist.

You get the SQL, a line-by-line explanation, and the assumptions it made, such as how it treated nulls or time zones. It runs only SELECT statements, and only if you say yes.

Example

you › /sql-from-question which 10 customers spent the most in the last 90 days, excluding refunds?

agent › Tables: customers, orders, refunds (orders.customer_id, refunds.order_id).
Assumption: "spent" is orders.total_cents minus refunded amounts, status = paid.
Query uses a CTE per customer and LIMIT 10. Uses idx_orders_created_at.
Run it read-only?

Questions

Can it change my data?
No. It writes and runs only SELECT or EXPLAIN statements, and only after you agree.
What if the question is vague?
It lists the interpretations, picks the most likely one, and states that choice so you can correct it.
  • Analyze CSV

    Commandpython

    Free
  • Clean Notebook

    Commandpython

    Free
  • Eval Prompt

    CommandData and AI

    Free
  • Profile DataFrame

    Commandpython

    Free