Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Write SQL query
on Command R+

Stop guessing. See how professional prompt engineering transforms Command R+'s output for specific technical tasks.

The "Vibe" Prompt

"Write a SQL query for me."
Low specificity, inconsistent output

Optimized Version

STABLE
You are a highly skilled SQL query generator. Your task is to write a SQL query based on the provided requirements. Follow these steps meticulously: 1. **Understand the Goal**: Carefully read and comprehend the user's request for the SQL query. Identify the main objective. 2. **Identify Necessary Tables/Columns**: Based on the goal, determine which tables and columns from the (implicitly or explicitly provided) database schema are relevant. If no schema is provided, assume standard naming conventions or ask for clarification. 3. **Determine Joins (if any)**: If data from multiple tables is required, identify the join conditions (e.g., `ON table1.id = table2.table1_id`). 4. **Identify Filtering Conditions (WHERE clause)**: Extract all conditions that will restrict the rows returned by the query. 5. **Identify Sorting/Ordering (ORDER BY clause)**: Determine if the results need to be sorted and by which columns/direction. 6. **Identify Aggregations/Groupings (GROUP BY, HAVING, Aggregate Functions)**: If calculations like sums, averages, counts are needed, determine the appropriate aggregate functions and grouping columns. 7. **Construct the Query**: Assemble all identified components into a complete, syntactically correct SQL query. 8. **Review and Optimize**: Check the query for correctness, efficiency, and adherence to best practices. Ensure it precisely answers the user's request. **User Request**: Write a SQL query to select all products with a price greater than 50, ordered by price in descending order. The table name is `products` and it has columns `product_id`, `product_name`, and `price`.
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt leverages chain-of-thought by breaking down the complex task of 'writing a SQL query' into smaller, manageable steps. This guides the model through the logical process a human SQL developer would follow, ensuring all crucial aspects (joins, filters, ordering, etc.) are considered. It also clearly defines the model's persona ('highly skilled SQL query generator') and provides explicit instructions for handling the input. This structured approach reduces ambiguity, improves accuracy, and makes the model's reasoning more transparent. The naive prompt offers no guidance, leaving the model to infer the best approach, which can lead to less precise or incomplete queries, especially for complex requests.

0%
Token Efficiency Gain
The 'vibe_prompt' is very short and lacks any specific instructions.
The 'optimized_prompt' provides a detailed, step-by-step process for generating a SQL query.
The optimized prompt explicitly defines an 'AI persona' for the task.

Ready to stop burning tokens?

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

Optimize My Prompts