Struggling with SQL Queries? This Prompt-Writing Hack Changes Everything

In partnership with

SQL is the lifeblood of data analysis, but we’ve all been stuck staring at our screens, wondering, “Why isn’t this query working?” The good news? Writing the perfect SQL query starts with crafting the perfect prompt.

In today’s newsletter, I’ll break down how to write prompts for building SQL queries that eliminate guesswork, save time, and give you exactly what you need.

But before we dive in, here’s what caught my attention this week.

Best Content, News & Resources This Week

How AI Is Revolutionizing Data Analysis:
This blog explores how AI-driven tools are transforming the way data analysts approach their work.

Key Insight: Understanding how to write AI-friendly prompts is the future of efficient data workflows.

DataGPT:
A powerful tool for creating data insights through simple natural language prompts. This is a great resource for anyone experimenting with prompt-driven analysis workflows.

10 Prompts Every Data Analyst Must Use:
This Notion page outlines essential prompts to master for effective analysis.

Pro Tip: Bookmark this as a cheat sheet for writing better prompts in tools like SQL and Python.

The Daily Newsletter for Intellectually Curious Readers

If you're frustrated by one-sided reporting, our 5-minute newsletter is the missing piece. We sift through 100+ sources to bring you comprehensive, unbiased news—free from political agendas. Stay informed with factual coverage on the topics that matter.

How to Write Prompts for Building SQL Queries

Let’s dive into how you can craft SQL prompts that get you answers without wasting time on inefficient or incorrect queries.

1. Start with the End in Mind

Always begin with clarity:

  • What is the question you’re trying to answer?

  • What specific data do you need?

A vague prompt like “Get sales data” will lead to poorly structured queries. Instead, be as detailed as possible:

  • Example: “Retrieve total sales per product category for Q4 2024, grouped by region.”

Writing the desired outcome in plain English before converting it into SQL can make your queries more efficient and accurate.

2. Define Scope Explicitly

Ambiguity wastes time. Define these upfront:

  • Tables: Which dataset(s) are relevant?

  • Columns: What fields do you need?

  • Filters: What conditions must the data meet?

Example Prompt:
"From the 'transactions' table, get 'transaction_id', 'customer_id', and 'total_amount' for purchases made between January 1, 2024, and March 31, 2024, where total_amount > $500."

SELECT 
    transaction_id,
    customer_id,
    total_amount
FROM 
    transactions
WHERE 
    transaction_date BETWEEN '2024-01-01' AND '2024-03-31'
    AND total_amount > 500;

3. Use Hierarchical Prompts for Complex Queries

Break complex prompts into logical steps:

  1. Filter data.

  2. Aggregate values.

  3. Format or transform results.

Prompt Workflow Example:

  1. Retrieve customers with purchases > $500.

  2. Group by customer_id.

  3. Calculate total spending per customer.

This process ensures clarity and keeps queries readable.

SELECT 
    customer_id, 
    SUM(total_amount) AS total_spending
FROM 
    transactions
WHERE 
    transaction_date BETWEEN '2024-01-01' AND '2024-03-31'
GROUP BY 
    customer_id
HAVING 
    SUM(total_amount) > 500;

4. Provide Context (for Collaborators or AI)

When sharing prompts with others (or AI tools like ChatGPT), add details to ensure your intentions are clear.

  • Example Prompt for AI:
    "Write a query to find customers who haven’t made a purchase in the last 90 days. Use the 'customers' and 'transactions' tables. I need a list of customer IDs."

SELECT c.customer_id
FROM customers c
LEFT JOIN transactions t
  ON c.customer_id = t.customer_id
     AND t.transaction_date >= CURRENT_DATE - INTERVAL '90 days'
WHERE t.transaction_id IS NULL;

5. Test, Refine, Iterate

Even the best prompt needs tweaking. Once you run the query, review the output:

  • Does it answer your question?

  • Is it optimized for performance?

If not, revisit your prompt, add missing details, and re-run.

Common Prompt Pitfalls to Avoid:

  1. Vague prompts: Avoid “Get me the sales data.”

  2. Overloading one query: Break down large queries into smaller, focused steps.

  3. Ignoring context: Specify everything—tables, columns, filters, and even the relationships between datasets.

By mastering these steps, you’ll save time, reduce frustration, and get the insights you need faster than ever.

I hope today’s newsletter gave you actionable ideas for improving your SQL prompts! If you’re ready to take your query-writing skills to the next level:

  • Need Help? Reply to this email with your toughest SQL challenge, and I’ll help you rewrite the perfect prompt.

Finally, share this newsletter with your team. Better prompts = better queries = better data-driven decisions.

How helpful was this newsletter?

Login or Subscribe to participate in polls.