Back to Prompt Library
Coding
SQLCodingDatabase

SQL Query Generator and Explainer

Writes and explains SQL queries for any database type.

Best For

  • Product managers or analysts who know what data they need but don't write SQL day to day.
  • Backend developers switching between database dialects who want a quick, correct query without checking syntax docs.
  • Data teams onboarding a non-technical stakeholder who needs the query's logic explained in plain English.

Prompt Template

Generate a SQL query to [DESCRIBE WHAT YOU WANT TO RETRIEVE OR DO]. The database uses [DATABASE TYPE, PostgreSQL/MySQL/SQLite]. Relevant tables and their key columns: [LIST TABLES AND COLUMNS]. Requirements: [ANY SPECIFIC REQUIREMENTS, filtering, ordering, joining, aggregating]. After writing the query, explain each clause in plain English so a non-developer could understand what it does. Then suggest one optimization if applicable.

Pro Tip

List the actual column names and types from your schema instead of describing tables loosely, since vague table descriptions are the most common cause of wrong joins.

Example Output

A sample of what this prompt produces once you fill in the placeholders.

For 'total orders per customer in the last 30 days' against PostgreSQL tables orders and customers, the output is a SELECT with a JOIN on customer_id, a WHERE clause filtering order_date, and a GROUP BY customer name. The plain-English explanation walks through the join and date filter, and the optimization tip suggests adding an index on orders.order_date.

How to use this prompt

  1. Copy the prompt template using the button above.
  2. Paste it into your preferred AI assistant (ChatGPT, Claude, Gemini, etc.).
  3. Replace all bracketed placeholders like [TOPIC] with your specific details.
  4. Send the prompt and refine the output as needed.