Claude Skill

sql-query-reviewer

Reviews SQL queries for correctness, performance and safety problems such as SELECT star, missing indexes, unbounded updates, implicit casts and injection risks. Use when the user asks to optimize a query, explain why a query is slow, or check SQL before it runs on production dat

LLM Mart · 0 points · 1 views 0 listing impressions 0 install-command copies
Download lethe044-agentskill-kit-skills_sql-query-reviewer-219c6e5.zip · 1 KB

Install

skills CLI npx skills add https://github.com/Lethe044/agentskill-kit/tree/main/skills/sql-query-reviewer
Claude Code claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install lethe044-agentskill-kit@llmmart
Git git clone https://github.com/Lethe044/agentskill-kit.git

The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole lethe044/agentskill-kit collection as a plugin from our marketplace. Git is the plain clone.

Skill manifest

SQL query reviewer

When to use

  • A query is slow or times out.
  • The user is about to run an UPDATE or DELETE on real data.
  • SQL is built from user input in application code.

Checklist

Safety first

  1. UPDATE or DELETE without a WHERE clause. Ask for confirmation and suggest running a SELECT COUNT(*) with the same condition first.
  2. Wrap destructive statements in a transaction and show how to roll back.
  3. String concatenation of user input into SQL. Replace it with bound parameters.

Correctness

  1. NULL comparisons written with = instead of IS NULL.
  2. LEFT JOIN followed by a WHERE condition on the right table, which silently turns it into an inner join.
  3. GROUP BY that does not match the selected columns, and COUNT(column) used where COUNT(*) was meant.
  4. NOT IN with a subquery that can return NULL. Prefer NOT EXISTS.

Performance

  1. SELECT * on wide tables. List the columns that are used.
  2. Functions on indexed columns in WHERE (WHERE LOWER(email) = ...). Suggest an expression index or a different predicate.
  3. Leading wildcards (LIKE '%term') that cannot use an index.
  4. Missing indexes on join and filter columns. Ask for EXPLAIN output instead of guessing.
  5. OFFSET pagination on large tables. Suggest keyset pagination.
  6. ORDER BY without LIMIT on large results, and DISTINCT used to hide a bad join.

Steps

  1. Identify the database engine. Advice differs between PostgreSQL, MySQL, SQLite and SQL Server.
  2. Go through the checklist and list only real findings, most serious first.
  3. Show the rewritten query next to each finding.
  4. If performance is the question and no EXPLAIN output was given, ask for it and say what to look for (sequential scans, row estimates far from actual rows).

Example

Input: DELETE FROM sessions WHERE created_at < now() - interval '30 days'

Review: valid, but deletes an unknown number of rows. Run SELECT COUNT(*) with the same condition first, delete in batches if the count is large, and keep it inside a transaction until the result looks right.

Errors

If the schema is unknown, say which assumptions the review makes about indexes and types.

Files (agentskill-kit)
  • SKILL.md 2.6 KB
    ---
    name: sql-query-reviewer
    description: Reviews SQL queries for correctness, performance and safety problems such as SELECT star, missing indexes, unbounded updates, implicit casts and injection risks. Use when the user asks to optimize a query, explain why a query is slow, or check SQL before it runs on production data.
    license: MIT
    metadata:
      version: 1.0.0
      author: Lethe044
      tags: [sql, database, performance, review]
    ---
    
    # SQL query reviewer
    
    ## When to use
    
    - A query is slow or times out.
    - The user is about to run an `UPDATE` or `DELETE` on real data.
    - SQL is built from user input in application code.
    
    ## Checklist
    
    **Safety first**
    
    1. `UPDATE` or `DELETE` without a `WHERE` clause. Ask for confirmation and suggest running a `SELECT COUNT(*)` with the same condition first.
    2. Wrap destructive statements in a transaction and show how to roll back.
    3. String concatenation of user input into SQL. Replace it with bound parameters.
    
    **Correctness**
    
    4. `NULL` comparisons written with `=` instead of `IS NULL`.
    5. `LEFT JOIN` followed by a `WHERE` condition on the right table, which silently turns it into an inner join.
    6. `GROUP BY` that does not match the selected columns, and `COUNT(column)` used where `COUNT(*)` was meant.
    7. `NOT IN` with a subquery that can return `NULL`. Prefer `NOT EXISTS`.
    
    **Performance**
    
    8. `SELECT *` on wide tables. List the columns that are used.
    9. Functions on indexed columns in `WHERE` (`WHERE LOWER(email) = ...`). Suggest an expression index or a different predicate.
    10. Leading wildcards (`LIKE '%term'`) that cannot use an index.
    11. Missing indexes on join and filter columns. Ask for `EXPLAIN` output instead of guessing.
    12. `OFFSET` pagination on large tables. Suggest keyset pagination.
    13. `ORDER BY` without `LIMIT` on large results, and `DISTINCT` used to hide a bad join.
    
    ## Steps
    
    1. Identify the database engine. Advice differs between PostgreSQL, MySQL, SQLite and SQL Server.
    2. Go through the checklist and list only real findings, most serious first.
    3. Show the rewritten query next to each finding.
    4. If performance is the question and no `EXPLAIN` output was given, ask for it and say what to look for (sequential scans, row estimates far from actual rows).
    
    ## Example
    
    Input: `DELETE FROM sessions WHERE created_at < now() - interval '30 days'`
    
    Review: valid, but deletes an unknown number of rows. Run `SELECT COUNT(*)` with the same condition first, delete
    in batches if the count is large, and keep it inside a transaction until the result looks right.
    
    ## Errors
    
    If the schema is unknown, say which assumptions the review makes about indexes and types.
    

Comments (0)

Sign in to join the conversation.

No comments yet.

Reviews (0)

No reviews yet.