Recursive CTE Query Builder

Build recursive Common Table Expression queries for hierarchical data

6/11/2026
Prompt
Build a recursive CTE for this hierarchical data requirement:

**Table Structure:**
```sql
{{TABLE_STRUCTURE}}
```

**Hierarchy Type:** {{HIERARCHY_TYPE}}
(e.g., org chart, category tree, BOM, graph traversal)

**Query Requirement:** {{REQUIREMENT}}
**Max Depth:** {{MAX_DEPTH}}
**Database System:** {{DATABASE_SYSTEM}}

Please provide:
1. Recursive CTE query with:
   - Anchor member
   - Recursive member
   - Termination condition
2. Step-by-step execution explanation
3. Path tracking implementation
4. Cycle detection if needed
5. Performance optimization tips
6. Depth limiting implementation
7. Alternative approaches (nested sets, materialized path)
8. Sample data and expected output

Or press ⌘C to copy

Variables (5)

Replace these placeholders with your own content before using the prompt.

[{TABLE_STRUCTURE][{HIERARCHY_TYPE][{REQUIREMENT][{MAX_DEPTH][{DATABASE_SYSTEM]

Ready to use?

Copy and paste into your favorite AI tool

Test this prompt live

Try it in our AI Playground with Claude

Open Playground

Explore more Coding prompts

Browse Coding

Command Palette

Search for a command to run...