Best Claude Prompt for “Write a SQL Query

Claude performs best with XML-tagged prompts using <task>, <context>, <requirements>, and <format> blocks. Unlike other models, Claude generalizes well from motivations — explaining WHY you want something, not just WHAT, produces more accurate and nuanced responses. This free template is optimized specifically for Claude, achieving approximately 37% token reduction compared to unoptimized prompts while producing higher quality output.

Optimize Any Prompt Instantly

Stop manually formatting prompts for each AI model. PromptOptimizr auto-restructures your prompts for ChatGPT, Claude, Gemini, Cursor, and Claude Code — with 3 optimization styles.

What Most People Type

Write a SQL query to get [specific data] from my database

Optimized Claude Prompt

<context>
The user has a database and needs to retrieve specific data. This requires constructing a SQL query. The database schema is not provided, so the query will need to be general or assume a common structure.
</context>

<task>
As a SQL expert, build a SQL query to retrieve specific data from a database.
</task>

<requirements>
*   **Clarity and Specificity**: The query should be precise in targeting the requested data.
*   **Database Agnosticism (if applicable)**: If the schema is unknown, aim for standard SQL that is compatible with most major database systems (e.g., PostgreSQL, MySQL, SQL Server).
*   **Efficiency**: The query should be reasonably efficient, avoiding unnecessary operations.
*   **Readability**: Include comments within the SQL code to explain complex parts, making it easier for others to understand.
*   **Placeholder Usage**: Clearly indicate where the user needs to substitute their specific table names, column names, and conditions.
*   **Explanation**: Provide a brief explanation of how the query works, detailing the purpose of each clause (SELECT, FROM, WHERE, etc.). This helps the user understand the logic and adapt it if needed.
</requirements>

<format>
*   Present the SQL query clearly, preferably in a code block.
*   Follow the query with a concise explanation (3-5 sentences) of its components and how it retrieves the specified data.
*   Before finishing, verify your response covers the user's core ask of building a SQL query for specific data.
</format>

Why This Claude Prompt for “Write a SQL Query” Works Better

When you type “Write a SQL query to get [specific data] from my database” into Claude, it lacks structure — Claude has to guess what format you want, how detailed to go, and what to prioritize. The optimized version removes that guesswork.

It wraps the request in XML tags (<task>, <requirements>, <format>) that Claude uses to separate context from instructions. It also explains WHY behind each constraint — Claude produces better results when it understands your reasoning.

This Prompt Optimized for Other AI Models

Each AI model processes instructions differently. See how this same prompt is optimized for:

Frequently Asked Questions

What makes a good Claude prompt for write a sql query?

Claude performs best with XML-tagged prompts using <task>, <context>, <requirements>, and <format> blocks. Unlike other models, Claude generalizes well from motivations — explaining WHY you want something, not just WHAT, produces more accurate and nuanced responses.

Can I use this prompt template for free?

Yes — copy the optimized prompt below and paste it directly into Claude. For unlimited prompt optimization across all 5 AI models, try PromptOptimizr free with 10 prompts per month. You can also use the Chrome extension to optimize prompts directly inside Claude.

Why is this prompt different from a generic one?

Each AI model processes instructions differently. Claude has specific formatting preferences — this prompt is structured to work with those preferences, not against them. The result is better output quality with approximately 37% fewer tokens.

More Claude Prompt Templates

Optimize Prompts Without Leaving Claude

The PromptOptimizr Chrome Extension injects directly into Claude's interface. No tab switching — click the optimize button right inside the chat, pick your style, and get a model-specific prompt instantly. Works inside ChatGPT, Claude, and Gemini.

Get the Free Chrome Extension →