Works with the AI Agents you already use

    SQL Review: Catch Wrong Numbers, Slow Queries & Injection

    by Aryan Sharan

    1

    It focuses on correctness first, catching the quiet bugs that create wrong numbers, like accidental duplicates after a join or dates that miss the last day of the month.

    $4.99

    Secure checkout via Stripe

    Money-back guarantee

    Converts to your local currency at checkout

    Security scanned

    What it does

    • Identifies logic bugs that cause silent errors like double-counting or missing rows
    • Proposes specific index changes or rewrites to speed up slow queries with evidence
    • Checks for security risks like SQL injection in your application code
    • Flags dangerous database changes that might lock up a busy production table
    • Verifies that suggested fixes return the exact same data as your original query
    • Tailors every tip to your specific database type like Postgres, MySQL, or SQL Server

    Who it's for

    Data analysts and report builders

    Stop sending reports with wrong numbers caused by hidden duplicates or date calculation errors.

    Try asking: “Check if this dashboard query is double-counting sales when it joins the refunds table.”

    Founders and technical leads reviewing code

    Review database changes and new queries for security risks or slow-downs before they hit production.

    Try asking: “Review these new dbt models and tell me if they are safe to ship to the warehouse.”

    Developers using databases like PostgreSQL or MySQL

    Find out exactly why your query is slow and get a verified, faster version that works for your database.

    Try asking: “This SQL query takes 10 seconds to run. Look at this plan and tell me how to make it faster.”

    SQL Review: Catch Wrong Numbers, Slow Queries & Injection

    SQL Review: Catch Wrong Numbers, Slow Queries & Injection

    Example session with this skill installed

    Our January dashboard says North did $1,120 and South $50, but finance says the whole month was $600. Can you review this Postgres query?

    SELECT c.region,
           SUM(o.amount)                              AS revenue,
           COUNT(o.id)                                AS orders,
           COUNT(r.customer_id) / COUNT(o.id) * 100   AS refund_rate_pct
    FROM customers c
    LEFT JOIN orders o       ON o.customer_id = c.id
    LEFT JOIN order_items i  ON i.order_id = o.id
    LEFT JOIN refunds r      ON r.customer_id = c.id
    WHERE o.created_at BETWEEN '2026-01-01' AND '2026-01-31'
    GROUP BY c.region
    ORDER BY revenue DESC;
    
    • Read your context and instructions
    • Compiled the sql review

    SQL review: January dashboard query

    Verdict: Do not ship
    Dialect: PostgreSQL 16 (stated by user)
    What it does: Revenue, orders and a refund rate per region for January 2026. Intended grain: one row per region.
    Assumptions: orders.id is unique; order_items and refunds have many rows per order / customer (checked: up to 3 items per order, 2 refunds per customer).

    Findings

    #SeverityConfidenceAreaFinding
    1BlockerProvenCorrectnessJoins to order_items and refunds repeat each order: revenue and order counts inflated
    2BlockerProvenCorrectnessBETWEEN ... '2026-01-31' stops at midnight and drops almost all of 31 January
    3MajorProvenCorrectnessWHERE on the LEFT JOINed orders table removes regions with no orders
    4MajorProvenCorrectnessInteger division makes the refund rate 0 or 100
    5MinorLikelyDefinition"Refund rate" divides refund rows by order rows; business meaning needs confirming

    1. Join fan-out inflates revenue and orders

    Problem: each order row repeats once per item and once per refund. North shows 1,120.00 from 8 "orders"; the true figure is 400.00 from 3 orders.
    Evidence: fan-out check: 5 January order rows become 9 rows after joining order_items, before refunds multiply them again. SUM(amount) straight from orders = 600.00, matching finance.
    Fix: aggregate orders per customer first; drop the unused order_items join. (DISTINCT would hide the symptom, not fix the grain.)

    2. The month boundary drops 31 January

    '2026-01-31' means midnight at the start of the 31st. South's 150.00 order at 09:00 on the 31st is missing.
    Fix: created_at >= '2026-01-01' AND created_at < '2026-02-01'.

    3. LEFT JOIN turned into INNER JOIN

    Customers without January orders have NULL created_at, so WHERE removes them. The West region disappears instead of showing 0.

    4. Integer division

    Both sides are integers, so PostgreSQL truncates. Fix: 100.0 * num / NULLIF(den, 0).

    Corrected query

    WITH jan_orders AS (
        SELECT o.customer_id, SUM(o.amount) AS revenue, COUNT(*) AS orders
        FROM orders o
        WHERE o.created_at >= TIMESTAMP '2026-01-01'
          AND o.created_at <  TIMESTAMP '2026-02-01'
        GROUP BY o.customer_id
    ),
    refunders AS (
        SELECT DISTINCT r.customer_id FROM refunds r WHERE r.customer_id IS NOT NULL
    )
    SELECT c.region,
           COALESCE(SUM(j.revenue), 0) AS revenue,
           COALESCE(SUM(j.orders), 0)  AS orders,
           ROUND(100.0 * COUNT(f.customer_id) FILTER (WHERE j.customer_id IS NOT NULL)
                 / NULLIF(COUNT(j.customer_id), 0), 1) AS pct_customers_with_refund
    FROM customers c
    LEFT JOIN jan_orders j ON j.customer_id = c.id
    LEFT JOIN refunders  f ON f.customer_id = c.id
    GROUP BY c.region
    ORDER BY revenue DESC, c.region;
    

    Before vs after (both queries run on the same data)

    RegionRevenue beforeRevenue afterOrders beforeOrders after
    North1,120.00400.0083
    South50.00200.0012
    Westmissing0.00missing0
    Total1,170.00600.0095

    The corrected total matches finance. The rewrite was parsed for PostgreSQL and executed.

    Not checked

    Performance (no plan provided). On a large orders table an index on orders (created_at) may help; confirm with EXPLAIN before adding it.

    Next step: confirm which refund metric the business wants (% of customers with a refund, or refunded amount ÷ revenue) and I'll adjust the query.

    Connects securely to your tools. The creator never sees your data.

    About this skill

    When your database query gives you a number that "feels" wrong, or your app slows to a crawl, finding the cause is frustrating. This skill reviews your SQL queries like a senior database expert. It focuses on correctness first, catching the quiet bugs that create wrong numbers, like accidental duplicates after a join or dates that miss the last day of the month. It supports all major databases, from local ones like SQLite to cloud giants like Snowflake and BigQuery.

    How it works

    1. Paste your SQL query or a file containing database code into the chat.
    2. Tell your AI agent which database you use, like PostgreSQL, MySQL, or BigQuery.
    3. This skill scans the code for logic errors, slow spots, and security holes.
    4. It provides a table of findings from "Blocker" to "Minor" with clear proof for each one.
    5. You get the corrected code and a way to verify the fix actually works.

    What you get: A detailed report showing exactly what is wrong with your SQL and a verified, safer version of the code.

    Cannot promise specific speed-up percentages. Requires you to provide the database type for the most accurate results. Cannot run code on your database without your permission.

    Built for multi-dialect SQL auditing including PostgreSQL, MySQL, SQL Server, Oracle, BigQuery, Snowflake, and more. It utilizes a Python static analysis script (sqlglot-based) to flag candidates for join fan-out, NULL handling errors, and non-sargable predicates. The skill follows a structured multi-pass workflow: Correctness, Performance, Security, and Migrations. It generates small test queries for DuckDB/SQLite to prove findings and includes equivalence checks to ensure rewrites preserve data integrity.

    How to use

    1. 1

      Connect your agent to the Agensi MCP

      Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup

    2. 2

      Sign in to Agensi

      Your agent shows a sign-in link. Sign in and click Allow.

    3. 3

      Ask for what you need

      Your agent loads this skill and uses it on your task. It always asks before buying a skill.

    Reviews

    No reviews yet

    Be one of the first to try it. Every listed skill goes through the trust checks below.

    Security scanned

    Checked by our 8-point scan before listing

    Fresh listing

    Recently published to Agensi

    Money-back guarantee

    Doesn't do what its page promises? You get your money back.

    Frequently Asked Questions

    This skill identifies hidden logic errors that cause reports to show wrong numbers, such as double-counting during joins or missing data at month-end. It also checks for slow performance, security risks like SQL injection, and whether database changes are safe to run on live systems.

    You only need to provide the SQL query or the file containing your database code, along with the name of the database you use. If you are using a tool like dbt or writing code in languages like Python or JavaScript that generates SQL, you can provide those files for review as well.

    You receive a detailed report that categorizes findings from Blocker to Minor, including evidence for each issue. It provides corrected versions of your code and a specific way to verify that the fix works as expected.

    This skill covers all major database engines including PostgreSQL, MySQL, SQL Server, Oracle, and SQLite, as well as cloud platforms like BigQuery, Snowflake, and Redshift. It uses a helper script to verify syntax and check for common risks across these different dialects.

    It is not meant for writing brand-new queries from scratch or for general database administration tasks like server tuning and backups. It also avoids reviewing non-SQL data stores or giving performance guarantees without evidence from a query plan.

    You can ask your agent to audit, debug, optimize, or check a specific query for errors. For example, you might ask why a certain report is returning duplicate numbers or if a new database migration is safe to run.

    New in Data & Databases

    Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for Data & Databases.