スキル一覧に戻る
srsubramanian

write-sql

by srsubramanian

0🍴 0📅 2026年1月25日
GitHubで見るManusで実行

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

ToolPurpose
sql_queryExecute SQL queries and return results
sql_list_tablesList all tables in the database
sql_describe_tableGet detailed table schema
sql_explain_queryAnalyze query execution plan
sql_validate_queryCheck SQL syntax before running
sql_get_samplesView sample data from tables

Query Writing Guidelines

1. Always Start with Schema Understanding

Before writing queries:

  1. Use sql_list_tables to see available tables
  2. Use sql_describe_table to understand column types and constraints
  3. Use sql_get_samples to 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:

  1. Use sql_validate_query to check syntax
  2. Use sql_explain_query to understand performance
  3. 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 TypeCauseSolution
Syntax errorMissing comma, quote, keywordUse sql_validate_query first
Unknown columnTypo or wrong tableUse sql_describe_table to verify
Type mismatchComparing incompatible typesCast explicitly: CAST(col AS INTEGER)
NULL comparisonUsing = NULLUse IS NULL or IS NOT NULL
Ambiguous columnSame name in multiple tablesUse table alias: t.column_name
Division by zeroDividing by zero valueUse NULLIF(divisor, 0)

Dialect Differences

Be aware that SQL syntax varies by database:

FeatureSQLitePostgreSQLMySQL
String concat|||| or CONCAT()CONCAT()
Current timedatetime('now')NOW()NOW()
Limit + OffsetLIMIT n OFFSET mLIMIT n OFFSET mLIMIT m, n
Boolean type0/1TRUE/FALSE0/1 or TRUE/FALSE
Auto-incrementAUTOINCREMENTSERIALAUTO_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

スコア

総合スコア

50/100

リポジトリの品質指標に基づく評価

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

0/10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

レビュー

💬

レビュー機能は近日公開予定です