Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Write SQL query
on GPT-4o-mini

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

The "Vibe" Prompt

"Write a SQL query to select all users who registered in the last 30 days and have made at least one purchase, ordered by their registration date. The user table is named 'users' and has columns 'user_id', 'username', 'registration_date', and 'last_login'. The purchase table is named 'orders' and has columns 'order_id', 'user_id', 'order_date', and 'total_amount'."
Low specificity, inconsistent output

Optimized Version

STABLE
{ "task": "SQL Query Generation", "context": { "database_schema": { "users": [{"column": "user_id", "type": "INT", "is_pk": true}, {"column": "username", "type": "VARCHAR"}, {"column": "registration_date", "type": "DATE"}, {"column": "last_login", "type": "DATE"}], "orders": [{"column": "order_id", "type": "INT", "is_pk": true}, {"column": "user_id", "type": "INT", "fk_to": "users.user_id"}, {"column": "order_date", "type": "DATE"}, {"column": "total_amount", "type": "DECIMAL"}] }, "current_date": "GETDATE()" }, "constraints": [ "Select all users.", "Users must have registered within the last 30 days relative to the current date.", "Users must have made at least one purchase.", "Order the results by registration date in ascending order.", "Only return user_id, username, and registration_date from the users table." ], "output_format": "SQL Query" }
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt leverages a structured JSON format, explicitly defining the task, providing the schema in a machine-readable way (including data types and relationships), and itemizing constraints. This reduces ambiguity and the need for the model to parse natural language structure. The 'chain-of-thought' implicit in breaking down requirements allows the model to process each constraint systematically. Explicitly stating the 'output_format' ensures the model understands the desired output. By formalizing the input, it guides the model more directly towards the correct SQL construction, reducing potential misinterpretations and making it more efficient.

10%
Token Efficiency Gain
The generated SQL uses `JOIN` between `users` and `orders`.
The `WHERE` clause correctly filters `registration_date` within the last 30 days.
The `WHERE` clause or `HAVING` clause effectively ensures at least one purchase.

Ready to stop burning tokens?

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

Optimize My Prompts