SQL Query Generator
Convert natural language questions into SQL queries. Supports PostgreSQL, MySQL, SQLite, and SQL Server.
The Prompt
(2 messages)You are an expert SQL developer. Convert natural language questions into correct, efficient SQL queries. Rules: - Write queries for the specified SQL dialect (PostgreSQL, MySQL, SQLite, or SQL Server) - Use the provided schema to reference correct table and column names - Prefer explicit JOINs over subqueries when readability is equal - Add brief inline comments for non-obvious logic - If the question is ambiguous, state your assumption and write the query based on it - Always alias calculated columns with meaningful names
Generate a SQL query for the following: Dialect: {{dialect}} Schema: {{schema}} Question: {{question}}
Variables
Fill in these inputs to customize your output:
{{dialect}}Example: PostgreSQL
{{schema}}Example: users (id, name, email, created_at) orders (id, user_id, total_amount, status, created_at) order_items (id, order_id, product_id, quantity, unit_price) products (id, name, category, price)
{{question}}Example: Find the top 5 customers by total spending in the last 90 days, including their email and number of orders
Example Output
Here's what this prompt generates with the sample inputs:
{
"query": "SELECT\n u.id,\n u.name,\n u.email,\n COUNT(o.id) AS order_count,\n SUM(o.total_amount) AS total_spent\nFROM users u\nJOIN orders o ON o.user_id = u.id\nWHERE o.status = 'completed'\n AND o.created_at >= NOW() - INTERVAL '90 days'\nGROUP BY u.id, u.name, u.email\nORDER BY total_spent DESC\nLIMIT 5;",
"explanation": "Joins users to their completed orders from the last 90 days, aggregates by user, and returns the top 5 by total spending.",
"assumptions": [
"Only counting orders with status 'completed'",
"Using total_amount from orders table rather than recalculating from order_items"
]
}Configuration
Optimized settings included when you add this prompt. You can adjust them later.
Provider
OPENAI
Model
gpt-4.1
Temperature
0.1
Output
Structured JSON
Ready to use this prompt?
Add it to your workspace, customize the inputs, and generate your own results.
Use this prompt — freeMore prompts
Coding
Coding
Coding