Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Write SQL query
on Llama 3.1 405B

Stop guessing. See how professional prompt engineering transforms Llama 3.1 405B's output for specific technical tasks.

The "Vibe" Prompt

"Write a SQL query to get all orders from the 'orders' table where the total amount is greater than 100, and the order date is after '2023-01-01', for customers located in 'New York'. Join with the 'customers' table to filter by city."
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert SQL query writer. Your goal is to generate a correct, efficient, and readable SQL query based on the provided schema and request. Always use aliases for table names if joins are involved. Always specify the columns explicitly in the SELECT clause; never use SELECT *. <SCHEMA> CREATE TABLE customers (customer_id INT PRIMARY KEY, name VARCHAR(255), city VARCHAR(255)); CREATE TABLE orders (order_id INT PRIMARY KEY, customer_id INT, total_amount DECIMAL(10,2), order_date DATE); </SCHEMA> <REQUEST> Get order_id, order_date, and total_amount for all orders placed after '2023-01-01' with a total amount greater than 100, where the ordering customer is located in 'New York'. </REQUEST> <INSTRUCTIONS> 1. Identify relevant tables: 'orders' and 'customers'. 2. Determine join condition: 'orders.customer_id = customers.customer_id'. 3. Select specified columns: order_id, order_date, total_amount. 4. Apply filters on 'orders' table: total_amount > 100 AND order_date > '2023-01-01'. 5. Apply filters on 'customers' table: city = 'New York'. 6. Combine all parts into a single SQL query. </INSTRUCTIONS> <SQL_QUERY_GENERATION_PLAN> - Start with SELECT clause with specified columns and aliases. - Add FROM clause for the primary table. - Add JOIN clause for secondary table with ON condition. - Add WHERE clause for combined filtering conditions using AND. </SQL_QUERY_GENERATION_PLAN> SQL Query:
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt leverages several techniques: 1. **Role-playing and Persona**: 'You are an expert SQL query writer' sets the expectation for quality. 2. **Explicit Schema**: Providing the DDL directly eliminates ambiguity and guides the model on available tables and columns. 3. **Clear Request**: The request is precise and avoids vague language. 4. **Chain-of-Thought (CoT)**: The '<INSTRUCTIONS>' section breaks down the task into logical steps, guiding the model's reasoning process. This is akin to showing the model how an expert would approach the problem. 5. **Generation Plan**: '<SQL_QUERY_GENERATION_PLAN>' provides a structured approach to constructing the query, reinforcing best practices like starting with SELECT, then FROM, JOIN, and WHERE. 6. **Constraints/Best Practices**: Directives like 'Always use aliases' and 'never use SELECT *' ensure adherence to good SQL practices. 7. **Specific Output Format**: Ending with 'SQL Query:' explicitly cues the model to output *only* the SQL query, reducing auxiliary text.

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

Ready to stop burning tokens?

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

Optimize My Prompts