Skills DirectorySkills Directory
SkillsLearnSecurityCategoriesDocsBlogPro
Sign InSubmit Skill
Skills Directory

Security-tested agent skills for Claude, coding agents, and AI workflows.

Directory

  • Browse Skills
  • All Skills A–Z
  • Claude Skills
  • Claude Code Skills
  • Agent Skills
  • Categories
  • Authors
  • Submit a Skill

Learn

  • Learn Hub
  • Install Claude Skills
  • Write SKILL.md
  • Skills vs MCP
  • Directories Compared

Security

  • Security
  • Methodology
  • Secure Claude Skills
  • Security Badges
  • Chrome Extension
  • Skill Manager

Company

  • About
  • Community
  • Blog
  • API Docs
  • Advertise

2026 Skills Directory. All rights reserved.

ProTermsPrivacyRefunds
Back to skills

Sql Query Builder

ASecurity

当需要把自然语言需求转成正确高效的 SQL、做联表/聚合/窗口查询或排错时使用;触发词:写 SQL、查询、联表、聚合、窗口函数、慢查询。

3 stars
0 votes
0 copies
1 views
Added 9/19/2026
ai-agentssqldatabase

Works with

cursorcli

Security Analysis

A100/100

Scanned 9/19/2026

$npx -y skills add findscripter/everything-skills --skill sql-query-builder --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Sql Query Builder?

Add the live security badge to your README — it updates automatically with every re-scan.

Security grade badge for Sql Query Builder
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/findscripter-sql-query-builder/badge)](https://www.skillsdirectory.com/skills/findscripter-sql-query-builder)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
Files
SKILL.md
---
name: sql-query-builder
title: SQL 查询构建
description: 当需要把自然语言需求转成正确高效的 SQL、做联表/聚合/窗口查询或排错时使用;触发词:写 SQL、查询、联表、聚合、窗口函数、慢查询。
domain: 数据/sql
tags: [sql, database, query]
level: 进阶
status: stable
version: 0.1.0
agents: [claude-code, codex, cursor, gemini-cli]
tools: [sql]
requires: []
related: [erd-schema-designer, postgresql-optimization, dbt-transformation-modeler, snowflake-development]
combines_with: [postgresql-optimization, kpi-dashboard-design, dbt-transformation-modeler]
license: CC-BY-SA-4.0
---
## 何时使用

- 把自然语言需求翻译成可执行 SQL:筛选、联表、聚合、分组、排序、分页。
- 编写多表 JOIN、窗口函数(排名/累计/同环比/去重取最新)、子查询/CTE。
- 排查错误 SQL 或慢查询:定位结果不对、性能瓶颈、索引未命中。

不该用的边界:
- 数据已在本地 CSV/脏数据、需清洗去重而非查库 → 用 csv-data-cleaner。
- 表结构未知且无法查到 schema → 先要 DDL 或 `information_schema`,不要凭空猜列名。
- 涉及写操作(INSERT/UPDATE/DELETE/DDL)批量改库 → 本技能只产出,执行前须用户确认,不自动跑。

## 步骤 / 指令

```
1. 确认方言与 schema
   - 方言:postgres | mysql | sqlite | sqlserver | bigquery(默认 postgres)
   - 表/列:要到 DDL 或读 information_schema.columns;缺失则停下追问,不臆造列名
2. 拆解需求 → 映射子句
   - 输出哪些列/指标         → SELECT
   - 涉及哪些表、连接键、连接类型 → FROM / JOIN(默认 INNER;要保留左表全集用 LEFT)
   - 过滤条件(行级,聚合前)  → WHERE
   - 分组维度               → GROUP BY
   - 过滤条件(聚合后)      → HAVING
   - 排序、Top-N / 分页      → ORDER BY / LIMIT OFFSET
3. 需要"组内排名/累计/取最新一条"→ 用窗口函数,不要相关子查询
   - ROW_NUMBER()/RANK()/SUM() OVER (PARTITION BY ... ORDER BY ...)
   - 去重取最新:子查询里打 ROW_NUMBER 后外层 WHERE rn=1
4. 嵌套逻辑 → 用 CTE(WITH)拆分,每个 CTE 单一职责,自顶向下可读
5. 自检(产出前逐条过)
   - JOIN 是否漏写 ON / 笛卡尔积?多对多是否导致计数翻倍?
   - SELECT 非聚合列是否都在 GROUP BY?
   - NULL:外连接后的 NULL、NOT IN 含 NULL 陷阱、COUNT(col) 忽略 NULL
   - 聚合条件错放 WHERE(应 HAVING)或反之?
   - 分页有 ORDER BY 保证稳定顺序?
6. 慢查询排查
   - 跑 EXPLAIN / EXPLAIN ANALYZE,看是否 Seq Scan / 全表扫描、行数估算偏差
   - 检查 WHERE/JOIN 列是否有索引;避免在索引列上套函数(如 DATE(col))导致失效
   - SELECT * → 改为只取所需列;用 EXISTS 替代低效 IN 子查询
```

## 示例

需求:"按部门统计 2024 年在职员工平均薪资,只看人数≥5 的部门,从高到低排。"

```sql
-- postgres
SELECT d.name AS dept,
       COUNT(*)            AS headcount,
       ROUND(AVG(e.salary)) AS avg_salary
FROM employees e
JOIN departments d ON d.id = e.dept_id
WHERE e.status = 'active'
  AND e.hire_date < DATE '2025-01-01'
GROUP BY d.name
HAVING COUNT(*) >= 5
ORDER BY avg_salary DESC;
```

窗口函数 —— 每个客户的最近一笔订单:

```sql
WITH ranked AS (
  SELECT o.*,
         ROW_NUMBER() OVER (PARTITION BY customer_id
                            ORDER BY created_at DESC) AS rn
  FROM orders o
)
SELECT customer_id, id AS order_id, amount, created_at
FROM ranked
WHERE rn = 1;
```

给 Agent 的提示词模板:

```
方言=postgres。表 orders(id,customer_id,amount,created_at,status)。
需求:<自然语言>。
要求:用 CTE 拆分;只取必要列;产出后附 EXPLAIN 关注点。
```

## 注意事项

- 先确认方言:LIMIT/OFFSET vs TOP、字符串拼接、日期函数、布尔类型各方言不同。
- 列名/表名不确定就追问,绝不编造;带保留字或大小写敏感时加引号。
- 多对多 JOIN 会放大聚合值,必要时先在子查询里去重再聚合。
- `NOT IN (子查询)` 当子查询含 NULL 时整体返回空集,改用 `NOT EXISTS`。
- 浮点除法注意整数除零与精度,按需 CAST;金额避免用 FLOAT。
- 大表分页用 keyset 分页(WHERE id > last_id)替代大 OFFSET。
- 仅产出 SELECT 查询;写操作必须用户显式确认且给出影响行数估计后才执行。

## 互见

- requires:无。
- related:无。
- combines_with:csv-data-cleaner —— 查询结果导出后或需先清洗本地数据时衔接。

Attribution

findscripterfindscripter
View sourceSee grades on GitHubMore from findscripter →
SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments (0)

No comments yet. Be the first to comment!

SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

Related Skills

Caveman

Terse caveman voice: answer first, fluff gone, every technical fact kept. Use for /caveman, "caveman mode", "talk like caveman", "be brief", "less tokens". Stays on until "stop caveman" or "normal mode".

1100021 votes

Hyperplan

Adversarial multi-agent planning skill. Self-orchestrates 5 hostile category members (unspecified-low, unspecified-high, deep, ultrabrain, artistry) via team-mode for ruthless cross-critique debate, distills only the defensible insights, then MANDATORILY hands the distilled insight bundle to the `plan` agent for executable plan formalization. Use when planning needs maximum rigor and surfacing of weak assumptions, blind spots, and over-engineering. Triggers: 'hyperplan', 'hpp', '/hyperplan', ...

698621 votes

Writing Skills

Create and manage Claude Code skills in HASH repository following Anthropic best practices. Use when creating new skills, modifying skill-rules.json, understanding trigger patterns, working with hooks, debugging skill activation, or implementing progressive disclosure. Covers skill structure, YAML frontmatter, trigger types (keywords, intent patterns), UserPromptSubmit hook, and the 500-line rule. Includes validation and debugging with SKILL_DEBUG. Examples include rust-error-stack, cargo-dep...

3931 votes

Mcp Code Execution

Routes multi-tool workflows through MCP servers for large datasets and pipelines. Use when Bash tool overhead is limiting throughput on data-heavy tasks.

3421 votes

catchup

Recovers the conversation and failed tool calls of a previous Codex, Amp, Claude Code, Antigravity, Cline, Copilot CLI, Cursor, DeepSeek Harness, Grok Build, Kimi, OpenCode, Pi Agent, or ZCode session. Use when the user says "catch up", "what did the last session do", "get me up to speed", "I switched agents", asks to recover/summarize a previous session before continuing, or asks to diagnose or report a catchup failure. Do NOT use for the current conversation, git history, or any non-agent log.

741 votes
View all in ai-agents →