SlideShare a Scribd company logo
WRITING RECURSIVE QUERIES
RETRIEVING HIERARCHY DATA FROM RELATIONAL TABLES
BEN LIS
POSTGRESCONF US 2019
IS THIS TALK RIGHT FOR YOU?
What we will cover
• Why we need recursive queries
• How they work
• How to write them
What you should know
• Basic knowledge of SQL through outer joins
• General programming concepts
• Slides and SQL:
https://github.com/benjlis/talk-writing-recursive-
queries
2
ABOUT ME
• Data Engineer at LEI Smart
• Writes recursive queries on corporate hierarchy data
• Initially found recursive SQL a bit confusing
• Using PostgreSQL since 2015
• Started working with Oracle databases in 1988!
• Adjunct Associate Faculty in Applied Analytics at Columbia University
• Held various technical, product, and management roles on Wall Street
• LinkedIn and Twitter
3
AGENDA
• Motivation
• Understanding Recursive SQL
• Writing Recursive Queries
• Next Steps
• Questions & Discussion
4
MOTIVATION
WHY WE NEED RECURSIVE SQL
5
HR DATABASE
hrex-create.sql
6
LET’S QUERY EMPLOYEES, THEIR BOSS & THE BOSS'S BOSS
boss-queries.sql
7
LET’S QUERY
EMPLOYEES’
ENTIRE
REPORTING LINE
HMMM… HOW DO WE DO THAT?
8
IDEA 1: EXTEND
CURRENT
APPROACH
“Add a few more JOINs”
Need to know the number of hierarchy levels
in advance
Not realistic or robust!
9
IDEA 2: WRITE A
PROGRAM
Use Python, Java, PL/pgSQL, etc.
Query all employees
For each employee get their boss’s record
and then their boss’s boss’s record, etc.
until you get a NULL
Complex and resource consumptive!
10
SHOULDN’T WE BE
ABLE TO WRITE
THIS QUERY IN
SQL?
11
WE SHOULD BE ABLE TO WRITE THIS QUERY IN SQL
• In the early days (decades) of SQL & relational databases we couldn’t
• Writing a program (Idea 2) was the only solution
• Other names for the hierarchy problem:
• Transitive Closure
• Bill of Materials
• Parts Explosion
• Good news: We can write this query in modern SQL using recursive SQL!
12
UNDERSTANDING RECURSIVE SQL
KEY IDEAS, CTE, SYNTAX, STRUCTURE & EVALUATION
13
RECURSIVE SQL SUPPORT
Part of ISO Standard SQL
Introduced in SQL:1999 (aka SQL 3)
Supported in
PostgreSQL
Oracle
SQL Server
DB2
14
KEY IDEAS
• re·cur·sive /rəˈkərsiv/: a program or routine of which a part
requires the application of the whole, so that its explicit
interpretation requires in general many successive executions
• Self-referencing
• Your Bosses = Your Manager + Your Manager’s Bosses
• Recursive SQL is an iterative process
• Think while loops
• But SQL is declarative!!!!
• Tip: Put aside knowledge of recursion in programming
• Recursive SQL uses Common Table Expressions (CTEs)
15
COMMON TABLE EXPRESSION (CTE) EXAMPLE
cte.sql
• Primarily used to simplify queries
• Think of as inline view(s)
16
RECURSIVE CTE
STRUCTURE
• Keyword: recursive
• Always a UNION or UNION ALL of
• Non-recursive select
• Recursive select
17
NON-RECURSIVE
SELECT
• Evaluated 1st - where we start
• FROM clause:
• Must not refer to <cte-name>
• Refers to table containing hierarchy
18
RECURSIVE SELECT
• Evaluated 2..N times
• FROM clause:
• Must reference <cte-name>
• Join of the <cte-name> and table
containing hierarchy
• N is a function of the data and the
recursive select’s where clause
19
RECURSIVE CTE
EVALUATION
QR = Query Results; WT = Working Table
Both initially empty
1. Execute <non-recursive select>
2. Add results to QR and WT
3. Repeat until WT is empty
a. Execute <recursive select> using WT
data for <cte-name>
b. Add results to QR
c. Replace WT with results
20
WRITING RECURSIVE QUERIES
LET’S GET DOWN TO IT!
21
WHAT IS ADAMS REPORTING LINE?
rl.sql
22
ALL EMPLOYEES FROM CEO TO ENTRY LEVEL
oc.sql
23
INCLUDE A LEVEL
oc-level.sql
24
INCLUDE A PATH
oc-path.sql
25
NEXT STEPS
RECOMMENDED READINGS & BEYOND HIERARCHY
26
RECOMMENDED READINGS
• PostgreSQL documentation
• Fun with SQL: Recursive CTEs in Postgres – Craig Kerstiens
• Modern SQL in Open Source and Commercial Databases (slides 45-60) – Markus Winand
• Exporting a Hierarchy in JSON: with recursive queries - Dmitri Fontaine
• Joe Celko’s Trees and Hierarchies in SQL for Smarties (Book)
27
BEYOND HIERARCHY
• Hierarchy queries are the most common application of recursive SQL, but not the only application!
• Pretty wild recursive SQL queries:
• Solving the Traveling Salesman Problem with Postgres Recursive CTEs - Periscope Data
• Mandelbrot Set
• tic-tac-toe
• My views:
• Can doesn’t mean should
• Worth studying for learning advanced techniques
28
QUESTIONS
29

More Related Content

What's hot (17)

PDF
Overview of Apache Spark 2.3: What’s New? with Sameer Agarwal
Databricks
 
PDF
Structured Streaming Use-Cases at Apple
Databricks
 
PDF
WebCamp: Developer Day: Оптимизация Lift Framework для работы с большими пото...
GeeksLab Odessa
 
PDF
Lessons Learned with Spark at the US Patent & Trademark Office-(Christopher B...
Spark Summit
 
PDF
Migrating to Spark 2.0 - Part 2
datamantra
 
PDF
Introduction to spark 2.0
datamantra
 
PDF
Enabling Scalable Data Science Pipeline with Mlflow at Thermo Fisher Scientific
Databricks
 
PDF
Workflow Engines for Hadoop
Joe Crobak
 
PDF
Introduction to Spark 2.0 Dataset API
datamantra
 
PDF
Scaling Security Threat Detection with Apache Spark and Databricks
Databricks
 
PDF
Grokking TechTalk #20: PostgreSQL Internals 101
Grokking VN
 
PPTX
How Totango uses Apache Spark
Oren Raboy
 
PDF
Galene - LinkedIn's Search Architecture: Presented by Diego Buthay & Sriram S...
Lucidworks
 
PDF
Migrating to spark 2.0
datamantra
 
PDF
Spark Summit EU talk by Oscar Castaneda
Spark Summit
 
PDF
Introduction to Apache Spark 2.0
Knoldus Inc.
 
PDF
Spark Summit EU talk by Kent Buenaventura and Willaim Lau
Spark Summit
 
Overview of Apache Spark 2.3: What’s New? with Sameer Agarwal
Databricks
 
Structured Streaming Use-Cases at Apple
Databricks
 
WebCamp: Developer Day: Оптимизация Lift Framework для работы с большими пото...
GeeksLab Odessa
 
Lessons Learned with Spark at the US Patent & Trademark Office-(Christopher B...
Spark Summit
 
Migrating to Spark 2.0 - Part 2
datamantra
 
Introduction to spark 2.0
datamantra
 
Enabling Scalable Data Science Pipeline with Mlflow at Thermo Fisher Scientific
Databricks
 
Workflow Engines for Hadoop
Joe Crobak
 
Introduction to Spark 2.0 Dataset API
datamantra
 
Scaling Security Threat Detection with Apache Spark and Databricks
Databricks
 
Grokking TechTalk #20: PostgreSQL Internals 101
Grokking VN
 
How Totango uses Apache Spark
Oren Raboy
 
Galene - LinkedIn's Search Architecture: Presented by Diego Buthay & Sriram S...
Lucidworks
 
Migrating to spark 2.0
datamantra
 
Spark Summit EU talk by Oscar Castaneda
Spark Summit
 
Introduction to Apache Spark 2.0
Knoldus Inc.
 
Spark Summit EU talk by Kent Buenaventura and Willaim Lau
Spark Summit
 

Similar to Writing Recursive Queries (20)

PDF
M|18 Taking Advantage of Common Table Expressions
MariaDB plc
 
PDF
Common Table Expressions in MariaDB 10.2 (Percona Live Amsterdam 2016)
Sergey Petrunya
 
PDF
Common Table Expressions in MariaDB 10.2
Sergey Petrunya
 
PDF
Recursive Query Throwdown
Karwin Software Solutions LLC
 
PDF
Common Table Expressions (CTE) & Window Functions in MySQL 8.0
oysteing
 
PPT
Handling tree structures — recursive SPs, nested sets, recursive CTEs
Mind The Firebird
 
PDF
[APJ] Common Table Expressions (CTEs) in SQL
EDB
 
PDF
Programming the SQL Way with Common Table Expressions
EDB
 
PPTX
T sql語法之 cte 20140214
LearningTech
 
PDF
MySQL Optimizer: What’s New in 8.0
oysteing
 
PPTX
Query hierarchical data the easy way, with CTEs
MariaDB plc
 
PDF
More SQL in MySQL 8.0
Norvald Ryeng
 
PPTX
Set operators - derived tables and CTEs
Steve Stedman
 
PPTX
Set Operators, Derived Tables and CTEs
Aaron Buma
 
PPTX
Modern sql
Elizabeth Smith
 
PDF
Modern sql
Creditas
 
PDF
Techday2010 Postgresql9
Dan-Claudiu Dragoș
 
PPTX
MySQL Optimizer: What's New in 8.0
Manyi Lu
 
PPTX
Sql analytic queries tips
Vedran Bilopavlović
 
PPT
Les19
Vijay Kumar
 
M|18 Taking Advantage of Common Table Expressions
MariaDB plc
 
Common Table Expressions in MariaDB 10.2 (Percona Live Amsterdam 2016)
Sergey Petrunya
 
Common Table Expressions in MariaDB 10.2
Sergey Petrunya
 
Recursive Query Throwdown
Karwin Software Solutions LLC
 
Common Table Expressions (CTE) & Window Functions in MySQL 8.0
oysteing
 
Handling tree structures — recursive SPs, nested sets, recursive CTEs
Mind The Firebird
 
[APJ] Common Table Expressions (CTEs) in SQL
EDB
 
Programming the SQL Way with Common Table Expressions
EDB
 
T sql語法之 cte 20140214
LearningTech
 
MySQL Optimizer: What’s New in 8.0
oysteing
 
Query hierarchical data the easy way, with CTEs
MariaDB plc
 
More SQL in MySQL 8.0
Norvald Ryeng
 
Set operators - derived tables and CTEs
Steve Stedman
 
Set Operators, Derived Tables and CTEs
Aaron Buma
 
Modern sql
Elizabeth Smith
 
Modern sql
Creditas
 
Techday2010 Postgresql9
Dan-Claudiu Dragoș
 
MySQL Optimizer: What's New in 8.0
Manyi Lu
 
Sql analytic queries tips
Vedran Bilopavlović
 
Ad

Recently uploaded (20)

PPTX
727325165-Unit-1-Data-Analytics-PPT-1.pptx
revathi148366
 
PPTX
@Reset-Password.pptx presentakh;kenvtion
MarkLariosa1
 
DOCX
Artigo - Playing to Win.planejamento docx
KellyXavier15
 
PPTX
25 items quiz for practical research 1 in grade 11
leamaydayaganon81
 
PPTX
Indigo dyeing Presentation (2).pptx as dye
shreeroop1335
 
PDF
A Web Repository System for Data Mining in Drug Discovery
IJDKP
 
PPTX
PPT2 W1L2.pptx.........................................
palicteronalyn26
 
PDF
Data science AI/Ml basics to learn .pdf
deokhushi04
 
PDF
TCU EVALUATION FACULTY TCU Taguig City 1st Semester 2017-2018
MELJUN CORTES
 
PDF
624753984-Annex-A3-RPMS-Tool-for-Proficient-Teachers-SY-2024-2025.pdf
CristineGraceAcuyan
 
PPTX
Communication_Skills_Class10_Visual.pptx
namanrastogi70555
 
PPTX
english9quizw1-240228142338-e9bcf6fd.pptx
rossanthonytan130
 
PPTX
Presentation by Tariq & Mohammed (1).pptx
AbooddSandoqaa
 
DOCX
Udemy - data management Luisetto Mauro.docx
M. Luisetto Pharm.D.Spec. Pharmacology
 
PPSX
PPT1_CB_VII_CS_Ch3_FunctionsandChartsinCalc.ppsx
animaroy81
 
DOCX
Starbucks in the Indian market through its joint venture.
sales480687
 
PPT
Camuflaje Tipos Características Militar 2025.ppt
e58650738
 
PPTX
MENU-DRIVEN PROGRAM ON ARUNACHAL PRADESH.pptx
manvi200807
 
PDF
ilide.info-tg-understanding-culture-society-and-politics-pr_127f984d2904c57ec...
jed P
 
PDF
Orchestrating Data Workloads With Airflow.pdf
ssuserae5511
 
727325165-Unit-1-Data-Analytics-PPT-1.pptx
revathi148366
 
@Reset-Password.pptx presentakh;kenvtion
MarkLariosa1
 
Artigo - Playing to Win.planejamento docx
KellyXavier15
 
25 items quiz for practical research 1 in grade 11
leamaydayaganon81
 
Indigo dyeing Presentation (2).pptx as dye
shreeroop1335
 
A Web Repository System for Data Mining in Drug Discovery
IJDKP
 
PPT2 W1L2.pptx.........................................
palicteronalyn26
 
Data science AI/Ml basics to learn .pdf
deokhushi04
 
TCU EVALUATION FACULTY TCU Taguig City 1st Semester 2017-2018
MELJUN CORTES
 
624753984-Annex-A3-RPMS-Tool-for-Proficient-Teachers-SY-2024-2025.pdf
CristineGraceAcuyan
 
Communication_Skills_Class10_Visual.pptx
namanrastogi70555
 
english9quizw1-240228142338-e9bcf6fd.pptx
rossanthonytan130
 
Presentation by Tariq & Mohammed (1).pptx
AbooddSandoqaa
 
Udemy - data management Luisetto Mauro.docx
M. Luisetto Pharm.D.Spec. Pharmacology
 
PPT1_CB_VII_CS_Ch3_FunctionsandChartsinCalc.ppsx
animaroy81
 
Starbucks in the Indian market through its joint venture.
sales480687
 
Camuflaje Tipos Características Militar 2025.ppt
e58650738
 
MENU-DRIVEN PROGRAM ON ARUNACHAL PRADESH.pptx
manvi200807
 
ilide.info-tg-understanding-culture-society-and-politics-pr_127f984d2904c57ec...
jed P
 
Orchestrating Data Workloads With Airflow.pdf
ssuserae5511
 
Ad

Writing Recursive Queries

  • 1. WRITING RECURSIVE QUERIES RETRIEVING HIERARCHY DATA FROM RELATIONAL TABLES BEN LIS POSTGRESCONF US 2019
  • 2. IS THIS TALK RIGHT FOR YOU? What we will cover • Why we need recursive queries • How they work • How to write them What you should know • Basic knowledge of SQL through outer joins • General programming concepts • Slides and SQL: https://github.com/benjlis/talk-writing-recursive- queries 2
  • 3. ABOUT ME • Data Engineer at LEI Smart • Writes recursive queries on corporate hierarchy data • Initially found recursive SQL a bit confusing • Using PostgreSQL since 2015 • Started working with Oracle databases in 1988! • Adjunct Associate Faculty in Applied Analytics at Columbia University • Held various technical, product, and management roles on Wall Street • LinkedIn and Twitter 3
  • 4. AGENDA • Motivation • Understanding Recursive SQL • Writing Recursive Queries • Next Steps • Questions & Discussion 4
  • 5. MOTIVATION WHY WE NEED RECURSIVE SQL 5
  • 7. LET’S QUERY EMPLOYEES, THEIR BOSS & THE BOSS'S BOSS boss-queries.sql 7
  • 9. IDEA 1: EXTEND CURRENT APPROACH “Add a few more JOINs” Need to know the number of hierarchy levels in advance Not realistic or robust! 9
  • 10. IDEA 2: WRITE A PROGRAM Use Python, Java, PL/pgSQL, etc. Query all employees For each employee get their boss’s record and then their boss’s boss’s record, etc. until you get a NULL Complex and resource consumptive! 10
  • 11. SHOULDN’T WE BE ABLE TO WRITE THIS QUERY IN SQL? 11
  • 12. WE SHOULD BE ABLE TO WRITE THIS QUERY IN SQL • In the early days (decades) of SQL & relational databases we couldn’t • Writing a program (Idea 2) was the only solution • Other names for the hierarchy problem: • Transitive Closure • Bill of Materials • Parts Explosion • Good news: We can write this query in modern SQL using recursive SQL! 12
  • 13. UNDERSTANDING RECURSIVE SQL KEY IDEAS, CTE, SYNTAX, STRUCTURE & EVALUATION 13
  • 14. RECURSIVE SQL SUPPORT Part of ISO Standard SQL Introduced in SQL:1999 (aka SQL 3) Supported in PostgreSQL Oracle SQL Server DB2 14
  • 15. KEY IDEAS • re·cur·sive /rəˈkərsiv/: a program or routine of which a part requires the application of the whole, so that its explicit interpretation requires in general many successive executions • Self-referencing • Your Bosses = Your Manager + Your Manager’s Bosses • Recursive SQL is an iterative process • Think while loops • But SQL is declarative!!!! • Tip: Put aside knowledge of recursion in programming • Recursive SQL uses Common Table Expressions (CTEs) 15
  • 16. COMMON TABLE EXPRESSION (CTE) EXAMPLE cte.sql • Primarily used to simplify queries • Think of as inline view(s) 16
  • 17. RECURSIVE CTE STRUCTURE • Keyword: recursive • Always a UNION or UNION ALL of • Non-recursive select • Recursive select 17
  • 18. NON-RECURSIVE SELECT • Evaluated 1st - where we start • FROM clause: • Must not refer to <cte-name> • Refers to table containing hierarchy 18
  • 19. RECURSIVE SELECT • Evaluated 2..N times • FROM clause: • Must reference <cte-name> • Join of the <cte-name> and table containing hierarchy • N is a function of the data and the recursive select’s where clause 19
  • 20. RECURSIVE CTE EVALUATION QR = Query Results; WT = Working Table Both initially empty 1. Execute <non-recursive select> 2. Add results to QR and WT 3. Repeat until WT is empty a. Execute <recursive select> using WT data for <cte-name> b. Add results to QR c. Replace WT with results 20
  • 21. WRITING RECURSIVE QUERIES LET’S GET DOWN TO IT! 21
  • 22. WHAT IS ADAMS REPORTING LINE? rl.sql 22
  • 23. ALL EMPLOYEES FROM CEO TO ENTRY LEVEL oc.sql 23
  • 26. NEXT STEPS RECOMMENDED READINGS & BEYOND HIERARCHY 26
  • 27. RECOMMENDED READINGS • PostgreSQL documentation • Fun with SQL: Recursive CTEs in Postgres – Craig Kerstiens • Modern SQL in Open Source and Commercial Databases (slides 45-60) – Markus Winand • Exporting a Hierarchy in JSON: with recursive queries - Dmitri Fontaine • Joe Celko’s Trees and Hierarchies in SQL for Smarties (Book) 27
  • 28. BEYOND HIERARCHY • Hierarchy queries are the most common application of recursive SQL, but not the only application! • Pretty wild recursive SQL queries: • Solving the Traveling Salesman Problem with Postgres Recursive CTEs - Periscope Data • Mandelbrot Set • tic-tac-toe • My views: • Can doesn’t mean should • Worth studying for learning advanced techniques 28