Skip to main content
AIPromptIndex
ChatGPT Data Analysis intermediate

SQL Query Debugger

Added Apr 2, 2026

You are a senior database engineer specializing in SQL performance and correctness. I have a SQL query that is either producing incorrect results or running too slowly. Here is my query: [SQL_QUERY] The database system is [DATABASE_SYSTEM]. The expected behavior is [EXPECTED_BEHAVIOR], but instead it [ACTUAL_BEHAVIOR]. Please analyze this query step by step: 1. Identify any logical errors, including incorrect JOINs, missing WHERE conditions, or faulty GROUP BY clauses 2. Check for common pitfalls like NULL handling issues, implicit type conversions, or off-by-one errors in date ranges 3. Evaluate performance concerns such as missing indexes, unnecessary subqueries, or full table scans 4. Rewrite the corrected and optimized version of the query with inline comments explaining each change 5. Suggest an execution plan analysis approach to verify the improvements 6. Recommend any schema-level changes that would further boost performance
0
Share
Try in ChatGPT

About This Prompt

This prompt transforms ChatGPT into a SQL debugging assistant that systematically diagnoses and fixes problematic database queries. Whether your query returns wrong results, times out on large datasets, or produces unexpected NULLs, this prompt walks through a structured analysis covering logical errors, performance bottlenecks, and optimization opportunities. It is particularly valuable for developers who work with complex multi-table joins, aggregation queries, or legacy SQL that has grown unwieldy over time. The output includes a corrected query with explanatory comments, making it both a fix and a learning tool for improving your SQL skills going forward.

Variables to Customize

[SQL_QUERY]

The SQL query you need debugged or optimized

Example: SELECT u.name, COUNT(o.id) FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE o.status = 'completed' GROUP BY u.name

[DATABASE_SYSTEM]

The database engine you are using

Example: PostgreSQL 15

[EXPECTED_BEHAVIOR]

What the query should return

Example: all users with their completed order count, including users with zero orders

[ACTUAL_BEHAVIOR]

What the query actually does wrong

Example: users with zero orders are excluded from the results

Tips for Best Results

  • Include the table schema or at least column types when dealing with type conversion issues
  • Mention the approximate row counts for each table so the optimizer suggestions are realistic
  • If you have an EXPLAIN output already, paste it alongside the query for deeper analysis

Example Output

**Issue Identified:** Your LEFT JOIN is being converted to an INNER JOIN because the WHERE clause filters on `o.status = 'completed'`. When a user has no orders, `o.status` is NULL, which fails the equality check.

**Corrected Query:**
```sql
SELECT u.name, COUNT(o.id)
FROM users u
LEFT JOIN orders o ON u.id = o.user_id AND o.status = 'completed'
GROUP BY u.name;
```

The fix moves the filter condition into the JOIN clause so users without matching orders still appear with a count of 0.
sql debugging database optimization performance

Get the Best AI Prompts Weekly

Curated prompts, tips, and guides delivered to your inbox every week. Free.