On January 21th at 14:15 UTC , progression through exercises will be unavailable for 10 minutes due to a planned maintenance break.
Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
The very basics
4. Recursion explained: computer science
Infinite looping
Processing graphs with recursive CTEs


In the world of computer science, recursion takes place when we invoke a block of instructions inside itself.

In SQL, a recursive CTE is a CTE that refers to itself. The idea behind recursive CTEs is usually as follows: 'Hey, give me everything you have so far, but add something extra as well. Then, repeat this procedure until I tell you to stop. By adding small pieces in each recursive step, I expect to finally arrive at the right result.'

Sound vague? Let's introduce our first example.


Let's say we want to show the numbers from 1 to 10 but we don't have table with rows containing those numbers.

Luckily, we can use recursion. Look at the template, run it, and see the result. Don't pay too much attention to the details – we'll explain everything in a second.