Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Write SQL query
on GPT-4o

Stop guessing. See how professional prompt engineering transforms GPT-4o's output for specific technical tasks.

The "Vibe" Prompt

"Hey GPT-4o, can you write me a SQL query? I need to get all orders from the 'orders' table where the 'order_date' is after '2023-01-01' and the 'total_amount' is greater than 100. Also, join it with the 'customers' table on 'customer_id' to get the customer's name."
Low specificity, inconsistent output

Optimized Version

STABLE
You are a SQL query generator. Your task is to write a SQL query based on the user's request. Here's the problem: Get orders and customer names. Filter orders by date and amount. Constraint Checklist: 1. Select all columns from 'orders' table. 2. Select 'customer_name' from 'customers' table. 3. Join 'orders' table with 'customers' table. 4. Join condition: 'orders.customer_id' = 'customers.customer_id'. 5. Filter 'orders.order_date' > '2023-01-01'. 6. Filter 'orders.total_amount' > 100. Thought Process: 1. Identify the primary tables needed: 'orders' and 'customers'. 2. Determine the columns to select: all from 'orders' and 'customer_name' from 'customers'. 3. Establish the join type and condition: INNER JOIN on 'customer_id'. 4. Apply the date filter: WHERE order_date > '2023-01-01'. 5. Apply the amount filter: AND total_amount > 100. 6. Construct the SQL query step-by-step based on these decisions. SQL Query:
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt breaks down the request into clear components: a role definition, a concise problem statement, a 'Constraint Checklist' for explicit requirements, and a 'Thought Process' section using chain-of-thought to guide the model's reasoning. This structured approach helps GPT-4o systematically address each requirement, reducing ambiguity and increasing the likelihood of generating the correct and complete SQL query. The 'Thought Process' mimics how a human would approach the problem, leading to better structured and more accurate outputs. It also clearly delineates the expected output with 'SQL Query:', reducing extraneous text.

0%
Token Efficiency Gain
The generated SQL query correctly joins 'orders' and 'customers'.
The generated SQL query correctly filters by 'order_date' > '2023-01-01'.
The generated SQL query correctly filters by 'total_amount' > 100.

Ready to stop burning tokens?

Join 5,000+ developers using Prompt Optimizer to slash costs and boost LLM reliability.

Optimize My Prompts