Back to gallery

Coding

SQL Query Review

Review a SQL query for correctness, security, performance, and maintainability.

Models
ChatGPT, Claude
Level
Advanced
Version
v0.9

Published by HiddenPrompts · Published · Updated

Editorial template. Model evaluation is not recorded; the preview illustrates the intended result. Testing Method

01

What this prompt does

Review a SQL query for correctness, security, performance, and maintainability.

02

Best for

Correctness issues, injection risks, index considerations, rewritten query, EXPLAIN plan checklist, and edge cases.

03

Prompt variables

[SQL][SCHEMA][DATABASE][GOAL]
System Prompt
You are a database reviewer. Treat schema and execution-plan facts as unknown unless supplied.
User Prompt
OBJECTIVE:
Review [SQL] against [SCHEMA] on [DATABASE] for the goal [GOAL].

INPUTS:
SQL: [SQL]
SCHEMA: [SCHEMA]
DATABASE: [DATABASE]
GOAL: [GOAL]

OUTPUT CONTRACT:
Correctness issues, injection risks, index considerations, rewritten query, EXPLAIN plan checklist, and edge cases.

How to use

State the business meaning of the result and provide a tiny fixture with expected rows. Review joins and filters for correctness before discussing performance. Share existing indexes and an execution plan if performance is part of the task. Ask the assistant to distinguish a logical error from an index hypothesis and to preserve the database’s actual behavior.

Why it works

The blueprint separates authority, task context, evidence limits, and the output contract so requirements can be checked independently.

Model compatibility

ChatGPTuntestedEditorial compatibility hypothesis; cross-model test record pending.

ClaudeuntestedEditorial compatibility hypothesis; cross-model test record pending.

Authored teaching example, not a recorded model response.

05

Example input

SQL: SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id
SCHEMA: orders(id, customer_id, status)
DATABASE: MySQL; supply actual version for a real review
GOAL: Count completed orders per customer

06

Example output

Correctness finding: The query counts all statuses, not completed orders.
Candidate query: SELECT customer_id, COUNT(*) FROM orders WHERE status = 'completed' GROUP BY customer_id;
Verification: Use one customer with two completed orders and one canceled order; expected count is two.
Performance: Inspect EXPLAIN and representative table sizes before recommending an index. No speedup is measured here.

How to customize

For a join, include one unmatched row and one relationship with multiple matches. For date filtering, specify the column type and timezone boundary. Keep any proposed index separate from the query correction so you can validate each change independently.

Common mistakes

Do not claim a performance improvement without measuring it. A faster query that counts the wrong rows is not a successful optimization.

Test notes

Editorial blueprint awaiting recorded cross-model evaluation. It is visible for review but is not labeled as tested.

Why is this blueprint not marked as tested?

It has passed editorial structure review but does not yet have a published cross-model test record.