Coding & Software

Write a SQL Query From Your Schema

Writes a SQL query from a plain-English question and your table definitions, with assumptions listed and checks to verify the result.

Designed for general-purpose AI assistantsPublished November 2026

Customize

Fill in the fields and the prompt updates. What you type stays in this page and is never saved or sent.

Preview

Write a SQL query that answers the question below, using only the tables and columns I give you.

Database: PostgreSQL
Question in plain English: {{question}}

Schema (CREATE TABLE statements or a column list with types and keys):
{{schema}}

Facts about the data that affect the query: none
Result I expect to see (columns and a rough row count): {{expected result}}

Return:
1. The query, formatted for readability, with a short comment on any non-obvious clause.
2. Assumptions you made, each as one line (for example how you treated NULLs, duplicates, time zones, soft-deleted rows or ties).
3. Two or three checks I can run to confirm the result is right, such as a count that must match another query, or a sample row I can verify by hand.
4. One thing that could make this query slow on a large table and how an index or rewrite could help.

Rules:
- Do not invent tables or columns. If the question needs data the schema does not contain, say what is missing and stop.
- If the question is ambiguous (for example "active users" with no definition), ask me before writing the query.
- Prefer explicit JOIN syntax and name every column you select. Do not use SELECT *.
- Use only syntax that works in PostgreSQL.

3 required fields are empty and will stay as a {{placeholder}} in the copied text.

How to use it

  1. Paste real CREATE TABLE statements or a column list with types, primary keys and foreign keys. Column names alone leave the join logic to guesswork.
  2. Define business terms such as active, churned or revenue in the data notes. They are the most common source of a query that runs but answers the wrong question.
  3. Run the checks from step 3 before trusting the numbers, especially the one that compares totals against a simpler query.
  4. Never run a generated query that writes data against production. This prompt is for reads; review any UPDATE or DELETE line by line.

Example output

Illustrative only. Real answers depend on your input and the assistant you use.

Query:

SELECT c.id, c.email, SUM(o.total_cents) / 100.0 AS revenue
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'paid'
  AND o.created_at >= date_trunc('month', now()) - interval '3 months'
GROUP BY c.id, c.email
ORDER BY revenue DESC
LIMIT 10;

Assumptions:
- Only orders with status 'paid' count as revenue; refunds are not in this schema so they are not subtracted.
- "Last three months" means since the start of the month three months ago, in the database time zone.

Checks:
- The sum of revenue for all customers should equal SELECT SUM(total_cents)/100.0 FROM orders with the same filters.
- Pick the top customer and add up their paid orders by hand.

Performance: an index on orders(status, created_at) would let the filter avoid scanning the whole table.

Tips

Ambiguous terms are where generated SQL goes wrong while still looking correct. Define them in the data notes, or let the assistant ask.

If you have a data dictionary, paste the relevant part. A column called status means very little without its list of values.

For a query that touches more than four or five tables, build it up in stages: ask for the first join and verify it before adding the next.

  • Coding & Software

    Write Unit Tests for a Function

    Generates focused unit tests for one function, covering normal cases, boundaries and invalid input without mocking more than needed.

    Customizable fields
    3 fields
  • Coding & Software

    Explain Unfamiliar Code

    Explains code you did not write at the level you ask for, tracing the data flow and flagging what it cannot tell from the snippet alone.

    Customizable fields
    4 fields
  • Coding & Software

    Optimize a Slow SQL Query

    Reads a slow query and its execution plan, explains where the time goes and proposes changes you can test one at a time.

    Customizable fields
    7 fields