Home/Prompt Templates/Write a SQL Query
CodingClaude

How to Write a Great AI Prompt to Write a SQL Query

Most people ask AI to "Write a SQL query for my database"—and wonder why the output is mediocre. Here's how to write a prompt that actually gets you what you want on the first try.

Before

"Write a SQL query for my database"

Too vague—AI has to guess what you want

After

"Write a SQL query for a PostgreSQL database. Tables: orders (order_id, customer_id, order_date, total_value), customers (customer_id, name, country, signup_date). Goal: find the top 10 customers by total spend in the last 12 months, showing name, country, total spend, and number of orders. Exclude customers who signed up less than 30 days ago. Add a comment explaining each clause."

Specific, clear, ready to use

What makes the strong prompt better?

Database type specified ensures correct syntax (PostgreSQL vs MySQL differs)

Full schema provided means the query will actually run

Commented clauses make the query maintainable and educational

Pro tips for write a sql query prompts

  • Always specify your target audience or reader
  • Include concrete examples or context from your situation
  • Set tone guidance—formal, casual, witty, empathetic
  • Add length constraints to avoid rambling output
  • Exclude what you don't want (e.g., "no buzzwords", "no clichés")

Related prompt templates

Ready to build your perfect prompt?

Try Prompt Orange free—get a scored, refined prompt in under 2 minutes. No signup required.

Build my prompt now
Get started free