sql result repair agent

    1

    Diagnose and repair logical SQL errors like join fan-out, grain mismatch, and improper NULL handling.

    $5

    Secure checkout via Stripe

    30-day refund guarantee

    Converts to your local currency at checkout

    0 installsSecurity scanned

    Works with the AI tools you already use

    Claude CodeClaude CodeCursorCursorCodex CLICodex CLIGitHub CopilotGitHub CopilotGemini CLIGemini CLI+20 more

    sql result repair agent

    Example session with this skill installed

    My Postgres query for total sales per customer is returning double the actual amounts. It joins 'customers' to 'orders' and 'order_items'. I suspect the joins are duplicating rows.

    • Read your context and instructions
    • Compiled the sql result repair
    • Generated the document

    The query was failing due to a many-to-many fan-out where order_items multiplied the orders grain before aggregation. I have moved the item-level aggregation into a CTE to preserve the order grain.

    SQL Repair Report

    Relational Contract

    • Intended grain: One row per customer_id.
    • Row preservation: All customers must be present, even those with zero orders.
    • Join cardinality: 1:Many (Customer to Orders), 1:Many (Orders to Items).
    • Aggregation: Sum of item_price grouped by customer_id.

    Defect Isolation

    The original query joined orders and order_items directly in the main block. Because a single order has multiple items, the order.total (if used) or the join itself was duplicating the parent record before the final group by, leading to inflated sums.

    Minimal Repair

    • Old: FROM customers c LEFT JOIN orders o ON ... LEFT JOIN order_items i ON ...

    New: Wrap order_items in a subquery or CTE that aggregates to the order_id grain first, then join to customers.

    Verification Plan

    AspectCheckExpected Result
    Graincount(distinct customer_id)Matches count(*) of query
    Fan-outCheck for order_id duplicates0 duplicates
    PreservationCount customers with null ordersShould match source count

    Residual Risk

    Data-dependent risk: If an order exists in orders but has no entries in order_items, the current inner join in the CTE will exclude that order's value.

    • NULL semantics: Assumes item_price is NOT NULL; otherwise, SUM behavior might vary.

    Final SQL (PostgreSQL)

    WITH daily_order_totals AS (
        SELECT 
            order_id, 
            customer_id,
            SUM(price) AS order_total
        FROM order_items
        GROUP BY 1, 2
    )
    SELECT 
        c.customer_name,
        COALESCE(SUM(dot.order_total), 0) AS total_spent
    FROM customers c
    LEFT JOIN daily_order_totals dot ON c.customer_id = dot.customer_id
    GROUP BY c.customer_id, c.customer_name;
    

    Next steps

    1. Run the Verification Plan table queries against your dev environment.
    2. Confirm if customers with empty orders should show as $0.00 or NULL.
    3. Check the EXPLAIN plan to ensure the CTE is being materialized efficiently.

    sql-result-repair-agent.pdf

    PDF · document

    Generated

    Example file from a real run - the skill writes it into your workspace.

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

    What you get

    Fix join fan-out that causes inflated aggregate sums and duplicates.Resolve row preservation issues where LEFT JOINs act like INNER JOINs.Correct window function partitioning and frame boundaries.Standardize NULL handling and COALESCE logic across different SQL dialects.

    About this skill

    The problem

    SQL queries often execute without syntax errors but return logically incorrect data, such as duplicate rows from join fan-out or missing records due to improper NULL handling. Developers waste hours guessing at fixes or using DISTINCT to mask underlying structural flaws.

    What it does

    • Establishes a formal relational contract defining grain, row preservation, and join cardinality.
    • Isolates the specific owning defect by checking join fan-out, window frames, and aggregation scope.
    • Applies the minimal structural change required to satisfy the contract without rewriting the entire query.
    • Generates a targeted verification plan to test the repair against grain and cardinality requirements.
    • Identifies residual risks including data-dependent edge cases and dialect-specific behaviors.

    Frameworks & tools

    Supports all major SQL dialects including PostgreSQL, BigQuery, Snowflake, SQL Server, and MySQL.

    Why this beats prompting it yourself

    Standard LLMs often suggest "lazy" fixes like adding DISTINCT to the outer query, which hides bugs rather than fixing them. This skill enforces a rigorous debugging workflow that prioritizes relational integrity and minimal code churn.

    Use cases

    • Fixing report queries that return inflated totals due to many-to-many join fan-out.
    • Correcting window functions that reset at the wrong partition boundaries.
    • Repairing LEFT JOIN logic where WHERE clauses are accidentally filtering out preserved rows.
    • Migrating logic between dialects where NULL semantics or implicit casting differ.

    Known limitations

    Focuses strictly on logical correctness, not physical performance tuning or indexing. Requires the user to provide or confirm the intended grain of the result set.

    How to install

    Works the same in every agent - Claude, Cursor, Codex, Copilot and 20+ more.

    ~30 seconds
    1. 1

      Download the ZIP

      Free skills download straight away. Paid skills unlock right after purchase.

    2. 2

      Unzip into your skills folder

      Every agent reads skills from one folder on your machine. Drop the unzipped folder in there.

    3. 3

      Ask your agent to use it

      Restart the agent if it was already running. It picks the skill up automatically - no config needed.

    Skills folder by agent

    Click the path to copy it. Create the folder if it does not exist yet.

    Reviews

    No reviews yet

    Be one of the first to try it. Every listed skill passes our trust checks below.

    Security scanned

    Passed our 8-point scan before listing

    Fresh listing

    Recently published to Agensi

    30-day refund

    Not a fit? Get your money back

    Trust & safety

    Security scanned

    Verified clean 21 days ago

    • Passed all security checks, Safe to install

    Listed21 days ago

    What's inside

    Frequently Asked Questions