Bird
0
0

Which of the following is the correct way to declare two CTEs named first_cte and second_cte in a single SQL query?

easy📝 Syntax Q3 of 15
SQL - Common Table Expressions (CTEs)
Which of the following is the correct way to declare two CTEs named first_cte and second_cte in a single SQL query?
AWITH first_cte AS (SELECT * FROM table1), second_cte AS (SELECT * FROM table2) SELECT * FROM second_cte;
BWITH first_cte (SELECT * FROM table1); WITH second_cte (SELECT * FROM table2); SELECT * FROM second_cte;
CWITH first_cte = (SELECT * FROM table1), second_cte = (SELECT * FROM table2) SELECT * FROM second_cte;
DWITH first_cte AS SELECT * FROM table1, second_cte AS SELECT * FROM table2 SELECT * FROM second_cte;
Step-by-Step Solution
Solution:
  1. Step 1: Understand CTE syntax

    Multiple CTEs are declared after a single WITH keyword, separated by commas.
  2. Step 2: Analyze each option

    WITH first_cte AS (SELECT * FROM table1), second_cte AS (SELECT * FROM table2) SELECT * FROM second_cte; correctly uses WITH once, defines both CTEs with AS and parentheses, and separates them by a comma.
  3. Step 3: Identify errors in other options

    WITH first_cte (SELECT * FROM table1); WITH second_cte (SELECT * FROM table2); SELECT * FROM second_cte; incorrectly uses multiple WITH keywords; WITH first_cte = (SELECT * FROM table1), second_cte = (SELECT * FROM table2) SELECT * FROM second_cte; uses '=' instead of AS; WITH first_cte AS SELECT * FROM table1, second_cte AS SELECT * FROM table2 SELECT * FROM second_cte; misses parentheses around SELECT statements.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Single WITH, commas separate CTEs, AS with parentheses [OK]
Quick Trick: Use one WITH, separate CTEs by commas [OK]
Common Mistakes:
  • Using multiple WITH keywords for multiple CTEs
  • Omitting parentheses around CTE SELECT statements
  • Using '=' instead of AS to define CTEs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes