0
0
SQLquery~10 mins

Recursive CTE concept in SQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a recursive CTE with the anchor member.

SQL
WITH RECURSIVE cte AS (SELECT [1] AS n)
Drag options to blanks, or click blank then click option'
A1
Bn
C*
DCOUNT(*)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a column name instead of a constant for the anchor member.
Using an aggregate function in the anchor member.
2fill in blank
medium

Complete the code to add the recursive member that increments the number by 1.

SQL
WITH RECURSIVE cte AS (SELECT 1 AS n UNION ALL SELECT n [1] 1 FROM cte WHERE n < 5)
Drag options to blanks, or click blank then click option'
A/
B+
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to reference the CTE in the recursive member.
3fill in blank
hard

Fix the error in the recursive CTE by completing the termination condition.

SQL
WITH RECURSIVE cte AS (SELECT 1 AS n UNION ALL SELECT n + 1 FROM cte WHERE n [1] 10) SELECT * FROM cte;
Drag options to blanks, or click blank then click option'
A<=
B>
C=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' which stops recursion too early.
Using '=' which only allows one iteration.
4fill in blank
hard

Fill both blanks to create a recursive CTE that generates even numbers up to 10.

SQL
WITH RECURSIVE evens AS (SELECT 2 AS num UNION ALL SELECT num [1] 2 FROM evens WHERE num [2] 10) SELECT * FROM evens;
Drag options to blanks, or click blank then click option'
A+
B-
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition.
Using a wrong comparison operator that excludes 10.
5fill in blank
hard

Fill all three blanks to create a recursive CTE that calculates factorial of numbers from 1 to 5.

SQL
WITH RECURSIVE factorial AS (SELECT 1 AS n, 1 AS fact UNION ALL SELECT n [1] 1, fact [2] n FROM factorial WHERE n [3] 5) SELECT * FROM factorial;
Drag options to blanks, or click blank then click option'
A+
B*
C<=
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition for n.
Using addition instead of multiplication for factorial.
Using wrong comparison operator that stops recursion too early.