SQL query from a question
Professional structure: role, context, objective, details, steps, format, criteria and self-check. Designed for better results, not guaranteed ones: always review the answer.
Never use real personal data. If you need to, run it through the data cleaner.
## Role Act as a data analyst who is an SQL expert. ## Context I need to pull a figure from the database and I am not fluent in SQL. ## Objective Get a correct query for my database engine, with an explanation. ## Details (fill in what is in square brackets) - [engine: PostgreSQL, MySQL, SQL Server, BigQuery…] - [relevant tables and columns] - [business question] ## Step-by-step instructions 1. Restate the question in terms of tables and columns. 2. Write the query with clear aliases. 3. Explain each part: joins, filters and grouping. 4. Propose a check to validate the result. ## Output format Query in a code block, explanation and a checking query. ## Quality criteria - It uses the syntax of the stated engine. - It avoids SELECT * in the final result. - It handles nulls and duplicates in joins. ## Self-check Before answering, check every quality criterion. If information is missing, ask me instead of making it up, and at the end state what you have assumed.
Objective
Get a correct query for my database engine, with an explanation.
When to use it
This prompt is brilliant when you have a clear business requirement but struggle with the specific syntax required by your database engine. It is most useful for ad-hoc reporting, quick data exploration, or debugging legacy queries. However, avoid using this for complex database architecture changes or sensitive write operations, as an AI might misinterpret table relationships. Always verify the logic against your schema manually; while it provides a solid foundation, you remain responsible for the data's accuracy and integrity.
Worked example: input (fictional data)
Worked example: full response
Variants
Quick and concise
Provide only the SQL query and a brief one-sentence explanation for each major clause. Skip the validation step and detailed breakdown. Focus on getting a clean, working query as quickly as possible for experienced users who just need the syntax drafted.
For data beginners
Act as a patient tutor. Please write the query but explain each step in plain English, avoiding overly technical jargon. Include a breakdown of why we choose specific joins and how the aggregate functions work. Help me understand the 'why' behind the code, not just the code itself.
With schema context
Before writing the query, first analyse the table relationships provided. Identify potential issues, such as missing foreign keys or ambiguous column names. Then, write the query and include a comment block at the top detailing your assumptions about the data structure to ensure better results.
Common mistakes
- Not naming the engine: date functions differ.
- Running data-changing queries without a backup.
- Ignoring table relationships like primary and foreign keys. How to avoid it: Explicitly define your join keys in the prompt to ensure the AI connects the correct tables.
- Asking for complex multi-step analysis in one query. How to avoid it: Break down complex questions into simpler, sequential queries to maintain accuracy and readability.
- Failing to specify date formats or time zones. How to avoid it: Always state your preferred date format (e.g., YYYY-MM-DD) to prevent errors in your WHERE clauses.
- Overlooking potential null values in key columns. How to avoid it: Ask the AI to include COALESCE or IS NOT NULL checks if your data contains gaps.
Related guide
General and assistant-specific tips
Tips by assistant
- ChatGPT: you can attach the file (Excel, CSV or PDF) and ask it to analyse it; ask it to show the calculations it made.
Getting the most out of it
- Paste the prompt as is and only replace what is in [brackets].
- If the answer does not fit, do not start over: ask for “shorter”, “more formal” or “as a table”.
- Always review the result before using it: AI can get facts, dates or names wrong.
Before you paste it: quick checklist
- Remove client names, amounts and internal data before pasting.
- Say who the text is for and which tone you want.
- Ask for a specific length: “120 words max” or “three bullet points”.
- Read the result aloud and fix anything you wouldn’t say yourself.
How to adapt it to your assistant
The template works the same in ChatGPT, Gemini, Copilot or Claude because it only uses plain language. If your assistant supports fixed instructions (custom instructions, Gems or projects), you can store your preferred tone and format there and paste only the variable part. On a free plan, split long requests into two or three shorter messages.