Software and Code Intermediate 2 min read

SQL Query Prompt (Schema Included)

SQL written without the schema rarely runs; this prompt requires schema, relationships and expected output.

Works with: ChatGPTClaudeGemini Updated: 24 October 2026
Prompt 1099 characters · 7 variables
You are a database engineer. Write SQL against the schema I give you and nothing else.

SCHEMA
```sql
[CREATE TABLE STATEMENTS OR COLUMN LIST WITH TYPES]
```

DIALECT: [POSTGRESQL / MYSQL / SQLITE / SQL SERVER / BIGQUERY]

QUESTION
[WHAT I WANT TO KNOW, IN PLAIN LANGUAGE]

EXPECTED OUTPUT
- Columns: [COLUMN NAMES AND TYPES]
- Rows: [ONE PER X]
- Ordering: [SORT RULE]
- Edge cases: [NULLS, DUPLICATES, EMPTY GROUPS]

OUTPUT
1) ASSUMPTIONS — joins I inferred, filters I added, and why.
2) QUERY — formatted, commented per CTE or subquery.
3) EXPLANATION — what each part does, in order of execution (FROM → WHERE → GROUP BY → HAVING → SELECT).
4) EDGE CASES — how the query behaves with NULLs, duplicates and empty tables.
5) PERFORMANCE — indexes that would help and the likely bottleneck.
6) TEST — a small set of sample rows and the expected result.

RULES
- Use only tables and columns that exist in my schema. If something is missing, say so instead of inventing it.
- No SELECT *. Name the columns.
- If the question is ambiguous, give the two most likely interpretations as separate queries.

SQL correctness is 80% schema knowledge and 20% syntax. Give the schema.

02 — Variables

Fill in the brackets

The highlighted fields are the places where your own details go. You do not have to fill all of them; anything left empty is completed by the model's own assumption.

[CREATE TABLE STATEMENTS OR COLUMN LIST WITH TYPES]
[COLUMN NAMES AND TYPES]
[ONE PER X]
[SORT RULE]
[POSTGRESQL / MYSQL / SQLITE / SQL SERVER / BIGQUERY] Field to fill in the prompt
[WHAT I WANT TO KNOW, IN PLAIN LANGUAGE] Field to fill in the prompt
[NULLS, DUPLICATES, EMPTY GROUPS] Field to fill in the prompt

03 — How to use

How to use it, step by step

  1. 01

    Paste the real DDL. Column names and types resolve most ambiguity before it starts.

  2. 02

    State the dialect — date functions and window syntax differ enough to break the query.

  3. 03

    Ask for the execution order explanation; it is how you catch a wrong join before running it.

04 — Variations

Same prompt, different result

One prompt does not solve every case. These variations ask for the same job in a different tone, length or format — pick the one that fits.

Explain plan

Same query, plus: read the plan, identify the sequential scan, and propose the index that removes it.

Migration

Same schema, but write the migration: add column, backfill, add constraint, in a safe order with a rollback.

05 — Tips

Notes that take the result up a level

  • SELECT * hides the shape of the result and breaks when the schema changes. Always name columns.
  • If the query is slow, the join order or a missing index is almost always the cause.

06 — FAQ

Frequently asked about Software and Code

Can the model write SQL without the schema?

It can write plausible SQL that fails on your column names. The schema is not optional.

How do I check correctness?

Run it against a small sample with known results. Ask for the test rows in the prompt; that is what step 6 is for.

Next

You may also find these useful

This page was prepared by the CopyPromt editorial team, first published 24 October 2026. Prompts are written to work across models; brand names are mentioned for identification only.