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.
Last updated · By the Prompt Orange team
“Write a SQL query for my database”
Too vague—AI has to guess what you want
“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