---
title: SQL for fun and profit
url: "https://aaronfrancis.com/videos/sql-for-fun-and-profit"
path: "/videos/sql-for-fun-and-profit"
type: video
author: Aaron Francis
published: 2024-07-23
topic: Databases
keywords: [recursive cte, sqlite, common table expressions, gaps in sequences, orders table, data integrity, sql sequences, high performance sqlite]
youtube_url: "https://www.youtube.com/watch?v=sf_V8NC58gg"
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# SQL for fun and profit

## Chapters

- Recursive CTE in SQLite
- Orders Table Set-Up
- Find Missing Order using Recursive CTE
- When to Use Recursive CTE

## Transcript

**Aaron Francis:** I'm gonna show you how I used a recursive CTE in SQLite to find a missing order. So I think I must have missed a webhook or something, but somehow in my orders table, I missed an order. And so I wrote this recursive CTE to find all missing orders, and I think it's pretty cool. You might think that SQL is just for making friends or impressing potential suitors, and it is.

**Aaron Francis:** You will make tons of friends if you talk about SQL more, but it can also be used to solve business problems sometimes. Let me show you. I've stripped out some data here, but if we do select star from orders, we see this is basically the order table that I'm working with. Uh, you'll see I removed a ton of data. That was all personally identifiable information. So what we're left with are these order numbers and the product name.

**Aaron Francis:** Now, you'll notice something about the order numbers. These are generated externally by a service. These are generated by Lemon Squeezy, so these are not my order numbers. And I think it's kinda weird, honestly, the way these order numbers work. You'll see once it reaches 09, it actually goes to 10. And then once it reaches, uh, 099, it actually goes to 0100.

**Aaron Francis:** So it's not, uh, it's not what I would do. That's not a traditional way to generate an order number, but the reason that they do that is because of this. So if we pull this out here and we look at that, this is actually my store ID, and this is my order number. So when you hit 10, that's what it looks like, and when you hit 100, that's what it looks like.

**Aaron Francis:** So that's where those order numbers are being generated from. And, eh, that's a pretty good way to, to generate order numbers within a scope of a store without stomping over other potential tenants who are generating order numbers. Whatever, that's fine. But we need to find the missing order in this table. So there is an order that is missing in this table, and if we do select star from orders, order by order number descending, you'll see we have 741

**Aaron Francis:** orders, or we should have 741 orders. If you look down here, we have 740. And I'm not gonna go look for the missing one like a big idiot. I'm gonna use SQL to figure it out for me. So the first thing we're gonna do is with recursive, and this'll work in MySQL and Postgres as well. We just happen to be using SQLite.

**Aaron Francis:** Uh, with recursive all order numbers as. So this tells SQLite that this is a recursive CTE. The with tells it that it is a CTE, and then we made up a name. So the first thing I'm gonna do is do select, and I'm just gonna select, um, this order number right here.

**Aaron Francis:** So I'm gonna select the first one as order number, and I'm gonna select n. I'm gonna select 1 as n. And so that's gonna be the, the counter that we keep track of. And then we drop down here, and we say union all. So this is our first row, and now we're gonna say select. Um, we'll do select concat, select concat of our store number, which is this guy.

**Aaron Francis:** Select concat and n plus 1, and then we will select n plus 1. So we're creating a two-column table. One just has a running counter, and the other has the actual, um, order number that Lemon Squeezy generates on their side. And we're gonna select from the very table that we are building, which is one of the weird things about recursive CTEs.

**Aaron Francis:** Uh, you select from the table that you're building as you go, which is kinda wild. Where n is less than 741, 'cause that's how many orders that we have. And then we can say select star from all order numbers. And if we run that, you'll see there's order number, there's the n. We can order by n descending, and there is the final order number.

**Aaron Francis:** Now, what we're gonna do is we're gonna compare the orders table with this made-up table, and we're gonna join those together and look for missing results. Before we do that, I have to tell you, I have an entire course on SQLite at highperformancesqlite.com, where you learn this and much, much more. If you wanna learn more about SQLite, please go check out the course.

**Aaron Francis:** It's very good. I am biased, but it's very good. So if we were to say select star from all order numbers, that gives us our table. This is the universe of possibilities. Then it's easy. Whoop. We just join in what we have and see what is missing. So we will do left join orders on all order numbers.order number equal orders.

**Aaron Francis:** Nope. Orders.order number. So that should join everything in. Um, you'll see we've got the order number and the n, and then this is the orders table starting here all the way over, and that just sorted it. Hey, there's the one that we're missing. Um, so if we run that, uh, that gives us everything that we're looking for, and like we just saw, we want where orders.id is null.

**Aaron Francis:** So we left joined the universe of possibilities. We left joined the orders table in, and so anything that didn't have a matching order is gonna have orders id as null, which tells us that that is the missing order number. So now I can go to Lemon Squeezy and pull it down and put it in the database. Thank you, recursive CTEs.

**Aaron Francis:** This is really helpful in all situations where you have a sequence that shouldn't have gaps. So that could be numbers like we've done here. That could be dates. You know that every day of the year should exist. Even if you don't have data for that day of the year, you can generate a recursive CTE that shows all of the dates and then join in the data, and that way you don't have any gaps in your final table.

**Aaron Francis:** I think recursive CTEs are awesome. You will impress people if you talk about them. If you want to learn more, head to highperformancesqlite.com. Until the next time, see ya.

---

## Related on aaronfrancis.com

- [Finding missing orders with a recursive CTE](/writing/finding-missing-orders-with-a-recursive-cte) *(Article · 2024-09-22 · shared: recursive cte, sql sequences, sqlite)*
- [About](/about) *(Page · 2026-11-25 · shared: sqlite, high performance sqlite)*
- [Laravel's Big Day](/podcast/laravels-big-day-959237a1) *(Podcast · 2025-02-27 · shared: high performance sqlite, sqlite)*
- [Aaron Francis: Laravel Solo, Courses, Screen casting](/podcast/aaron-francis-laravel-solo-courses-screen-casting-e1626b9b) *(Podcast · 2024-12-20 · shared: high performance sqlite, sqlite)*
- [Rewriting SQLite from scratch (yes, really)](/videos/rewriting-sqlite-from-scratch-yes-really) *(Video · 2025-08-08 · shared: sqlite, data integrity)*

## 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)), `sqlite` ([S](/llms/keywords/s.txt)), `common table expressions` ([C](/llms/keywords/c.txt)), `gaps in sequences` ([F-G](/llms/keywords/f-g.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).
