← Back to Prompt Library

💻 Coding & Development

SQL Query Builder

Describe what data you need in plain English and get a correct, efficient SQL query with an explanation of how it works.

The Prompt — replace [BRACKETS] with your details

Act as a database engineer. Write a SQL query for the request below.

Database type: [DATABASE, e.g., PostgreSQL, MySQL, SQLite]

Table schema(s):
[PASTE SCHEMA, e.g.,
orders(id, customer_id, total, status, created_at)
customers(id, name, email, country)]

What I need: [DESCRIBE IN PLAIN ENGLISH, e.g., "total revenue per country for completed orders in the last 30 days, highest first"]

Requirements:
- Use explicit JOINs and meaningful aliases
- Add a short comment above each non-obvious clause
- Explain the query in 2-3 sentences after writing it
- Flag anything that could be slow on large tables and suggest an index if relevant

How to use this prompt

  • Paste real table and column names — accuracy depends on the schema.
  • Say which SQL dialect you use; date functions differ between databases.

Works well with

ChatGPT
Claude
DeepSeek
Gemini

Need a custom version of this prompt?

The free prompt generator builds a prompt tailored to your exact goal, framework, and target AI model — or paste this template into the optimizer to refine it.