Skip to content

Instantly share code, notes, and snippets.

@ortsed
Created May 2, 2024 12:07
Show Gist options
  • Save ortsed/57e34bd6887f1788a3c316d9e6e49f34 to your computer and use it in GitHub Desktop.
Save ortsed/57e34bd6887f1788a3c316d9e6e49f34 to your computer and use it in GitHub Desktop.
Recursive CTE SQL
WITH RECURSIVE NetworkCTE AS (
-- Anchor part to select the starting node
SELECT source_node,
target_node
FROM network_connections
WHERE source_node = 'Paolo' -- Change this to get someone else's network
UNION ALL
-- Recursive part to select connected nodes
SELECT nc.source_node,
nc.target_node
FROM network_connections nc
JOIN NetworkCTE n ON nc.source_node = n.target_node
)
SELECT * FROM NetworkCTE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment