Debug or Explain a SQL Query

3 Views
0 Uses

Prompt

ChatGPT
Claude
Grok
OpenRouter
You are a senior SQL reviewer. I will give you a query that errors, runs slowly, or returns numbers I don't trust.

SQL dialect: {{SQL_DIALECT}}
The query: {{SQL_QUERY}}
Schema notes (columns, keys, approximate table sizes): {{SCHEMA_NOTES}}
What is wrong: the error text, or the result I expected versus what I got: {{PROBLEM}}

Do this:
1. Explain what the query does, clause by clause, in logical order of execution (FROM and JOIN, WHERE, GROUP BY, HAVING, SELECT, ORDER BY).
2. Identify the problem. For wrong numbers, check the usual suspects: join fan-out duplicating rows, filters on the outer table turning a LEFT JOIN into an inner join, COUNT versus COUNT(DISTINCT), NULL comparisons, integer division, time zone boundaries in date filters.
3. Give a corrected query, with the changed lines marked.
4. Provide a small check that would have caught the bug.
5. If it is slow, suggest likely causes (functions on indexed columns, missing filters, SELECT *) but tell me to check the real execution plan, because you cannot see my data or indexes.

If you need more information to be certain, say exactly what. Don't present a guess about performance as a fact.

Customize this prompt

Fill in the variables below to personalize this prompt:

ChatGPT
Claude
Grok
OpenRouter

Model Settings

Recommended Models

ChatGPT Claude Gemini

Temperature

0.7

Max Tokens

2000

Additional Notes

Best for: Finding why a query is wrong, slow or confusing

About this prompt

“Debug or Explain a SQL Query” is a free prompt for ChatGPT in the Coding & Software category, 193 words long, added to The Prompt Index on October 28, 2026 by The Prompt Index. Copy it with the button above, paste it into ChatGPT or any other AI chat assistant, and send it.

It has 4 placeholders to fill in first: {{SQL_DIALECT}}, {{SQL_QUERY}}, {{SCHEMA_NOTES}}, {{PROBLEM}}. Use the “Customize this prompt” fields to personalise it before you copy.

Recommended models: ChatGPT, Claude, Gemini.

0 upvotes, sent to an AI tool 0 times, viewed 3 times.

Browse all Coding & Software prompts, the AI prompt database or use the AI prompt finder to search every prompt.