SQL Query Readability Rewrite and Documentation
Turn a hard-to-read query into one a colleague can maintain.
The Open buttons send the prompt text as currently shown below to that site only when you click. Fill in the placeholders first.
[VERIFY] is an editorial flag, not a blank to fill. Wherever the prompt shows [VERIFY], check that item against a reliable source before you use it.
Rewrite this SQL query for readability and document it. Query: [QUERY]. Database system: [DATABASE]. What it is supposed to answer: [PURPOSE]. Tables involved and their meaning: [TABLE_NOTES]. Preserve the results first, then improve style. Produce a version with consistent formatting, meaningful aliases, common table expressions in place of nested subqueries where that helps, explicit join types, and comments on business rules. Explain why the rewrite is logically equivalent and list where it might differ (NULL handling, duplicate rows, order of operations in joins). Do not change business logic unless you flag it as a suggested change, separate from the rewrite, and ask me to confirm the meaning of any ambiguous filter. Then write Markdown documentation: purpose, inputs, filters and their meaning, output columns with definitions, known limits, and an example of output with invented values. Output the query in a code block, the equivalence notes as a list, and the documentation. Suggested tests: compare row counts and totals between the original and the rewrite on the same data and check a few specific records. Mark syntax that varies by database [VERIFY].
Fill in before use
Optional: type your details and the prompt above updates. Nothing is saved or sent.
Replace each bracketed placeholder with your own details, then paste into Any AI or another assistant.