
Write Optimized SQL
OfficialFreeTransform natural language into efficient SQL queries.
Free · Opens the source repo
What Write Optimized SQL does
The Write Optimized SQL skill is designed to help users convert natural language descriptions into SQL queries tailored to their specific SQL dialect. This tool is particularly useful for data analysts, developers, and anyone who needs to generate complex SQL queries without deep expertise in SQL syntax. It streamlines the process of query creation by understanding user requests and translating them into optimized SQL code that adheres to best practices.
When using this skill, the user simply provides a description of the data they need, and the skill parses this input to identify the necessary output columns, filters, aggregations, joins, and sorting requirements. It also determines the appropriate SQL dialect, ensuring that the generated query is compatible with systems like PostgreSQL, Snowflake, BigQuery, and others. This capability is essential for users who work with diverse data environments and need to switch between different SQL dialects seamlessly.
Moreover, the skill includes advanced features such as schema discovery when connected to a data warehouse, allowing it to suggest relevant tables and optimize queries based on the underlying data structure. It emphasizes performance and readability by promoting best practices, such as using Common Table Expressions (CTEs) for clarity and avoiding inefficient query patterns. This focus on optimization helps users write queries that not only work correctly but also perform efficiently, particularly when dealing with large datasets.
Overall, the Write Optimized SQL skill is an invaluable resource for anyone looking to enhance their SQL query writing capabilities, making it easier to extract insights from data without needing extensive SQL knowledge.
When to use it
Use this tool when you need to quickly generate SQL queries from natural language descriptions, particularly in environments with varying SQL dialects.
When not to use it
This skill may not be suitable for users who require highly customized SQL queries beyond the scope of natural language descriptions or those who prefer manual query writing.
What you can build with it
Simple Data Retrieval
Generate a straightforward SQL query to count orders by status for the last 30 days.
Cohort Analysis
Create a complex SQL query for cohort retention analysis, grouping users by signup month.
Performance Optimization
Write a performance-critical SQL query to find the top users by event count from a large partitioned table.
How to install Write Optimized SQL
View source1. Install with the skills CLI
npx skills add anthropics/knowledge-work-plugins/write-query --agent claude-code2. Or install it manually
Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.
Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs
Inside SKILL.md
Written by anthropics/write-query - Write Optimized SQL
If you see unfamiliar placeholders or need to check which tools are connected, see CONNECTORS.md.
Write a SQL query from a natural language description, optimized for your specific SQL dialect and following best practices.
Usage
/write-query <description of what data you need>
Workflow
1. Understand the Request
Parse the user's description to identify:
- Output columns: What fields should the result include?
- Filters: What conditions limit the data (time ranges, segments, statuses)?
- Aggregations: Are there GROUP BY operations, counts, sums, averages?
- Joins: Does this require combining multiple tables?
- Ordering: How should results be sorted?
- Limits: Is there a top-N or sample requirement?
2. Determine SQL Dialect
If the user's SQL dialect is not already known, ask which they use:
- PostgreSQL (including Aurora, RDS, Supabase, Neon)
- Snowflake
- BigQuery (Google Cloud)
- Redshift (Amazon)
- Databricks SQL
- MySQL (including Aurora MySQL, PlanetScale)
- SQL Server (Microsoft)
- DuckDB
- SQLite
- Other (ask for specifics)
Remember the dialect for future queries in the same session.
3. Discover Schema (If Warehouse Connected)
If a data warehouse MCP server is connected:
- Search for relevant tables based on the user's description
- Inspect column names, types, and relationships
- Check for partitioning or clustering keys that affect performance
- Look for pre-built views or materialized views that might simplify the query
4. Write the Query
Follow these best practices:
Structure:
- Use CTEs (WITH clauses) for readability when queries have multiple logical steps
- One CTE per logical transformation or data source
- Name CTEs descriptively (e.g.,
daily_signups,active_users,revenue_by_product)
Performance:
- Never use
SELECT *in production queries -- specify only needed columns - Filter early (push WHERE clauses as close to the base tables as possible)
- Use partition filters when available (especially date partitions)
- Prefer
EXISTSoverINfor subqueries with large result sets - Use appropriate JOIN types (don't use LEFT JOIN when INNER JOIN is correct)
- Avoid correlated subqueries when a JOIN or window function works
- Be mindful of exploding joins (many-to-many)
Readability:
- Add comments explaining the "why" for non-obvious logic
- Use consistent indentation and formatting
- Alias tables with meaningful short names (not just
a,b,c) - Put each major clause on its own line
Dialect-specific optimizations:
- Apply dialect-specific syntax and functions (see
sql-queriesskill for details) - Use dialect-appropriate date functions, string functions, and window syntax
- Note any dialect-specific performance features (e.g., Snowflake clustering, BigQuery partitioning)
5. Present the Query
Provide:
- The complete query in a SQL code block with syntax highlighting
- Brief explanation of what each CTE or section does
- Performance notes if relevant (expected cost, partition usage, potential bottlenecks)
- Modification suggestions -- how to adjust for common variations (different time range, different granularity, additional filters)
6. Offer to Execute
If a data warehouse is connected, offer to run the query and analyze the results. If the user wants to run it themselves, the query is ready to copy-paste.
Examples
Simple aggregation:
/write-query Count of orders by status for the last 30 days
Complex analysis:
/write-query Cohort retention analysis -- group users by their signup month, then show what percentage are still active (had at least one event) at 1, 3, 6, and 12 months after signup
Performance-critical:
/write-query We have a 500M row events table partitioned by date. Find the top 100 users by event count in the last 7 days with their most recent event type.
Tips
- Mention your SQL dialect upfront to get the right syntax immediately
- If you know the table names, include them -- otherwise Claude will help you find them
- Specify if you need the query to be idempotent (safe to re-run) or one-time
- For recurring queries, mention if it should be parameterized for date ranges
Frequently asked questions about Write Optimized SQL
Similar skills
ClickHouse Logs Queries
Efficiently manage Supabase logs with ClickHouse SQL.
EF Core D2 Database Diagram Generator
Visualize your EF Core models as D2 diagrams effortlessly.
Safe SQL Execution
Ensure secure SQL execution in Supabase applications.
Oracle to PostgreSQL Migration
Identify migration risks between Oracle and PostgreSQL.
SSMA Console
Streamline Oracle to SQL Server migrations with ease.
SQL Performance Optimization
Enhance SQL query efficiency across all databases.
