SQL for fun and profit
Recursive CTEs (common table expressions) are really powerful, and actually useful! In this video, I demonstrate how to use a recursive CTE in SQLite to identify a missing order from an orders table, showcasing a practical solution to a common business problem. This technique is not only powerful for maintaining data integrity but also impressively versatile for various scenarios, such as filling gaps in sequences like dates or numbers.
Everything I said
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.
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.
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.
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.
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.