Loading topic
SELECT queries with WHERE, ORDER BY, joins and self-joins, and that you have used a plain WITH clause. It covers the parts of a recursive common table expression (CTE) and the rules that tie them together. The next chapter covers how the database runs them.7 min readWITH query, self-join a table, and read a UNION ALL. You do not need to have written a recursive query before. The goal is to learn what the database does at each step, so you can predict a recursive query's output and cost before you run it.7 min readWITH clause (a common table expression, or CTE) and read the anchor, UNION ALL, and recursive member layout from the previous chapter. You should also be comfortable with SELECT, WHERE, LEFT JOIN, and GROUP BY. Here you will build recursive quer6 min readWITH RECURSIVE. This chapter assumes you know the anatomy of a recursive CTE (anchor member, recursive member, UNION ALL) and the working-table model from earlier chapters. It also assumes you can read SELECT, `7 min readUNION ALL) and know that the database builds the result one iteration at a time. The examples use PostgreSQL. The dialect notes show what changes elsewhere.7 min readUNION ALL, and a termination condition. It also assumes you have tracked depth and path columns through a tree. You should be comfortable with self-joins and with a table that stores one6 min readGROUP BY with aggregates, and read a basic WITH RECURSIVE query (an anchor member, a recursive member, and UNION ALL). If the working table is unfamiliar, review the chapter on how the database evaluates a recursive CTE first.5 min readUNION ALL). It also assumes you know how the working table drives each iteration, and that you are comfortable with self-joins and GROUP BY. Here you will learn what changes when the two members are joined6 min readUNION ALL) and that you know how a query terminates when the recursive member returns no new rows. It covers where the four most common engines differ and how to adjust a query when you move it between them.6 min readWITH RECURSIVE query with an anchor member and a recursive member, and that you know how the working table is refilled on each iteration. It also assumes you are comfortable with joins, GROUP BY, and subqueries. The examples run on PostgreSQL and SQLite6 min readEXPLAIN. Here you will learn why recursive queries get slow, which indexes help, and 7 min readUNION ALL and cycle-guard patterns from the earlier chapters. Recursive CTEs are a good default for hierarchies and graphs, but they are not always the best tool. This chap6 min read