AgentSkillsCN

Sql

SQL

SKILL.md

SQL Skills

Comprehensive SQL skill suite covering query writing, optimization, debugging, analytics, and cross-dialect translation.

Overview

This skill provides guidance for all SQL-related tasks across major database systems (PostgreSQL, MySQL, SQL Server, Oracle, SQLite). Use when writing queries, optimizing performance, debugging issues, or translating between dialects.

Trigger Conditions

Activate this skill when the user:

  • Asks to write, create, or generate SQL queries
  • Needs help with SELECT, INSERT, UPDATE, DELETE, or DDL statements
  • Wants to optimize slow queries or improve database performance
  • Has SQL errors or unexpected query results
  • Needs to understand or explain complex queries
  • Wants to translate SQL between database dialects
  • Requires window functions, CTEs, or advanced SQL features
  • Needs analytics, reporting, or data quality queries

Sub-Skills Directory

SkillPurposeWhen to Use
query-writerWrite SQL queries from requirementsCreating new queries from scratch
syntax-checkerValidate SQL syntax and structureChecking queries for errors before execution
query-explainerExplain complex queries in plain languageUnderstanding existing SQL code
optimizerImprove query performanceSlow queries, execution plan analysis
debuggerDebug failing or incorrect queriesError messages, wrong results
window-functionsAdvanced window/analytic functionsRankings, running totals, comparisons
performance-tuningDatabase-level performanceIndexing, query profiling, bottlenecks
analytics-sqlAnalytical and aggregation queriesReports, metrics, OLAP operations
dialect-translatorConvert between SQL dialectsMigrating queries between databases
data-qualityData validation and integrityNULL handling, constraints, cleansing
reportingBusiness reporting queriesKPIs, dashboards, scheduled reports

Skill Selection Logic

code
User Request -> Analyze Intent -> Select Appropriate Sub-Skill

"Write a query to..." -> query-writer
"Check this SQL..." -> syntax-checker
"Explain this query..." -> query-explainer
"This query is slow..." -> optimizer or performance-tuning
"Why is this failing..." -> debugger
"I need a ranking..." -> window-functions
"Convert this to PostgreSQL..." -> dialect-translator
"Check for duplicate data..." -> data-quality
"Create a monthly report..." -> reporting
"Calculate running total..." -> window-functions or analytics-sql

Supported Database Dialects

DialectVersion SupportKey Differences
PostgreSQL12+Rich functions, JSONB, arrays, CTEs
MySQL8.0+AUTO_INCREMENT, backticks, LIMIT syntax
SQL Server2016+TOP, IDENTITY, T-SQL extensions
Oracle19c+ROWNUM, sequences, PL/SQL
SQLite3.35+Lightweight, limited types, file-based

Query Structure Reference

SELECT Statement Components

sql
SELECT [DISTINCT] columns
FROM table
[JOIN other_table ON condition]
[WHERE filter_condition]
[GROUP BY grouping_columns]
[HAVING aggregate_condition]
[ORDER BY sort_columns]
[LIMIT n OFFSET m]

Common Clause Order

  1. SELECT - What columns to return
  2. FROM - Source table(s)
  3. JOIN - Additional tables with relationships
  4. WHERE - Row-level filtering (before grouping)
  5. GROUP BY - Aggregate grouping
  6. HAVING - Aggregate filtering (after grouping)
  7. ORDER BY - Result sorting
  8. LIMIT/OFFSET - Pagination

Best Practices Summary

Query Writing

  • Use explicit column names, avoid SELECT *
  • Alias tables and complex expressions
  • Use parameterized queries to prevent SQL injection
  • Format queries for readability

Performance

  • Index columns used in WHERE, JOIN, ORDER BY
  • Avoid functions on indexed columns in WHERE
  • Use EXISTS instead of IN for subqueries
  • Limit result sets with appropriate filtering

Maintainability

  • Use CTEs for complex multi-step queries
  • Add meaningful aliases
  • Break complex logic into views or functions
  • Document business logic in comments

Error Handling Pattern

sql
-- PostgreSQL
BEGIN;
    -- your queries here
    IF condition THEN
        RAISE EXCEPTION 'Error message';
    END IF;
COMMIT;

-- SQL Server
BEGIN TRY
    BEGIN TRANSACTION;
    -- your queries here
    COMMIT TRANSACTION;
END TRY
BEGIN CATCH
    ROLLBACK TRANSACTION;
    THROW;
END CATCH;

Quick Reference Cards

Aggregation Functions

FunctionPurpose
COUNT(*)Total rows
COUNT(col)Non-NULL values
SUM(col)Total of values
AVG(col)Average value
MIN(col)Minimum value
MAX(col)Maximum value
GROUP_CONCAT / STRING_AGGConcatenate values

JOIN Types

TypeReturns
INNER JOINMatching rows only
LEFT JOINAll left + matching right
RIGHT JOINAll right + matching left
FULL OUTER JOINAll rows from both
CROSS JOINCartesian product

Common Operators

OperatorUsage
=Exact match
<> or !=Not equal
LIKEPattern match
INValue in list
BETWEENRange inclusive
IS NULLNULL check
EXISTSSubquery has results

Validation Checklist

Before outputting any SQL:

  • Syntax is valid for target dialect
  • Table and column names exist
  • JOIN conditions are complete (no cartesian products)
  • WHERE clause has appropriate filters
  • GROUP BY includes all non-aggregated columns
  • ORDER BY columns are in SELECT or valid
  • Parameters are properly escaped/parameterized
  • Query handles NULL values appropriately