Data AnalysisHierarchy SQL

Traverse a hierarchy with a recursive CTE

Use for org charts, category trees or bill-of-materials stored as parent/child rows.

The prompt
prompt.txt
You are a SQL specialist in {{sql_dialect}}.

Table {{table_name}} stores a hierarchy with id {{id_column}} and parent id {{parent_column}}, plus {{label_column}}.

Write recursive CTEs to:
1. Return the full path from any node up to the root.
2. Return all descendants of a given node {{start_node}} with their depth level.
3. Compute a rolled-up sum of {{measure_column}} from leaves up to each parent.
4. Detect and guard against cycles.

Explain the anchor vs recursive member and any depth limit I should set.

Click the copy button in the top right of the block to grab the full prompt.

Variables

Replace each placeholder below with your own values before you run the prompt.

  • {{sql_dialect}}
  • {{table_name}}
  • {{id_column}}
  • {{parent_column}}
  • {{label_column}}
  • {{start_node}}
  • {{measure_column}}
Recommended models
Claude Opus 4.8GPT-5Gemini 2.5 Pro
Tags
#sql#recursive-cte#hierarchy#tree

Related prompts

Data AnalysisPlan cleaning
Build a data cleaning plan from a messy dataset

You are a meticulous data analyst. Here is a sample of my raw dataset (header row plus a few rows): {{sample_rows}} Context: this data describes {{data_description}} and I plan to...

Claude Opus 4.xGPT-5Gemini 2.5 Pro
#cleaning#data-quality#planning
View
Data AnalysisWrite cleaning code
Generate pandas code to clean a dataset

You are a senior Python data engineer. Write clean, reproducible pandas code to clean my dataset. Columns and their meaning: {{column_spec}} Known issues to handle: {{known_issues}...

Claude Opus 4.xGPT-5Gemini 2.5 Pro
#pandas#python#cleaning
View
Data AnalysisPlan EDA
Run an exploratory data analysis walkthrough

Act as a data analyst guiding me through exploratory data analysis. Dataset summary: {{dataset_summary}} My goal / the question I care about: {{analysis_goal}} Produce an EDA plan...

Claude Opus 4.xGPT-5Gemini 2.5 Pro
#eda#exploration#planning
View
Data AnalysisWrite SQL
Write a SQL query from a plain-English question

You are a SQL expert writing for a {{sql_dialect}} database. Here is the relevant schema (tables, columns, types, keys): {{schema}} Question to answer: {{question}} Rules: - Use on...

Claude Opus 4.xGPT-5Gemini 2.5 Pro
#sql#querying#database
View
Data AnalysisDebug SQL
Explain and debug an existing SQL query

You are a SQL reviewer. Explain and debug the query below. Dialect: {{sql_dialect}} What I expected it to return: {{expected_result}} What is actually wrong (if known): {{symptom}}...

Claude Opus 4.xGPT-5Gemini 2.5 Pro
#sql#debugging#performance
View
Data AnalysisTune SQL
Optimize a slow SQL query

You are a database performance specialist for {{sql_dialect}}. Slow query: {{query}} Context: - Approximate row counts of the main tables: {{table_sizes}} - Existing indexes: {{ind...

Claude Opus 4.xGPT-5Gemini 2.5 Pro
#sql#performance#optimization
View

0 Comments

Sign in to post

Loading discussion...