Skip to content
Back to skills

Recursive Queries

ASecurity

Walk hierarchies and graphs with recursive CTEs, with a termination condition and cycle protection so the query ends. Use when querying trees such as org charts, categories, threads, or dependency graphs.

  • 7 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 5, 2026
ai-agentsgonodegitdatabaseperformance

Security analysis

A100/100

Scanned September 5, 2026

npx -y skills add Amey-Thakur/AI-SKILLS --skill recursive-queries --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Recursive Queries?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Recursive Queries
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/amey-thakur-recursive-queries/badge)](https://www.skillsdirectory.com/skills/amey-thakur-recursive-queries)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: recursive-queries
description: Walk hierarchies and graphs with recursive CTEs, with a termination condition and cycle protection so the query ends. Use when querying trees such as org charts, categories, threads, or dependency graphs.
---

# Recursive queries

A recursive CTE repeats a step until it produces nothing new, which is
how you follow a parent chain or expand a dependency graph in one query.
The two failure modes are a recursion that never terminates and one that
revisits the same node forever.

## Method

1. **Write the anchor and the step separately.** The anchor selects the
   starting rows and the recursive part joins the CTE back to the table.
   Getting the anchor wrong produces an empty or enormous result
   immediately.
2. **Track depth and cap it.** Carrying a level column lets you both
   report depth and stop at a maximum, which protects against
   unexpectedly deep or malformed data.
3. **Detect cycles explicitly.** Accumulate the visited path and exclude
   nodes already in it. Real hierarchies contain accidental cycles more
   often than schemas admit.
4. **Return the path when it is the point.** Building a breadcrumb of
   ancestors as you descend is usually the reason for the query and
   costs almost nothing to carry.
5. **Choose direction by the question.** Descendants start from a node
   and follow children; ancestors start from a node and follow parents.
   The shape is the same and the join direction reverses.
6. **Consider a materialised path or closure table for hot reads.**
   Recursion is elegant and not always fast, and denormalising the
   hierarchy is a legitimate trade when reads dominate (see
   database-normalization).

## Boundaries

- Recursive CTEs suit modest hierarchies; large graph traversal belongs
  in a graph database or an application algorithm.
- Performance degrades quickly with breadth, since each level joins
  against everything found so far.
- Syntax and the RECURSIVE keyword requirement differ between engines.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…