WITH RECURSIVE faktorial (n, f) AS (
     SELECT 0, 1
     UNION ALL
     SELECT n+1, (n+1)*f
     FROM faktorial
     WHERE n < 5
)
SELECT  n, f
FROM    faktorial;


WITH RECURSIVE hierarchie (id,jmeno,vedouci,uroven) AS (
     SELECT id, jmeno, vedouci,0
     FROM   zamestnanci
     WHERE  jmeno='Marie' -- OR vedouci IS NULL 
     UNION ALL
     SELECT z.id, z.jmeno, z.vedouci, hierarchie.uroven+1
     FROM   zamestnanci z
     JOIN   hierarchie ON hierarchie.id = z.vedouci
)
SELECT *
FROM   hierarchie;


WITH RECURSIVE potomci (pokoleni, id, jmeno) AS (
     SELECT 0, id, jmeno
     FROM sqltutor_data.premyslovci
     WHERE jmeno='Karel IV. Lucemburský'
     UNION ALL
     SELECT pokoleni+1, p.id, p.jmeno
     FROM   sqltutor_data.premyslovci AS p
     JOIN   potomci 
            ON potomci.id IN (p.otec, p.matka)
     where  pokoleni < 5
)
SELECT * FROM potomci;



WITH RECURSIVE predci (n, id, otec, matka, jmeno, rod) AS (
     SELECT 0, id, otec, matka, jmeno, rod
     FROM   sqltutor_data.premyslovci
     WHERE  jmeno='Václav'
     UNION ALL
     SELECT n+1, p.id, p.otec, p.matka, p.jmeno, p.rod
     FROM   sqltutor_data.premyslovci AS p
     JOIN   predci
            ON p.id IN (predci.otec, predci.matka)
     -- WHERE  n < 2 -- prarodice,
)
SELECT n, id, jmeno
FROM   predci;
