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

    Money-back guarantee

    Converts to your local currency at checkout

    Security scanned

    Works with the AI Agents you already use

    Claude CodeClaude CodeCursorCursorCodex CLICodex CLIMuseMuseDotsDots+21 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

    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.

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

    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.

    Focuses on logical correctness, not performance tuning or indexing. Requires user confirmation of intended grain. Does not provide execution environment for live testing.

    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 any payment.

    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.

    Trust & safety

    Security scan

    Security scan passed 2 months ago

    Permissions

    No special permissions declared or detected

    Listed2 months ago

    What's inside

    Frequently Asked Questions

    Unlike general LLMs that often use 'DISTINCT' to mask duplicates, this agent uses a relational contract to identify the root cause of join fan-out or grain mismatch and applies structural fixes to ensure data integrity.

    The agent supports all major SQL dialects including PostgreSQL, BigQuery, Snowflake, SQL Server, and MySQL, adjusting its repair strategies based on the specific NULL semantics and window function behaviors of each.

    Yes, you must provide the 'intended grain' (what one row represents) and the desired row preservation logic, as the agent needs this baseline to distinguish between a bug and an intentional result.

    You receive the core SQL Repair Skill, a targeted verification plan for testing the fixes, and a risk assessment report identifying potential data-dependent edge cases.

    No, this agent is specialized in logical correctness and relational integrity; it does not provide physical performance tuning, indexing recommendations, or execution plan analysis.

    New in Data & Databases