+------+-------+------------+ | 1 | 0 | 1 | | 2 | 1 | 1 | | 3 | 1 | 2 | | 4 | 2 | 3 | | 5 | 3 | 5 | | 6 | 5 | 8 | | 7 | 8 | 13 | | 8 | 13 | 21 | | 9 | 21 | 34 | | 10 | 34 | 55 | +------+-------+------------+ 

How the CTE works: 

• n  is a display column to indicate that the row contains the n -th Fibonacci number. For example, the 8th Fibonacci number is 13. 

•The fib_n  column displays Fibonacci number n . 

•The next_fib_n  column displays the next Fibonacci number after number n . This column provides the next series value to the next row, so that row can produce the sum of the two previous series values in its fib_n  column. 

•Recursion ends when n  reaches 10. This is an arbitrary choice, to limit the output to a small set of rows. 

The preceding output shows the entire CTE result. To select just part of it, add an appropriate WHERE clause to the top-level SELECT . For example, to select the 8th Fibonacci number, do this: 

## Date Series Generation 

mysql> WITH RECURSIVE fibonacci ... ... SELECT fib_n FROM fibonacci WHERE n = 8; +-------+ | fib_n | +-------+ | 13 | +-------+ 

A common table expression can generate a series of successive dates, which is useful for generating summaries that include a row for all dates in the series, including dates not represented in the summarized data. 

Suppose that a table of sales numbers contains these rows: 

mysql> SELECT * FROM sales ORDER BY date, price; +------------+--------+ | date | price | +------------+--------+ | 2017-01-03 | 100.00 | | 2017-01-03 | 200.00 | | 2017-01-06 | 50.00 | | 2017-01-08 | 10.00 | | 2017-01-08 | 20.00 | | 2017-01-08 | 150.00 | | 2017-01-10 | 5.00 | +------------+--------+ 

This query summarizes the sales per day: 

mysql> SELECT date, SUM(price) AS sum_price 

