- Home
- Skills
- Data & Databases
- sql result repair agent
sql result repair agent
Diagnose and repair logical SQL errors like join fan-out, grain mismatch, and improper NULL handling.
$5
Works with the AI Agents you already use
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_pricegrouped bycustomer_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
| Aspect | Check | Expected Result |
|---|---|---|
| Grain | count(distinct customer_id) | Matches count(*) of query |
| Fan-out | Check for order_id duplicates | 0 duplicates |
| Preservation | Count customers with null orders | Should 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_priceis NOT NULL; otherwise,SUMbehavior 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
- Run the Verification Plan table queries against your dev environment.
- Confirm if customers with empty orders should show as $0.00 or NULL.
- Check the
EXPLAINplan 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 JOINlogic whereWHEREclauses 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
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
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 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