Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Write SQL query
on Claude 3.5 Sonnet

Stop guessing. See how professional prompt engineering transforms Claude 3.5 Sonnet's output for specific technical tasks.

The "Vibe" Prompt

"Write a SQL query to get all orders for customer ID 123."
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert SQL query generator. Your task is to write a SQL query to retrieve all orders for a specific customer. <thought> 1. Identify the core request: retrieve orders for a customer. 2. Determine the necessary tables: 'orders' table (assuming it contains customer references). 3. Determine the filtering condition: 'customer_id' = 123. 4. Determine the columns to select: all columns from the 'orders' table (SELECT *). 5. Construct the SQL query using SELECT, FROM, and WHERE clauses. </thought> Write a SQL query to get all columns from the 'orders' table where the 'customer_id' is 123. Ensure the query is valid for a standard SQL database (e.g., PostgreSQL, MySQL).
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt provides clear instructions, defines the persona (expert SQL query generator), and includes a chain-of-thought section. This C-o-T breaks down the problem into logical steps, helping the model understand the exact requirements and thought process, leading to a more accurate and robust SQL query. It guides the model through table identification, filtering, and column selection, reducing ambiguity.

0%
Token Efficiency Gain
The 'optimized_prompt' clearly states the goal of generating an SQL query.
The 'optimized_prompt' uses a chain-of-thought to guide the model's reasoning.
The 'optimized_prompt' specifies the target customer ID (123) and table ('orders').

How We Validate This Prompt

Every optimized prompt for Write SQL query on Claude 3.5 Sonnet 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' clearly states the goal of generating an SQL query.
  • The 'optimized_prompt' uses a chain-of-thought to guide the model's reasoning.
  • The 'optimized_prompt' specifies the target customer ID (123) and table ('orders').
  • The 'optimized_prompt' requests all columns ('SELECT *').
  • The 'vibe_prompt' is significantly shorter and less detailed than the 'optimized_prompt'.

Related Optimizations

Optimize your own Write SQL query prompt

Run any prompt through the same optimizer that produced this Claude 3.5 Sonnet guide — clarity, structure, and token efficiency in one pass.

Open the Optimizer