Plain-English Question to a Correct SQL Query for Your Schema

Writes a SQL query from a plain-English question using your real table definitions, explains each part, checks it for common mistakes and suggests indexes if it will be slow.

You get: the query in a code block; a short explanation of each step; a list of the assumptions about the data, such as which column defines an active user; a small sample of the result…

More like this

The prompt

plain-english-question-to-a-correct-sql-query-for-your-schema.md
Act as a database engineer. Write a SQL query that answers this question: {your question in plain English}

First ask me for the CREATE TABLE statements or column lists for the tables involved, and which database I use (PostgreSQL, MySQL, SQL Server, SQLite, BigQuery or Snowflake), if I have not given them.

Rules:
1. Use only tables and columns from my schema. If something the question needs is missing, stop and say what is missing.
2. Watch for the usual traps: duplicate rows from joins, NULLs in filters and averages, time zones in date ranges, and integer division.
3. Use readable aliases and CTEs over deeply nested subqueries.
4. Read-only: never write UPDATE, DELETE or DROP unless I ask.

Output format: the query in a code block; a short explanation of each step; a list of the assumptions about the data, such as which column defines an active user; a small sample of the result shape; and index suggestions if the query will scan large tables, with the risks of each.

Tips and common edits

Paste your schema when asked. Works in ChatGPT, Claude or Gemini; in Cursor or GitHub Copilot, open the schema file so it reads the real columns. Test on a copy of the data first.

Questions about this prompt

More in Coding

SQL Query from Schema and Question

Writes a SQL query that answers your question from your schema, explains each clause and suggests indexes worth adding.

Edge-Case Test Suite for One Function in Your Existing Test Framework

Writes tests for a single function that cover normal inputs, boundaries, bad input and error paths, in the test framework your project already uses.

Characterization Tests Before a Refactor

Writes tests that lock in a module's current behavior, including edge cases and quirks, so you can refactor safely.

Codebase Tour for a Specific Task

Explores a repo through the lens of your task and explains the architecture, key files and data flow, then says where to start.

Debug an Error From the Message and the Code Around It

Finds the most likely cause of an error from the message and the relevant code, ranks other possible causes, and gives the smallest fix plus a way to prove it worked.

Explain a Codebase Folder to a New Developer on Their First Day

Produces an onboarding guide for one folder of a codebase: what it does, how data moves through it, the files to read first and the traps a new developer should avoid.

Failing Test First Bug Fix

Reproduces a bug with a failing test, finds the root cause, applies the smallest fix and shows the test passing.

Pull Request Review with Merge Recommendation

Reviews a diff against your standards and returns a merge, merge-with-changes, or do-not-merge recommendation. Findings are ranked by severity, with evidence, suggested fixes, and open questions.

Regex Builder With Match and No-Match Test Cases

Builds a regular expression from a plain-English description and example strings, explains each part, and proves it against strings that should and should not match.

Root Cause Analysis from Stack Trace and Logs

Turns an error report, stack trace, and logs into ranked root-cause hypotheses. Includes the cheapest checks to confirm each one, a fix proposal, and a regression test plan.

Unit Test Plan and Test Code for an Existing Function

Builds a behavior-based test matrix for a function or module and then generates test code in your framework. Covers edge cases, failures, and mocking boundaries, with coverage gaps stated honestly.

Architecture Decision Record for a Technology Choice

Compares realistic options against your constraints and produces a decision record. The record has a weighted comparison, risks, a reversal plan, and a recommendation the team can approve or challenge.