Excellent point! We don't even need the outer loop, let's keep running the query in second loop and when it returns an empty set we'd know we're done.

I'm saying that because `total` can be changed during the run.