dataOpenai
SQL Generation Prompt (OpenAI)
This variant uses the ChatGPT system/user format and requests markdown code blocks, which GPT-4o renders with syntax highlighting in the chat interface. The system message instruction to always use code blocks is more reliable than including it in the user message alone, especially in multi-turn conversations.
Prompt Template
## System
You are a database expert. Always respond with valid {{dialect}} SQL and a plain-English explanation. Format code in markdown code blocks with sql syntax highlighting.
## User
Write a {{dialect}} query to answer this question:
**Question:** {{question}}
**Schema:**
```sql
{{schema}}
```
**Requirements:** {{requirements}}
Respond with:
1. The SQL query in a ```sql``` block
2. A plain-English explanation of what the query does (2-3 sentences)
3. Performance notes: any indexes that would speed this up
4. Sample output: what 2 example rows would look likeVariables
{{dialect}}SQL dialect: PostgreSQL, MySQL, SQLite, BigQuery{{question}}Business question in natural language{{schema}}Relevant CREATE TABLE statements{{requirements}}Extra constraints or "None"Example
Input
dialect: PostgreSQL question: Show me the top 5 products by revenue in the last 30 days schema: CREATE TABLE products (id INT, name TEXT, price NUMERIC); CREATE TABLE order_items (id INT, order_id INT, product_id INT, quantity INT, created_at TIMESTAMPTZ); requirements: Exclude cancelled orders (no orders table available, assume all items are valid)
Output
```sql SELECT p.name, SUM(oi.quantity * p.price) AS revenue FROM order_items oi JOIN products p ON oi.product_id = p.id WHERE oi.created_at >= NOW() - INTERVAL '30 days' GROUP BY p.id, p.name ORDER BY revenue DESC LIMIT 5; ``` **Explanation:** Joins order_items with products to compute revenue (quantity × price) for each product in the last 30 days, then returns the top 5 by total revenue. **Performance:** Add an index on order_items(created_at) to speed up the date filter on large tables.
Related Tools
FAQ
- Can I use Code Interpreter to run this query?
- No — Code Interpreter runs Python, not SQL. Paste the query into your database client (pgAdmin, DBeaver, etc.) or use the Python code: conn.execute(query) if you have a database connection.
- How is this different from the generic SQL generation prompt?
- This version uses the ChatGPT system/user separation and markdown formatting. The generic prompt produces more detailed comments inside the query body. Use this version for quick queries in the ChatGPT interface.
- Can GPT-4o handle complex multi-table queries?
- Yes, for up to 10-15 tables. For more complex schemas, include only the relevant tables and their relationships in the schema field.
Related Prompts
SQL Query Generation Prompt
Generating SQL without schema context produces queries that reference non-existent columns...
Data Analysis PromptMost AI data analysis prompts produce vague observations like "sales increased in Q2". Thi...
CSV Data Processing PromptCSV processing tasks involve numerous small decisions about null handling, column types, a...