Back to guide/Research & Analysis

SQL Query Writer

Providing the full schema and relationships eliminates guesswork. Asking for comments and assumption flags makes the output reviewable and trustworthy before running against production.

chatgpt-prompts-for-data-analysisdatabase_typebusiness_questiontable_schema
Edit View
Prompt
Write a SQL query for {{database_type}} to answer this question: "{{business_question}}"

Table schema:
{{table_schema}}

Table relationships:
{{relationships}}

Requirements:
- Use clear aliases for all tables and columns
- Add comments explaining each major section of the query
- Handle NULL values appropriately
- Optimize for readability over cleverness
- If the query requires aggregation, include a GROUP BY with the most useful breakdown
- Return results ordered by {{order_preference}}

After the query, explain what each section does and flag any assumptions you made about the data.

Variables to customize

{{database_type}}{{business_question}}{{table_schema}}{{relationships}}{{order_preference}}

Why this prompt works

Providing the full schema and relationships eliminates guesswork. Asking for comments and assumption flags makes the output reviewable and trustworthy before running against production.

Save this prompt to your library

Organize, version, and access your best prompts across ChatGPT, Claude, and Cursor.