Skip to main content
← Prompt library

write-sql-query · gpt-4o · data · 90% confidence

Write a SQL query with GPT-4o — optimized prompt

A GPT-4o SQL prompt: schema as DDL in a fenced block, the question stated plainly, output is the query plus a one-line rationale and the assumptions made.

90% confidenceUpdated 9/7/2026write-sql-query · gpt-4o · data
Original prompt
Write me a SQL query that gets the top customers by revenue this year.
Optimized prompt
Act as a SQL engineer. Write one query for the target dialect and nothing else around it.

Dialect: [Postgres / MySQL / BigQuery / ...]
Schema:
```sql
[PASTE CREATE TABLE STATEMENTS or column lists with types]
```
Question: [what the result set must contain, and the grain — one row per what?]
Filters: [date range, status, tenant, etc.]

Return:
1. The query, formatted, as a fenced sql block.
2. Assumptions — one line each (which column is the join key, how NULLs are treated, tie-breaking).
3. One line: what index or partition this leans on.

No explanation paragraph, no sample output. If the schema is missing a column you need, name it and stop.

Why this GPT-4o prompt works

Tuned for GPT-4o: schema as DDL in a fenced block, the grain stated plainly, output limited to the query plus one-line assumptions. GPT-4o writes correct SQL fast when the target dialect and join keys are pinned and it is told not to explain.

What to check in the output

  • The generated SQL query correctly joins 'orders' and 'customers'.
  • The generated SQL query correctly filters by 'order_date' > '2023-01-01'.
  • The generated SQL query correctly filters by 'total_amount' > 100.

Slug89464fed-3a74-4093-9ec4-54eb60198410

ModelGPT-4o