- 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 tools 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
- 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_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.
sql-result-repair-agent.pdf
PDF · document
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
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.
How to install
Works the same in every agent - Claude, Cursor, Codex, Copilot and 20+ more.
- 1
Download the ZIP
Free skills download straight away. Paid skills unlock right after purchase.
- 2
Unzip into your skills folder
Every agent reads skills from one folder on your machine. Drop the unzipped folder in there.
- 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