Skip to content
Promptnic

SQL Query Writer With Schema Check

Writes correct SQL from plain English, states assumptions about your schema and explains performance.

Open in:ChatGPTClaudeGrokPerplexity

The prompt

You are a senior data engineer. Write a [SQL_DIALECT] query for this request: [PLAIN_ENGLISH_REQUEST] Schema: [TABLE_DEFINITIONS] Rules: - Use only tables and columns that exist in the schema. If something is missing, stop and say what you need. - Use CTEs with clear names for anything beyond one join. - Handle NULLs and duplicate rows explicitly. - After the query, explain it in 3 bullet points a non-engineer understands. - Add one note on indexes or performance if the tables could be large.

Fill in the variables

The prompt above updates live. Nothing leaves your browser.

Remix it

One-click variations. Stack them, then copy.

Open in Prompt Lab →

Why this prompt works

The "only use what exists" rule is the single biggest fix for hallucinated columns. CTEs make the logic auditable, and the plain-English explanation lets you sanity-check the query against what you actually asked for.

Framework: RTF. Role, Task, Format: the simplest reliable structure.

Example

Illustrative, not a real model output

Example input

[SQL_DIALECT]
PostgreSQL
[PLAIN_ENGLISH_REQUEST]
monthly revenue by plan for 2026, only paid invoices
[TABLE_DEFINITIONS]
invoices(id, customer_id, plan, amount_cents, status, paid_at)

What a good answer looks like (abridged)

WITH paid AS (SELECT plan, date_trunc('month', paid_at) AS month, amount_cents FROM invoices WHERE status = 'paid' AND paid_at >= '2026-01-01' AND paid_at < '2027-01-01') SELECT month, plan, SUM(amount_cents)/100.0 AS revenue FROM paid GROUP BY 1,2 ORDER BY 1,2; - Keeps only paid invoices from 2026 ...

How to use it

  1. Fill in 3 variables below (or in the prompt) with specific details.
  2. Paste it into ChatGPT (also works in Claude, Gemini, Grok).
  3. Check the result against the prompt’s rules and ask for a revision where it falls short.

ChatGPT tips

  • Put the output format (schema, table columns) near the top: ChatGPT follows explicit structure very reliably.
  • Use a Project or custom instructions for prompts you reuse daily.

Common mistakes

  • Leaving [VARIABLES] unfilled or vague ("my business") instead of specific ("a 3-person Calgary bakery").
  • Deleting the constraints to make the prompt shorter; they are what prevent generic output.
  • Accepting the first answer. Reply with what to fix and ask for a revision.

Guide: Promptnic template.

Frequently asked questions

Which AI model works best with the SQL Query Writer With Schema Check prompt?+

It is tuned for ChatGPT and also works in Claude, Gemini, Grok, DeepSeek. Put the output format (schema, table columns) near the top: ChatGPT follows explicit structure very reliably.

How do I use this prompt?+

Copy it, replace the 3 [VARIABLES] (sql dialect, plain english request, table definitions) with your details, then paste it into ChatGPT. You can fill the variables on this page and copy the finished version.

Is this prompt free to use?+

Yes. Every prompt on Promptnic is free to copy, adapt and use in personal or commercial work. A link back is appreciated, not required.

Why does this prompt work?+

The "only use what exists" rule is the single biggest fix for hallucinated columns. CTEs make the logic auditable, and the plain-English explanation lets you sanity-check the query against what you actually asked for.

Related prompts