WITH and then query. Adding RECURSIVE lets the definition refer to itself.UNION or UNION ALL: the anchor (a normal query that never mentions the CTE) and the recursive part (a query that reads the CTE).SELECT * FROM team) reads the finished result.UNION ALL keeps adding the same cities forever. UNION throws away rows that are already in the result, so the loop dies out. A depth limit such as WHERE level < 5 is the other common guard.UNION, rows are compared on every column (so adding a level column can stop it removing repeats). Row order is not guaranteed by SQL; the demo lists rows in the order they are found.RECURSIVE; PostgreSQL, MySQL 8 and SQLite require it.WHERE that finally returns nothing.| Space | Play / pause |
| → ← | Next / previous step |
| Home End | Back to start / skip to end |
| 1 – 3 | Count / org chart / flight routes |
| U | Switch UNION ALL / UNION |
| ? | This help |
Created by