Back to Cookbook

Query Decoder

Turn spaghetti SQL into readable, documented code

Paste any monster SQL query — 400 lines of nested CTEs with no comments — and get back a documented version with plain-English explanations, a dependency diagram, and suggestions for modularization.

CommunitySubmitted by CommunityWork1 min

PROMPT

Create a skill called "Query Decoder". When I paste a SQL query, analyze it thoroughly and produce: (1) A one-paragraph plain-English summary of what the query calculates or produces. (2) The same query with inline comments explaining each CTE, subquery, JOIN, and non-obvious WHERE clause. (3) A list of all tables and columns referenced, with their apparent purpose. (4) A text-based dependency diagram showing how CTEs feed into each other. (5) Any implicit business rules found (e.g., "only includes orders where status != 'cancelled' and created_date > '2023-01-01'"). (6) Suggestions for breaking the query into smaller, reusable pieces (as dbt models or database views). If I provide business context, incorporate it into the documentation.

How It Works

Legacy SQL queries are terrifying. The senior analyst who wrote them left

two years ago, there are no comments, and touching anything might break

downstream reports. This skill reverse-engineers the logic, adds documentation,

and shows you what's actually happening.

What You Get

  • Plain-English summary of what the query does
  • Inline comments for each CTE/subquery explaining its purpose
  • A dependency diagram showing how CTEs relate to each other
  • Business logic extraction (implicit rules buried in WHERE clauses and CASE statements)
  • Suggestions for breaking the query into reusable modules (dbt models, views)
  • A list of all tables referenced with their roles in the query

Setup Steps

  1. Ask your Claw to create a "Query Decoder" skill with the prompt below
  2. Paste the undocumented SQL query
  3. Optionally describe the business context ("this generates our weekly revenue report")
  4. Get back the annotated version plus a summary document

Tips

  • Use this when onboarding to a new team — decode the critical queries first
  • The business logic extraction is especially valuable for preserving institutional knowledge
  • For dbt teams, the modularization suggestions map directly to new model files
  • Run this on departing team members' most-used queries before they leave
Tags:#sql#documentation#legacy-code#onboarding