Debug SQL query errors and incorrect row counts
Find syntax errors, incorrect joins, and silent null filter bugs in database queries. Use it when a query returns syntax errors or unexpected row counts.
You getRoot Cause Analysis, corrected SQL Query and explanation of Changes
Prompt
Act as a principal database administrator and SQL expert. Analyze the SQL query, schema context, and problem description pasted below.
- Identify the exact root cause of the error or unexpected row count, checking joins, WHERE clauses, NULL handling, and GROUP BY rules.
- Provide an optimized, corrected SQL query adhering to standard SQL conventions.
- Explain precisely why the original query misbehaved and how your change fixes it.
- Provide a verification query to run against sample data to validate correctness.
Output format:
1. Root Cause Analysis (bullet points)
2. Corrected SQL Query (fenced code block)
3. Explanation of Changes (numbered list)
4. Verification Query (code block)
Here is the query, database dialect, and the issue:Example output
Paste your broken SQL query along with your database dialect (e.g. PostgreSQL, MySQL, Snowflake) into ChatGPT, Claude, or Gemini. Ask to add indexes or performance recommendations as a follow-up.
Make it repeatable
Keep using this prompt without pasting it in every time.
Prompt details
- By
- Prompts-GPT editors
- Published
Did this prompt work?
Keep exploring
More like this
Explain a Complex SQL Query in Plain English, Step by Step
Get a plain-English walkthrough of any SQL query in execution order, with tiny sample tables, row counts after each clause, and the hidden gotchas.
Design Cursor-Based Pagination Logic for High-Volume APIs
Get a complete implementation plan for cursor-based pagination including SQL queries, cursor encoding logic, and strategies for handling record deletions.
Find and Fix Data Quality Issues with Ready-to-Run SQL Checks
Paste your schema and get SQL queries that catch duplicates, NULLs, orphaned foreign keys and out-of-range values, each with what a bad result means and a fix.
Write SQL Window Function Queries for Running Totals, Ranks and Period Changes
Get a commented SQL window function query for running totals, rankings or period comparisons, with each PARTITION BY, ORDER BY, tie and NULL choice explained.
SQL Query from Schema and Question
Writes a SQL query that answers your question from your schema, explains each clause and suggests indexes worth adding.
Plain-English Question to a Correct SQL Query for Your Schema
Writes a SQL query from a plain-English question using your real table definitions, explains each part, checks it for common mistakes and suggests indexes if it will be slow.
- Convert raw error logs into a clean bug report
- Find the failing line in a stack trace
- Fix N+1 Database Query Patterns in ORM Code
- Design a Normalized Database Schema from a Plain Feature Description
- Optimize a Slow SQL Query Using Its EXPLAIN Plan Step by Step
- Translate SQL Between MySQL, PostgreSQL and SQL Server with a Change Table