Becoming a SQL Guru with Recursive CTEs
Tags: sql, database, postgres
Published: 2020-12-31 18:01:17

---
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/bcaphqtayy6jhcqmx0u8.png
title: Becoming a SQL Guru with Recursive CTEs
published: true
date: 2020-12-30 20:08:22 UTC
tags: sql, databases, postgreSQL
canonical_url: https://blog.arctype.com/a-journey-down-recursive-ctes/
---
*Arctype writer: [Daniel Lifflander](http://dliff.com/)*
## Table of Contents
* [Intro to CTEs](#introduction)
* [Organizing Complex Queries](#example)
* [Unlocking the Power of CTEs with Recursion](#recursion)
* [Recursive CTEs in Action](#example-2)

## Intro to CTEs <a name="introduction"></a>
It's easy to get lost in a maze of subqueries, derived tables, and temporary tables when working with complex SQL queries. Postgres' common table expressions, or CTEs, are a lesser known feature, but they can be a useful structuring tool to craft a SQL query in a more readable and friendly way.

CTEs begin as a `with` statement and allow you to execute a query whose results will be available later to subsequent queries. If you have a query that relies on the results of another query, a reference of sorts, the CTE will allow Postgres to materialize the results of the reference query, ensuring that the query is only run once and its results are readily available to other queries farther down the line. (Note that this can be a double-edged sword, as it prevents the Postgres query planner from running optimization outside of the reference query’s SQL.)

The sequential list of "code blocks" created by CTEs adds readability to SQL scripts because every block is dependent on the ones above it. In contrast, it can be difficult to trace dependencies between derived tables and parse through nested subqueries (especially as their depth increases).

## Organizing Complex Queries <a name="introduction"></a>
**Derived Table Example**

![Derived Table](https://blog.arctype.com/content/images/2020/12/Screen-Shot-2020-12-30-at-11.59.46-AM.png)

**Temporary Table Example**   
_This will require two separate queries._

![Temporary Table](https://blog.arctype.com/content/images/2020/12/Screen-Shot-2020-12-30-at-12.00.53-PM.png)

**CTE Example**

![CTE](https://blog.arctype.com/content/images/2020/12/Screen-Shot-2020-12-30-at-12.01.23-PM.png)

Note that the query above depends on a, which is listed first with the CTE, but as a derived table it is listed inside the query. CTEs allow you to logically order queries that are dependent on others. As queries get more complex, **organization** becomes key to reducing errors. CTEs allow you to organize code into distinct blocks with a clear dependency hierarchy. 

## Unlocking the Power of CTEs with Recursion <a name="recursion"></a>
You can also make CTEs more than syntactic sugar by adding the `recursive` keyword! Once you add the recursive keyword, Postgres will now allow you to reference your CTE from within itself, allowing you to “generate” rows based on recursion.   
  
Here’s an example using Postgres CTEs to implement the Fibonacci sequence:

![A journey down recursive CTEs.](https://blog.arctype.com/content/images/2020/12/Screen-Shot-2020-12-30-at-12.02.00-PM.png)

Running this, we’d get the first 10:

![A journey down recursive CTEs.](https://blog.arctype.com/content/images/2020/12/Screen-Shot-2020-12-30-at-12.02.28-PM.png)

It's probably not too often we find ourselves in a situation where we need Fibonacci numbers in SQL. Let’s continue on and take a look at a real problem I solved using this Postgres feature.

## Recursive CTEs in Action <a name="example-2"></a>
In the manufacturing industry, a bill of materials (or BoM) is a list of the raw materials, sub-assemblies, intermediate assemblies, sub-components, parts, and the quantities of each needed to manufacture an [end product](https://en.wikipedia.org/wiki/Product_(business)) … BOMs are of [hierarchical nature](https://en.wikipedia.org/wiki/Directed_acyclic_graph), with the top level representing the finished product which may be a sub-assembly or a completed item. BOMs that describe the sub-assemblies are referred to as modular BOMs

More simply, a BoM is a list of parts that a product is assembled from. Each part may consist of other parts, creating hierarchy. A car is a final product that is composed of many parts. A door is an example of a part of a car, its subparts being among a window, handle, various switches, and so forth. Recursive CTEs are great for hierarchical data! Let’s take a look at how we could model this hierarchy and use CTEs to generate a modular BoM sheet.

**part table** stores our parts.

![A journey down recursive CTEs.](https://blog.arctype.com/content/images/2020/12/Screen-Shot-2020-12-30-at-12.03.50-PM.png)

**part\_hierarchy** table stores a simple parent to child reference between parts, as well as a quantity if the part happens to need more than 1 of the child part for assembly. The check constraints prevent a part from referencing itself circularly.

![A journey down recursive CTEs.](https://blog.arctype.com/content/images/2020/12/Screen-Shot-2020-12-30-at-12.04.24-PM.png)

Let's insert some parts

![A journey down recursive CTEs.](https://blog.arctype.com/content/images/2020/12/Screen-Shot-2020-12-30-at-12.04.46-PM.png)

And some relations, approximately resembling the car example mentioned above

![A journey down recursive CTEs.](https://blog.arctype.com/content/images/2020/12/Screen-Shot-2020-12-30-at-12.05.06-PM.png)

In our database we now have 5 parts and some relationships between them. If we wanted to see how many direct subassemblies a door has, we could query something like this, though it would only show us direct descendants:

![A journey down recursive CTEs.](https://blog.arctype.com/content/images/2020/12/Screen-Shot-2020-12-30-at-12.05.44-PM.png)

Finally, let's now construct a query to list _all_ of the parts and their subassemblies of our car.

![A journey down recursive CTEs.](https://blog.arctype.com/content/images/2020/12/Screen-Shot-2020-12-30-at-12.06.28-PM.png)

The union all is the key part to this CTE. The first part of this UNION selects the base part, the second half selects any child parts by joining to the part\_hierarchy table and calling itself (inducing recursion), then combines those results with the parent via the union. Without recursion, you'd have to add a join for every level of the hierarchy you needed to support.

The level value is introduced here to track how deep the recursion has run. It is hard coded at 0 to represent the parent. As the query "unwraps" and it calls itself, this increases by 1 to indicate how many generations far from the parent part the child part is.   
  
The very last part that selects from the CTE pads the name with spaces based on the level, which is a quick and dirty way to visualize the hierarchy.

![A journey down recursive CTEs.](https://blog.arctype.com/content/images/2020/12/Screen-Shot-2020-12-30-at-12.06.52-PM.png)

From here, you could use aggregate functions and rollup to calculate the costs of specific assemblies.   
  
This example is definitely simplified from what I ended up using to actually solve this problem. Supporting part versioning and branching and other business requirements quickly complicates what this solution would look like in the real world. Recursive CTEs in SQL are not the only way to store and query hierarchical data, but for certain data types, sizes, and speed requirements, they are a quick and convenient feature to have in your wheelhouse.

###Arctype
Now you're one step closer to becoming a SQL guru, but don't let an outdated SQL editor hold you back. Check out [Arctype](www.arctype.com) today and experience the modern SQL editor.