Prompts for writing sql queries
Prompts that translate a business question into a working, readable SQL query.
Translating a business question into correct SQL is where a lot of analysis stalls, and it's a sweet spot for AI when you give it your schema. The Write a SQL Query From a Business Question prompt does exactly this: hand it your table structure and a plain question and it returns a clean, commented query you can read and trust rather than a black box. The comments matter, because a query you can't explain is a query you can't defend when someone questions the number.
This collection treats SQL as part of a wider analytics workflow. The Use SQL Window Functions prompt handles the running totals, rankings, and period-over-period comparisons that trip people up, the Design a Normalized Database Schema prompt covers the structure your queries run against, and the Pandas prompts (Analyze a Dataset, Write a Data Cleaning Script) catch the cases where SQL isn't the right tool. The Choose the Right Chart Type and Plan a Data Dashboard prompts close the loop from query to presentation. The big pitfall AI helps you avoid is a query that runs cleanly but answers the wrong question, usually because of a quietly wrong join or an unhandled NULL.
What makes a good prompt for writing sql queries
The single most important input is your actual schema: table names, column names, types, and how tables relate. With that, the model writes joins that work; without it, it invents plausible column names that don't exist. Paste the relevant DDL or a description of the tables and state your SQL dialect, since Postgres, MySQL, and BigQuery differ in ways that break queries.
Then be precise about the question, including how to handle the awkward cases: do you want to count NULLs, deduplicate rows, include refunds? Ask the model to add comments explaining each clause and to flag any assumption it made about the data. A query you understand is one you can verify against a known number before you ship it to a dashboard.
Get sharper results
- 01Paste your real table and column names (or the DDL) and state your SQL dialect, so the model writes joins that execute instead of inventing columns.
- 02Spell out how edge cases should behave (NULLs, duplicates, date boundaries, refunds), because that's where a clean-looking query silently returns the wrong number.
- 03Ask for inline comments on each clause and always sanity-check the result against a number you already know before trusting the query.
- 04For ranking, running totals, or period-over-period math, ask specifically for window functions, which the model handles well but won't reach for unless prompted.
Common questions
Why does the AI reference columns that don't exist in my database?
Because it's guessing at your schema. The fix is to paste your actual table definitions or a clear description of each table and its columns. Once the model can see the real structure, the hallucinated column names disappear and the joins line up.
Can I trust an AI-written query without checking it?
Verify before you trust, especially for anything that feeds a decision. Run it against a known result first, like a total you can confirm another way. Queries that run without errors can still join incorrectly or mishandle NULLs and quietly return a wrong answer.
Does the SQL dialect matter when I prompt?
Yes. Date functions, string handling, and window-function syntax differ across Postgres, MySQL, SQL Server, and BigQuery. State your dialect in the prompt so the query runs on your engine rather than needing translation.
Write a SQL Query From a Business Question
Translate a business question into a clean, commented SQL query against your schema.
Design a Normalized Database Schema
Design a fully normalized relational database schema with DDL, indexes, and design rationale.
Analyze a Dataset With Pandas Step-by-Step
Generate step-by-step Pandas EDA code covering nulls, outliers, and a business question.
Plan a Data Dashboard Layout and Metrics
Plan a full dashboard with curated KPIs, chart types, layout, and vanity-metric warnings.
Write a Data Cleaning Script for Messy Data
Generate a step-by-step Pandas data cleaning script with issue detection and before/after summaries.
Analyze an A/B Test for Statistical Significance
Statistically analyze an A/B test result with significance testing and a ship/no-ship recommendation.
Choose the Right Chart Type for Your Data
Get a chart type recommendation with rationale, alternatives, and common pitfalls for your data.
Use SQL Window Functions for Advanced Analytics
Generate advanced SQL window function queries with explanations and performance notes.
Design a normalized database schema from plain requirements
Turns plain-language requirements into a normalized relational schema with DDL, relationships, and trade-offs.
Diagnose and rewrite a slow SQL query using its plan
Reads a query plan to explain why a SQL statement is slow, then rewrites it and recommends indexes.
Turn a Plain-English Question Into a SQL Query
Converts a natural-language business question into a correct, commented SQL query grounded in your real schema.