SQLite Forum

short circuiting subqueries with `IN`?
Login
Also note that if your data might have loops then you need to change the `UNION ALL` to `UNION` in order to prevent getting stuck in a loop.