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.

You get: Working code with an explanation

More like this

The prompt

sql-query-from-schema-and-question.md
You are a senior data engineer who writes clear, correct SQL. My database schema is: {paste your database schema here}. My question is: {your question}. If the SQL dialect (PostgreSQL, MySQL, SQL Server, SQLite, BigQuery) or the meaning of any column is unclear, ask me up to two short questions first. Rules: use only tables and columns that exist in the schema; state every assumption about joins, duplicates, NULLs, time zones and what counts as a match; prefer readable CTEs over deep nesting; avoid SELECT star. Output format: 1) Assumptions as a short list. 2) The query in one code block. 3) Clause-by-clause explanation in plain words. 4) Indexes worth adding as CREATE INDEX statements, each with the clause that benefits and a write-cost note, or say none are needed. 5) Edge cases and a quick sanity check I can run to verify the result, such as a count or a known row. 6) Missing information and next actions. The answer is good if the query runs as written on the stated dialect and I understand why each clause is there.

Example output

AI-written example
-- DIALECT: PostgreSQL 14
-- SCHEMA USED:
--   customers(customer_id, email, created_at)
--   orders(order_id, customer_id, status, ordered_at timestamptz, currency)
--   order_items(order_item_id, order_id, product_id, quantity, unit_price, discount_amount)
--   products(product_id, name, category)
-- QUESTION: Top 5 product categories by net revenue last full calendar quarter,
--           with order and customer counts.

-- 1) ASSUMPTIONS
--  * "Last quarter" = the previous full calendar quarter, in UTC.
--  * Orders with status 'cancelled' or 'refunded' do not count.
--  * Net revenue = quantity * unit_price - discount_amount (NULL discount = 0).
--  * All orders share one currency; there is no conversion.
--  * Products with a NULL category are grouped as 'Uncategorized'.
--  * Ties on revenue are broken alphabetically by category.

-- 2) QUERY
WITH bounds AS (
    -- Half-open window [q_start, q_end), comput
Generated from this prompt by AI. Your results will vary.

Tips and common edits

Use when you know the question but not the query. Paste CREATE TABLE statements or a column list with keys, and state your question in plain English.

Questions about this prompt

More in Coding

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.

Explain This Error with Ranked Causes

Explains an error and stack trace in plain words, ranks the three most likely causes and gives a confirming check for each.

Regex with Match and Non-Match Test Strings

Writes a regex for your pattern, explains each part and provides 8 strings that should match and 8 that should not.

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.

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.

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.

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.

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.

Trending

Developmental Edit Memo for an Essay Draft With Revise-or-Cut Decisions

Gives a draft essay a structured developmental edit: thesis check, paragraph-by-paragraph audit, and a prioritized revision plan so you can decide what to keep, rewrite, or cut before line editing.

30-Minute Moderated Usability Test Script with Tasks and Notes Template

Produces a timed 30-minute moderated usability script with task scenarios, neutral follow-up questions and a ready-to-use notes template.