- Home
- Skills
- Data & Databases
- SQL Review: Catch Wrong Numbers, Slow Queries & Injection
Works with the AI Agents you already use
SQL Review: Catch Wrong Numbers, Slow Queries & Injection
by Aryan Sharan
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
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
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
| # | Severity | Confidence | Area | Finding |
|---|---|---|---|---|
| 1 | Blocker | Proven | Correctness | Joins to order_items and refunds repeat each order: revenue and order counts inflated |
| 2 | Blocker | Proven | Correctness | BETWEEN ... '2026-01-31' stops at midnight and drops almost all of 31 January |
| 3 | Major | Proven | Correctness | WHERE on the LEFT JOINed orders table removes regions with no orders |
| 4 | Major | Proven | Correctness | Integer division makes the refund rate 0 or 100 |
| 5 | Minor | Likely | Definition | "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)
| Region | Revenue before | Revenue after | Orders before | Orders after |
|---|---|---|---|---|
| North | 1,120.00 | 400.00 | 8 | 3 |
| South | 50.00 | 200.00 | 1 | 2 |
| West | missing | 0.00 | missing | 0 |
| Total | 1,170.00 | 600.00 | 9 | 5 |
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
- Paste your SQL query or a file containing database code into the chat.
- Tell your AI agent which database you use, like PostgreSQL, MySQL, or BigQuery.
- This skill scans the code for logic errors, slow spots, and security holes.
- It provides a table of findings from "Blocker" to "Minor" with clear proof for each one.
- 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
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 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.
Trust & safety
Security scan
Security scan passed 2 days ago
Nothing to install. It runs in your AI agent.
- Optional
Python 3.10+sqlglot Python package (for the checking script)
Permissions
What's inside
Frequently Asked Questions
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.