Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Write SQL query
on Gemini 1.5 Pro

Stop guessing. See how professional prompt engineering transforms Gemini 1.5 Pro's output for specific technical tasks.

The "Vibe" Prompt

"Hey Gemini, can you write some SQL for me? I need to select some data from a table."
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert SQL query generator. Your task is to write a SQL SELECT query based on the provided requirements. Here's a breakdown of the thought process you should follow: 1. **Understand the Goal:** Carefully read the request to identify the core objective (what data needs to be retrieved). 2. **Identify Tables/Views:** Determine which tables or views are relevant to fulfill the request. If not explicitly provided, assume a common table name based on the context. 3. **Identify Columns:** Pinpoint the specific columns that need to be selected. 4. **Determine Filters/Conditions (WHERE clause):** Look for any criteria that limit the rows to be returned. 5. **Identify Joins (if needed):** If data from multiple tables is required, determine the join type and conditions. 6. **Identify Aggregations (if needed):** If sums, counts, averages, etc., are requested, plan for GROUP BY and aggregate functions. 7. **Identify Sorting (ORDER BY clause):** If a specific order is required, note the column(s) and direction. 8. **Identify Limit/Offset (if needed):** If only a subset of results is required (e.g., top 10), plan for LIMIT. 9. **Construct the Query:** Assemble the SQL query following standard syntax, ensuring correct column aliases, table aliases, and proper indentation for readability. **User Request:** [Placeholder for User's Specific SQL Request] **Begin SQL Query Generation**
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt drastically improves performance by providing a clear, step-by-step chain-of-thought process. It forces the model to decompose the problem, ensuring all critical aspects of SQL query generation (tables, columns, filters, joins, aggregations, sorting, limits) are considered explicitly. The 'vibe_prompt' is too vague, offering no guidance and likely leading to generic or incorrect outputs, requiring multiple follow-up prompts. The structured approach ensures more accurate, complete, and relevant SQL queries on the first attempt.

0%
Token Efficiency Gain
The optimized_prompt must contain explicit instructions for a chain-of-thought process.
The optimized_prompt must mention SQL-specific clauses like 'WHERE', 'JOIN', 'GROUP BY', 'ORDER BY', 'LIMIT'.
The vibe_prompt must be short and unspecific.

How We Validate This Prompt

Every optimized prompt for Write SQL query on Gemini 1.5 Pro is scored against a fixed set of evaluation assertions. A revision ships only when it passes all of them, so the 0% token reduction never comes at the cost of output quality.

  • The optimized_prompt must contain explicit instructions for a chain-of-thought process.
  • The optimized_prompt must mention SQL-specific clauses like 'WHERE', 'JOIN', 'GROUP BY', 'ORDER BY', 'LIMIT'.
  • The vibe_prompt must be short and unspecific.
  • The why_it_works explanation must clearly articulate the benefits of the structured approach over the vague approach.
  • The token_savings_pct will be null/zero as the optimized prompt is longer but leads to fewer conversational turns and higher quality in a real-world scenario.

Related Optimizations

Optimize your own Write SQL query prompt

Run any prompt through the same optimizer that produced this Gemini 1.5 Pro guide — clarity, structure, and token efficiency in one pass.

Open the Optimizer