Write SQL Queries
If you see unfamiliar placeholders or need to check which tools are connected, please ask about available integrations.
Write a SQL query from a natural language description, optimized for your specific SQL dialect and following best practices.
Usage
You can ask to write a SQL query (e.g., "Write a query to count orders" or "Get the top 10 users").
Arguments
description — Description of what data you need and the business logic
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 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
EXISTS over IN for 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
- 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.
Tips
- Mention your SQL dialect upfront to get the right syntax immediately
- If you know the table names, include them -- otherwise I 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
1---2name: data-write-query3description: Write optimized SQL for your dialect with best practices4---56# Write SQL Queries78> If you see unfamiliar placeholders or need to check which tools are connected, please ask about available integrations.910Write a SQL query from a natural language description, optimized for your specific SQL dialect and following best practices.1112## Usage1314You can ask to write a SQL query (e.g., "Write a query to count orders" or "Get the top 10 users").1516### Arguments1718- `description` — Description of what data you need and the business logic1920### Workflow2122### 1. Understand the Request2324Parse the user's description to identify:2526- **Output columns**: What fields should the result include?27- **Filters**: What conditions limit the data (time ranges, segments, statuses)?28- **Aggregations**: Are there GROUP BY operations, counts, sums, averages?29- **Joins**: Does this require combining multiple tables?30- **Ordering**: How should results be sorted?31- **Limits**: Is there a top-N or sample requirement?3233### 2. Determine SQL Dialect3435If the user's SQL dialect is not already known, ask which they use:3637- **PostgreSQL** (including Aurora, RDS, Supabase, Neon)38- **Snowflake**39- **BigQuery** (Google Cloud)40- **Redshift** (Amazon)41- **Databricks SQL**42- **MySQL** (including Aurora MySQL, PlanetScale)43- **SQL Server** (Microsoft)44- **DuckDB**45- **SQLite**46- **Other** (ask for specifics)4748Remember the dialect for future queries in the same session.4950### 3. Discover Schema (If Warehouse Connected)5152If a data warehouse is connected:53541. Search for relevant tables based on the user's description552. Inspect column names, types, and relationships563. Check for partitioning or clustering keys that affect performance574. Look for pre-built views or materialized views that might simplify the query5859### 4. Write the Query6061Follow these best practices:6263**Structure:**6465- Use CTEs (WITH clauses) for readability when queries have multiple logical steps66- One CTE per logical transformation or data source67- Name CTEs descriptively (e.g., `daily_signups`, `active_users`, `revenue_by_product`)6869**Performance:**7071- Never use `SELECT *` in production queries -- specify only needed columns72- Filter early (push WHERE clauses as close to the base tables as possible)73- Use partition filters when available (especially date partitions)74- Prefer `EXISTS` over `IN` for subqueries with large result sets75- Use appropriate JOIN types (don't use LEFT JOIN when INNER JOIN is correct)76- Avoid correlated subqueries when a JOIN or window function works77- Be mindful of exploding joins (many-to-many)7879**Readability:**8081- Add comments explaining the "why" for non-obvious logic82- Use consistent indentation and formatting83- Alias tables with meaningful short names (not just `a`, `b`, `c`)84- Put each major clause on its own line8586**Dialect-specific optimizations:**8788- Apply dialect-specific syntax and functions89- Use dialect-appropriate date functions, string functions, and window syntax90- Note any dialect-specific performance features (e.g., Snowflake clustering, BigQuery partitioning)9192### 5. Present the Query9394Provide:95961. **The complete query** in a SQL code block with syntax highlighting972. **Brief explanation** of what each CTE or section does983. **Performance notes** if relevant (expected cost, partition usage, potential bottlenecks)994. **Modification suggestions** -- how to adjust for common variations (different time range, different granularity, additional filters)100101### 6. Offer to Execute102103If 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.104105## Tips106107- Mention your SQL dialect upfront to get the right syntax immediately108- If you know the table names, include them -- otherwise I will help you find them109- Specify if you need the query to be idempotent (safe to re-run) or one-time110- For recurring queries, mention if it should be parameterized for date ranges