Prompts / SQL from a schema
SQL from a schema
Get a correct query against your actual schema, with the reasoning shown, instead of a plausible-looking guess.
Copying runs entirely in your browser - nothing here is ever sent anywhere.
Fill in the variables
Here is my database schema (DDL or a plain description of tables/columns/
relationships):
{{schema}}
Write a SQL query that does the following: {{request}}
Before the query, briefly state which tables and join conditions you're
using and why, so I can catch a wrong assumption about the schema before
running anything. Use only tables and columns that actually appear in the
schema above - if something you need doesn't exist there, say so instead
of inventing a plausible column name.
After the query, note anything that could make it slow at scale (a missing
index it depends on, a join that could fan out, a subquery that could be
a window function instead) and how you'd verify the query is correct
before running it against real data (e.g. a smaller LIMIT first, an EXPLAIN).
When to use
Writing a query against a schema you didn’t design, or one with enough tables that getting a join wrong is easy. Especially useful for a reporting or analytics query where a subtly wrong join silently produces a plausible but incorrect number.
Why it works
A model given a schema description alone will often invent a column name that “sounds right” for the request instead of admitting the schema doesn’t have it - explicitly forbidding that, and asking for the table/join reasoning up front, gives you a chance to catch a misunderstanding before running anything against real data. Asking for scale/correctness notes surfaces the kind of review a human DBA would give, instead of a query that merely looks plausible.
Variations
- Paste actual
EXPLAINoutput back in and ask “why is this slow” for a performance-focused follow-up. - Add “Also write the equivalent as a CTE for readability” if the query will be maintained by other people.
- For a schema you can’t fully paste (too large), describe just the tables you think are relevant and ask the model to first confirm which other tables it would need to see before answering.