Recursive CTEs: follow a chain
A recursive CTE has an anchor and a recursive step joined with UNION ALL. It can walk a hierarchy or generate a short sequence. Always design a stopping condition and avoid cycles.
हिन्दी में समझें
रिकर्सिव CTE में शुरुआती हिस्सा और UNION ALL से दोहराया कदम होता है। इससे क्रम या पदानुक्रम बनता है। रुकने की शर्त रखें और चक्र से बचें।
अभ्यास: रिकर्सिव CTE से 1 से 5 तक पूर्णांक बढ़ते क्रम में दिखाएँ।
Hinglish mein samjhein
Recursive CTE mein anchor aur UNION ALL se recursive step hota hai. Sequence ya hierarchy walk kar sakte ho. Stop condition zaroor do, cycles avoid karo.
Practice: Recursive CTE se 1 se 5 integers ascending order mein banao.
A small SQL example
WITH RECURSIVE numbers(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM numbers WHERE n<3) SELECT n FROM numbers;Examples use the fictional shop database bundled with the SQL playground: customers, products, orders, order_items and employees. The engine is SQLite; other SQL dialects may differ.
Try this example in the SQL playground →Practise this idea
Generate integers 1 through 5 using a recursive CTE, returned in ascending order.
Check answer & explanation
WITH RECURSIVE numbers(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM numbers WHERE n<5) SELECT n FROM numbers ORDER BY n;The anchor starts at 1. The recursive step adds 1 while n < 5, producing the final 5 before recursion stops.
Stuck in the course? Choose Check answer for either the challenge or quiz. Read the explanation and continue to the next lesson at your own pace.
Check your understanding
A recursive query needs…
Check the quiz answer
A stopping condition
A recursive CTE has an anchor and a recursive step joined with UNION ALL. It can walk a hierarchy or generate a short sequence. Always design a stopping condition and avoid cycles.
References & corrections
Use the official SQLite documentation for full language rules. Read how these lessons are made and checked. Found a mistake? Report it to Excel Keys with this lesson URL and a reproducible example.