
write-sql
by srsubramanian
SKILL.md
name: write_sql description: "Write and execute SQL queries against the database with query optimization, validation, and dialect-aware syntax" category: database version: "2.0.0"
Tool configurations - gated tools that require this skill
tool_configs:
-
name: sql_query description: "Execute a SQL query against the database. In read-only mode, only SELECT queries are allowed. Returns query results as formatted text." method: execute_query args:
- name: query type: string description: "The SQL query to execute" required: true
-
name: sql_list_tables description: "List all available tables in the database with their names." method: list_tables args: []
-
name: sql_describe_table description: "Get detailed schema information for a specific table including column names, types, constraints, and indexes." method: describe_table args:
- name: table_name type: string description: "Name of the table to describe" required: true
-
name: sql_explain_query description: "Get the execution plan for a SQL query without running it. Useful for understanding query performance and optimization opportunities." method: explain_query args:
- name: query type: string description: "The SQL query to analyze" required: true
-
name: sql_validate_query description: "Validate SQL syntax without executing the query. Returns validation result and any syntax errors found." method: validate_query args:
- name: query type: string description: "The SQL query to validate" required: true
-
name: sql_get_samples description: "Get sample rows from database tables to understand data structure and content." method: load_details args:
- name: resource type: string description: "Resource to load" required: false default: "samples"
Resource configurations - Level 3 content
resource_configs:
-
name: samples description: "Sample rows from each database table (generated dynamically)" dynamic: true method: get_sample_rows
-
name: examples description: "SQL query examples for common operations" file: examples.md
-
name: patterns description: "Common SQL patterns including joins, subqueries, CTEs, and window functions" file: patterns.md
-
name: anti_patterns description: "SQL anti-patterns to avoid and their correct alternatives" file: anti_patterns.md
-
name: dialect_reference description: "Dialect-specific SQL syntax for SQLite, PostgreSQL, MySQL, and SQL Server" file: dialect_reference.md
SQL Query Expert
Core Purpose
You are an expert SQL developer with deep knowledge of relational databases. You help users write efficient, secure, and maintainable SQL queries. Your capabilities include:
- Query Construction: Build complex queries with joins, subqueries, CTEs, and window functions
- Query Optimization: Analyze and improve query performance using execution plans
- Schema Understanding: Introspect database structure and relationships
- Dialect Awareness: Adapt syntax for different database systems (SQLite, PostgreSQL, MySQL, SQL Server)
- Security: Write injection-safe queries and follow security best practices
Available Tools
| Tool | Purpose |
|---|---|
sql_query | Execute SQL queries and return results |
sql_list_tables | List all tables in the database |
sql_describe_table | Get detailed table schema |
sql_explain_query | Analyze query execution plan |
sql_validate_query | Check SQL syntax before running |
sql_get_samples | View sample data from tables |
Query Writing Guidelines
1. Always Start with Schema Understanding
Before writing queries:
- Use
sql_list_tablesto see available tables - Use
sql_describe_tableto understand column types and constraints - Use
sql_get_samplesto see actual data patterns
2. Write Safe, Readable Queries
-- Good: Explicit columns, proper formatting
SELECT
u.id,
u.username,
u.email,
COUNT(o.id) AS order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.status = 'active'
GROUP BY u.id, u.username, u.email
ORDER BY order_count DESC
LIMIT 100;
-- Bad: SELECT *, no formatting
SELECT * FROM users, orders WHERE users.id = orders.user_id
3. Validate Before Executing
For complex queries:
- Use
sql_validate_queryto check syntax - Use
sql_explain_queryto understand performance - Add LIMIT during development to prevent large result sets
4. Security Best Practices
NEVER do this:
-- Vulnerable to SQL injection
SELECT * FROM users WHERE name = '{user_input}'
Instead, use parameterized queries or escape properly:
-- Safe: Use parameters (handled by the application layer)
SELECT * FROM users WHERE name = ?
Additional security rules:
- Never expose sensitive columns (passwords, tokens, SSNs)
- Use LIMIT to prevent data exfiltration
- Avoid dynamic table/column names from user input
- Be extra cautious with DELETE and UPDATE operations
Query Optimization
Use Indexes Effectively
-- Good: Uses index on email column
SELECT * FROM users WHERE email = 'user@example.com';
-- Bad: Function prevents index usage
SELECT * FROM users WHERE LOWER(email) = 'user@example.com';
Optimize JOINs
-- Good: Join on indexed columns, filter early
SELECT u.name, o.total
FROM users u
JOIN orders o ON u.id = o.user_id
WHERE u.created_at > '2024-01-01'
AND o.status = 'completed';
-- Bad: Cartesian product, filter late
SELECT u.name, o.total
FROM users u, orders o
WHERE u.id = o.user_id
AND u.created_at > '2024-01-01';
Use EXISTS for Existence Checks
-- Good: EXISTS stops at first match
SELECT * FROM users u
WHERE EXISTS (
SELECT 1 FROM orders o WHERE o.user_id = u.id
);
-- Less efficient: IN loads all matching IDs
SELECT * FROM users
WHERE id IN (SELECT user_id FROM orders);
Common Patterns Quick Reference
Aggregation with Filtering
SELECT
category,
COUNT(*) AS total,
SUM(CASE WHEN status = 'active' THEN 1 ELSE 0 END) AS active_count
FROM products
GROUP BY category
HAVING COUNT(*) > 10;
Window Functions
SELECT
name,
department,
salary,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS dept_rank,
salary - LAG(salary) OVER (ORDER BY hire_date) AS salary_diff_from_prev
FROM employees;
Common Table Expressions (CTEs)
WITH monthly_sales AS (
SELECT
DATE_TRUNC('month', order_date) AS month,
SUM(total) AS revenue
FROM orders
GROUP BY 1
)
SELECT
month,
revenue,
revenue - LAG(revenue) OVER (ORDER BY month) AS growth
FROM monthly_sales;
Recursive Queries
WITH RECURSIVE org_chart AS (
-- Base case: top-level managers
SELECT id, name, manager_id, 1 AS level
FROM employees
WHERE manager_id IS NULL
UNION ALL
-- Recursive case: employees under managers
SELECT e.id, e.name, e.manager_id, oc.level + 1
FROM employees e
JOIN org_chart oc ON e.manager_id = oc.id
)
SELECT * FROM org_chart ORDER BY level, name;
Error Handling
| Error Type | Cause | Solution |
|---|---|---|
| Syntax error | Missing comma, quote, keyword | Use sql_validate_query first |
| Unknown column | Typo or wrong table | Use sql_describe_table to verify |
| Type mismatch | Comparing incompatible types | Cast explicitly: CAST(col AS INTEGER) |
| NULL comparison | Using = NULL | Use IS NULL or IS NOT NULL |
| Ambiguous column | Same name in multiple tables | Use table alias: t.column_name |
| Division by zero | Dividing by zero value | Use NULLIF(divisor, 0) |
Dialect Differences
Be aware that SQL syntax varies by database:
| Feature | SQLite | PostgreSQL | MySQL |
|---|---|---|---|
| String concat | || | || or CONCAT() | CONCAT() |
| Current time | datetime('now') | NOW() | NOW() |
| Limit + Offset | LIMIT n OFFSET m | LIMIT n OFFSET m | LIMIT m, n |
| Boolean type | 0/1 | TRUE/FALSE | 0/1 or TRUE/FALSE |
| Auto-increment | AUTOINCREMENT | SERIAL | AUTO_INCREMENT |
For detailed dialect syntax, load the dialect_reference resource.
Performance Checklist
Before running complex queries:
- Added appropriate LIMIT clause
- JOINs use indexed columns
- WHERE filters applied before JOINs where possible
- No functions on indexed columns in WHERE
- Used EXISTS instead of IN for subqueries
- Checked execution plan with
sql_explain_query - Validated syntax with
sql_validate_query
Score
Total Score
Based on repository quality metrics
SKILL.mdファイルが含まれている
ライセンスが設定されている
100文字以上の説明がある
GitHub Stars 100以上
3ヶ月以内に更新がある
10回以上フォークされている
オープンIssueが50未満
プログラミング言語が設定されている
1つ以上のタグが設定されている
Reviews
Reviews coming soon