5 Best ChatGPT Prompts for SQL Queries (2026)

From question to trustworthy query: schema-aware drafting, correctness checks, performance tuning, and turning one-off SQL into reusable views.

Verified against GPT-5.1 on 2026-07-125 prompts🟢 ChatGPT🔷 Gemini

Run = copies the prompt and opens the tool. Remix = save an editable copy to your workspace (free account).

1

Teach it your schema once

🟢 ChatGPT

Here is my database schema:
[PASTE CREATE TABLE STATEMENTS OR COLUMN LISTS]

Database: [POSTGRES/MYSQL/SQLITE/BIGQUERY]. Confirm your understanding: list each table's purpose in one line, the join keys between them, and any columns whose names are ambiguous (tell me what you're assuming they mean). Ask me up to 3 clarifying questions if the schema leaves real ambiguity.

Why this works: One schema-grounding step makes every later query dramatically more accurate.

Good output: A one-line-per-table summary and explicit assumptions you can correct now, not after a wrong query.

2

Draft the query from the business question

🟢 ChatGPT

Using that schema, write a query answering: [BUSINESS QUESTION — e.g. "monthly revenue per plan for the last 6 months, excluding refunded orders"].

Rules: comment each non-obvious clause with WHY, state which rows get excluded and by which condition, and if the question is ambiguous, implement the most likely reading and note the alternative.

Why this works: Forcing the exclusions to be explicit is where most 'wrong number in the dashboard' bugs die.

Good output: A commented query plus a one-line statement of what's excluded and why.

3

Verify correctness with adversarial rows

🟢 ChatGPT

Invent 8-10 rows of sample data that include the tricky cases for this query: refunds, nulls, timezone-boundary timestamps, duplicate events, a user with zero orders. Show the table, trace the query against it by hand, and confirm the output matches the business intent. If it doesn't, fix the query and re-trace.

Why this works: Hand-tracing adversarial rows catches the NULL and boundary bugs that look fine in production until month-end.

Good output: A worked example proving the query right — or a fixed query.

4

Performance pass

🟢 ChatGPT

Assume the big tables here have [ROUGH ROW COUNTS]. Analyze this query's plan risks: full scans, join order, functions on indexed columns, and OR conditions blocking index use. Suggest specific indexes (with exact CREATE INDEX statements) and a rewritten query if it materially helps. State the trade-off of each index.

Why this works: Index suggestions with explicit trade-offs beat 'add an index' hand-waving.

Good output: Concrete CREATE INDEX statements and an honest cost note for each.

5

Make it reusable

🟢 ChatGPT

This query will be needed regularly. Convert it into [A VIEW / CTE LIBRARY / dbt MODEL], parameterized by [DATE RANGE / SEGMENT]. Add a header comment documenting: business definition, exclusions, owner, and the verification date. Suggest a name that says what it MEANS, not what it joins.

Why this works: One-off queries become tribal knowledge; named, documented views become infrastructure.

Good output: A paste-ready view/model with a documentation header your future teammates will thank you for.

Frequently Asked Questions

Does this work for BigQuery / Snowflake?

Yes — name your dialect in step 1 and syntax adapts. The correctness and performance steps matter even more at warehouse scale.

Can I trust AI-generated SQL in production?

Trust the process, not the first draft: step 3's adversarial-row trace is the gate. A query that survives it is safer than most hand-written SQL that never got one.

Why is Gemini tagged on this pack?

For BigQuery-heavy stacks, Gemini's grounding in Google's dialect is convenient. The prompts run unchanged in either tool.

Make this workflow yours

Remix this pack into your workspace — edit the prompts, track your runs, and never hunt for them in old chats again.

← Browse all Prompt Packs