---
name: write-sql-from-plain-english-prompt
description: A prompt that writes a SQL query from a plain-English question - a correct, readable query for your database and schema - explicit columns, clear joins and filters, read-only by default - and explains what it returns and what to double-check. Use when the user needs a SQL query written, asks how to get some data out of their database, or does not know SQL.
license: CC0-1.0
metadata:
  category: coding-prompts
  tags: "sql,database"
  author: johnny-bravo
  version: "1.0"
  updated: "2026-11-02"
  status: published
  aliases: "text to sql, natural language to sql, write a query"
  source_url: "https://github.com/f/prompts.chat/blob/b09880e229d2f091ffa2254bc187532fb8980f9b/prompts.csv"
  source_license: CC0-1.0
  copyright: "Public domain (CC0 1.0) - prompt contributed to prompts.chat by a prompts.chat contributor"
  original_author: "A prompts.chat contributor ('SQL Query Generator from Natural Language')"
  modified: "Adapted by TheSkillVault: rewrote the structured JSON prompt as a fill-in prompt with schema, dialect and read-only rules, an explanation and a sanity-check section; added steps, adapt notes, safety and done-when."
  imported_on: "2026-11-02"
---

# Write SQL from plain English (prompt)

Describe the data you want and get a query you can read and trust, for your actual tables.

## When to use
- You know what data you want but not how to write the SQL.
- You want a report: sign-ups per week, top customers, unpaid invoices.
- You want a first draft of a tricky join or group-by.

## When not to use
- A query works but is slow. Use optimise-slow-sql-queries.

## Steps
1. Get your schema: table names, columns and how tables link. In Postgres, `\d tablename` shows it; most tools can export it.
2. Fill in {{DATABASE}} (PostgreSQL, MySQL, SQLite, SQL Server) and your question.
3. Paste the prompt and run the query on a copy or with a `LIMIT` first.
4. Check a few rows by hand before you rely on the numbers.

## Prompt
```text
Write a SQL query for {{DATABASE}}.

Question: {{WHAT YOU WANT, IN PLAIN ENGLISH}}

Schema:
{{TABLES, COLUMNS, KEYS}}

Rules:
1. Read-only: use SELECT only. If the question needs INSERT, UPDATE or DELETE, show it separately with a warning and a WHERE clause, and never include DROP or TRUNCATE.
2. List columns explicitly; no SELECT *.
3. Use short, clear table aliases and explicit JOIN ... ON.
4. Handle NULLs and time zones deliberately, and say how.
5. Add LIMIT for exploratory queries.

Then give me:
- A plain-English explanation of what the query returns.
- Assumptions you made about the schema or the question.
- Two checks to confirm the result is right (for example a row count or a known value).
```

## How to adapt
- Using an ORM? Add "also show the Prisma (or Drizzle) version".
- Building a dashboard? Ask for a query per metric with the same date filter.

## Gotchas
- Never paste production passwords, connection strings or real customer rows into an AI tool. Column names are enough.
- Run generated writes inside a transaction you can roll back, and back up first.
- Wrong joins quietly double-count. Check totals against a number you already know.

## Portability
Works with any SQL database and any chat AI or coding agent. No tool lock-in.

## Done when
- The query runs and returns what the question asked.
- You checked the result against at least one known number.

## Example prompt
"Using my orders and customers tables in Postgres, write a query for the top 10 customers by total spend in the last 90 days."

---
Based on the "SQL Query Generator from Natural Language" prompt by a prompts.chat contributor (CC0 1.0, public domain).
