SQL - Common Table Expressions (CTEs)Why does a recursive CTE require the UNION ALL operator instead of just UNION when processing hierarchical data?AUNION ALL preserves duplicates needed for recursion; UNION removes them causing incomplete resultsBUNION ALL sorts the results automatically; UNION does notCUNION ALL is faster because it uses indexes; UNION does notDUNION ALL is required only for non-recursive CTEsCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand difference between UNION and UNION ALLUNION removes duplicate rows, UNION ALL keeps all rows including duplicates.Step 2: Why duplicates matter in recursionRecursive CTEs rely on duplicates to continue building hierarchy; removing duplicates can stop recursion early.Final Answer:UNION ALL preserves duplicates needed for recursion; UNION removes them causing incomplete results -> Option AQuick Check:Recursive CTE needs UNION ALL to keep duplicates [OK]Quick Trick: Use UNION ALL to keep duplicates for recursion to work [OK]Common Mistakes:Thinking UNION ALL sorts resultsBelieving UNION ALL is faster due to indexesConfusing recursive and non-recursive CTE requirements
Master "Common Table Expressions (CTEs)" in SQL9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More SQL Quizzes Advanced Query Patterns - Finding gaps in sequences - Quiz 12easy CASE Expressions - CASE in SELECT for computed columns - Quiz 15hard Common Table Expressions (CTEs) - Why CTEs are needed - Quiz 6medium Database Design and Normalization - Database design best practices - Quiz 5medium Database Design and Normalization - Star schema concept - Quiz 2easy Indexes and Query Performance - Single column index - Quiz 14medium Stored Procedures and Functions - Variables and SET statements - Quiz 11easy Transactions and Data Integrity - Savepoints within transactions - Quiz 5medium Triggers - Trigger for audit logging - Quiz 5medium Triggers - DELETE trigger - Quiz 2easy