Write Query
AnthropicOfficialPublished by Anthropic
- Skill
- Content generator
- Data and AI
- Apache-2.0
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
The write-query skill translates natural language data requirements into structured, performant SQL code. It is designed to handle complex logic, including multi-CTE queries, joins, and aggregations, while adhering to database best practices. The skill supports a wide range of SQL dialects, ensuring that generated queries leverage dialect-specific syntax and performance features like partitioning or clustering. It emphasizes readability, maintainability, and efficiency by guiding the user through schema discovery and providing clear explanations for the generated logic.
What it does
- Translates natural language requests into syntactically correct SQL queries.
- Optimizes queries for specific dialects like Snowflake, BigQuery, Postgres, and more.
- Structures complex logic using readable Common Table Expressions (CTEs).
- Applies performance best practices such as early filtering and avoiding SELECT *.
- Provides explanations for query logic and performance considerations.
- Offers to execute queries directly if a data warehouse connection is available.
When to use it
- When you need to convert a business question into a functional SQL query.
- When building complex reports requiring multiple joins and aggregations.
- When optimizing existing queries for large, partitioned datasets.
- When you need to ensure your SQL follows industry-standard best practices.
- When working across different SQL dialects and needing specific syntax support.
Advantages
- Reduces manual coding time for complex analytical queries.
- Ensures consistent, readable, and well-documented SQL code.
- Promotes performance-oriented coding habits like partition filtering.
- Adapts to specific database engine requirements automatically.
Usage examples
- “/write-query Count of orders by status for the last 30 days”
- Generates a clean SQL query with a WHERE clause filtering for the last 30 days and a GROUP BY statement for order status.
- “/write-query Cohort retention analysis -- group users by their signup month, then show what percentage are still active at 1, 3, 6, and 12 months after signup”
- Produces a multi-CTE query that calculates cohort groups and calculates retention percentages using window functions or joins.
- “/write-query Find the top 100 users by event count in the last 7 days from our 500M row events table”
- Provides an optimized query that utilizes partition filters and efficient aggregation to handle large-scale data.
Install
Download the skill as a ZIP:
https://autogineer.com/agent-skills/anthropics/write-query/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip write-query.zip -d .agents/skills
Documentation by Anthropic
/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 columnsFilter 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 setsUse 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 daysComplex 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 signupPerformance-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
Files
- ATTRIBUTION.mdAdded by AutoGineer902 B
- LICENSELicence file added by AutoGineer11.1 KiB
- SKILL.md4.6 KiB
Requirements and compatibility
Written for: Claude Code, Claude apps
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Claude Code frontmatter, Needs an MCP server
Security
No issues found by our static scan· No issues found
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Published by
- Anthropic
- Publisher
- Anthropic (Official)
- Source
- anthropics/knowledge-work-plugins · data/skills/write-query · commit e050c2e
- Licence
- Apache License 2.0 (redistribution allowed)
- LICENSE (added by AutoGineer from data/LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by Anthropic or Anthropic.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download write-query.zip