---
title: Finding missing orders with a recursive CTE
url: "https://aaronfrancis.com/writing/finding-missing-orders-with-a-recursive-cte"
path: "/writing/finding-missing-orders-with-a-recursive-cte"
type: technical_article
author: Aaron Francis
published: 2024-09-22
updated: 2026-11-10
topic: Databases
description: Finding missing data in a sequence using recursive CTEs in SQL
keywords: [recursive cte, common table expression, finding missing data, gaps in sequence, sql sequences, left join, order numbers, sqlite, union all, anchor member, recursive member, hierarchical data]
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# Finding missing orders with a recursive CTE

Recently I had a customer write into support for our [SQLite course](https://highperformancesqlite.com), saying that
they paid but they still didn't have access. I looked in the database and, sure enough, their order wasn't in there.

I'm going to walk you through how I used a recursive CTE (Common Table Expression) in SQL to find the missing order.
It's an effective method for finding gaps in data, even when dealing with unconventional sequence patterns.

(Prefer to watch instead of read? Check out the video version of this article on
YouTube: [SQL for fun and profit](https://youtu.be/sf_V8NC58gg).)

## What is a recursive CTE?

A recursive CTE is a query that references itself, allowing for hierarchical or iterative data processing. It consists
of an initial condition and a recursive condition that refers back to the CTE.

It's like a loop in a programming language, but in SQL.

Here's the basic structure of a recursive CTE:

```sql
WITH RECURSIVE cte_name AS (
    -- Initial condition (base case, or anchor member)
    SELECT ...
    UNION ALL
    -- Recursive member
    SELECT ... FROM cte_name WHERE ...
)

SELECT * FROM cte_name;
```

The `cte_name` is the name of the recursive CTE. The recursive CTE consists of two parts:

1. **Anchor member**: This is the initial query that starts the recursion. It's the base case for the recursion.
2. **Recursive member**: This is the query that references the CTE itself. This is the loop.

The `UNION ALL` keyword is used to combine the results of the anchor and recursive members.

The `WHERE` clause in the recursive member is crucial because it defines the termination condition for the recursion.
When the `WHERE` clause evaluates to an empty result set, the recursion stops.

Now that we have a basic understanding of recursive CTEs, let's see how we can use them!

## Generating a sequence with recursive CTEs

Before we explore finding a missing order in the database, let's start with a simple example of a recursive CTE. In this
case, we'll use it to generate a sequence of numbers from 1 to 10.

```sql
WITH RECURSIVE numbers AS (
    -- Anchor member: Start with 1
    SELECT 1 AS num
    UNION ALL
    -- Recursive member: Increment num by 1 in each iteration
    SELECT num + 1
      FROM numbers -- Selecting from the CTE itself!
      WHERE num < 10 -- Termination condition: Stop when num reaches 10
)
-- Select all the numbers generated by the CTE
SELECT * FROM numbers;
```

Running this query will return the following sequence:

```text
1
2
3
4
5
6
7
8
9
10
```

This is a nice way to generate sequential data, either numbers or dates, traverse hierarchical or tree structures, or
perform cumulative calculations.

## The missing order

To the issue at hand: the missing order!

The order numbers are generated externally by our [payment processor](https://lemonsqueezy.com) so they don't follow the
traditional incremental order number pattern.

Instead, they're a combination of a store ID and an incremental number that resets in a somewhat unconventional way:

Our store ID is `89230`. The order numbers increment normally, but when you combine them it looks weird.

```text
The first order is: 892301  ("89230" + "1")
[...]
The ninth order is: 892309  ("89230" + "9")
The tenth order is: 8923010 ("89230" + "10")
```

So we went from 892,309 to 8,923,010. Kinda wonky.

This numbering system works well within the scope of a store, but it can make finding gaps in the sequence a bit tricky.
We can still use a recursive CTE to find the missing order though!

Here's a simplified version of the orders table for reference:

```sql
SELECT * FROM orders;

-- | id      | order_number | product_name         |
-- | ------- | ------------ | -------------------- |
-- | 2983419 | 892301       | SQLite: Early access |
-- | 2983469 | 892302       | SQLite: Early access |
-- | 2983584 | 892303       | SQLite: Early access |
-- | 2983858 | 892304       | SQLite: Early access |
-- | 2985444 | 892305       | SQLite: Early access |
-- | 2985462 | 892306       | SQLite: Early access |
-- | 2985483 | 892307       | SQLite: Early access |
-- | 2985485 | 892308       | SQLite: Early access |
-- | 2985521 | 892309       | SQLite: Early access |
-- | 2985325 | 8923010      | SQLite: Early access |
```

Based on the orders count from our payment processor, we should have 741 orders. However, the actual count in our
database shows only 740. Instead of manually hunting down the missing
order, we can use SQL to do the work for us.

## Find the missing order with recursive CTE

We're going to use a recursive CTE to generate a list of all possible order numbers, and then compare it against our
existing orders to find the gap.

1. Anchor member / initial condition: We start with the first order number, "892301". This part of the CTE is the base
   case for the
   recursion.

```sql
SELECT "892301" AS order_number, 1 AS n
```

2. Recursive member: We increment the order number by 1 in each iteration until we reach the last possible order
   number, 741.

```sql
UNION ALL
SELECT CONCAT("89230", n+1), n+1 
  FROM all_orders_numbers 
  WHERE n < 741
```

3. Putting it all together, we select the complete list of order numbers.

```sql
WITH RECURSIVE all_orders_numbers as (
    SELECT "892301" as order_number, 1 as n
    UNION ALL
    SELECT CONCAT("89230", n+1), n+1 
      FROM all_orders_numbers 
      WHERE n < 741
)

SELECT * FROM all_orders_numbers ORDER BY n DESC;

-- Partial output
-- | order_number | n   |
-- | ------------ | --- |
-- | 89230741     | 741 |
-- | 89230740     | 740 |
-- | 89230739     | 739 |
-- | 89230738     | 738 |
-- | 89230737     | 737 |
-- | 89230736     | 736 |
-- | 89230735     | 735 |
-- | 89230734     | 734 |
-- | 89230733     | 733 |
-- | 89230732     | 732 |
```

4. Find the missing order: Finally, we join the recursive CTE with the orders table and filter out the orders that
   exist. The missing order will be the one that doesn't have a corresponding entry in the orders table. We do this by
   performing a left join on the `order_number` column and filtering out the rows where `orders.id` is null.

```sql
WITH RECURSIVE all_orders_numbers AS (
    SELECT "892301" as order_number, 1 as n
    UNION ALL
    SELECT CONCAT("89230", n+1), n+1 FROM all_orders_numbers WHERE n < 741
)

SELECT * FROM all_orders_numbers
    LEFT JOIN orders ON all_orders_numbers.order_number = orders.order_number
    WHERE orders.id is NULL

-- | order_number | n   | id   |  order_number | product_name |
-- | ------------ | --- | ---- | ------------- | ------------ |
-- | 89230458     | 458 | NULL | NULL          | NULL         |
```

Boom. Order 89230458 is the missing order. It exists in the sequence but not in the database.

I can go back to my payment processor, retrieve the missing order, and insert it into my database. Problem solved, all
thanks to a recursive CTE!

## When to use recursive CTEs

Recursive CTEs are incredibly powerful for any scenario where you need to identify gaps in a sequence. While we used it
here to find a missing order number, the same technique can be applied to other types of sequences, like dates. For
example, if you need to ensure that every day of the year is accounted for in your data, a recursive CTE can generate a
complete list of dates, which you can then join with your data to spot any gaps.

Next time you encounter a missing sequence in your data, remember that you can use recursive CTEs! They're a handy tool
to have in your SQL toolkit for solving complex data problems.

## YouTube video

Check out the video version of this article on YouTube!

> Watch on YouTube: https://www.youtube.com/watch?v=sf_V8NC58gg

---

## Related on aaronfrancis.com

- [SQL for fun and profit](/videos/sql-for-fun-and-profit) *(Video · 2024-07-23 · shared: recursive cte, sqlite, sql sequences)*
- [About](/about) *(Page · 2026-11-25 · shared: sqlite)*
- [I built a Google Docs for me and Claude](/writing/i-built-a-google-docs-for-me-and-claude) *(Article · 2026-11-11 · shared: sqlite)*
- [Codex reverse engineered my studio lights](/videos/codex-reverse-engineered-my-studio-lights) *(Video · 2026-05-26 · shared: sqlite)*
- [Database School is live, and AI got really good](/newsletter/database-school-is-live-and-ai-got-really-good) *(Newsletter · 2025-12-05 · shared: sqlite)*

## Relevant Resources from Aaron Francis

- **[MySQL for Developers](https://databaseschool.com)** (overview on site: [/courses](/courses)): Full video course covering MySQL indexing, deferred joins, pagination, query plans, and schema design for application developers.
- **[High Performance SQLite](https://databaseschool.com)** (overview on site: [/courses](/courses)): Comprehensive video course on running SQLite in production: WAL mode, concurrency, FTS5, strict tables, backups, and performance tuning.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `recursive cte` ([Q-R](/llms/keywords/q-r.txt)), `common table expression` ([C](/llms/keywords/c.txt)), `finding missing data` ([F-G](/llms/keywords/f-g.txt)), `sql sequences` ([S](/llms/keywords/s.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
