Bird
0
0

Identify the error in the following SQL statement:

medium📝 Debug Q6 of 15
SQL - Set Operations
Identify the error in the following SQL statement:
SELECT id FROM table1 UNION SELECT id FROM table2 ORDER BY 2;
AMissing semicolon at the end
BUNION cannot be used with ORDER BY
CSELECT statements must have aliases
DORDER BY 2 is invalid because there is only one column
Step-by-Step Solution
Solution:
  1. Step 1: Analyze ORDER BY clause

    ORDER BY 2 means order by second column, but query selects only one column.

  2. Step 2: Confirm UNION and ORDER BY usage

    UNION with ORDER BY is valid if ORDER BY references existing columns.

  3. Final Answer:

    ORDER BY 2 is invalid because there is only one column -> Option D
  4. Quick Check:

    ORDER BY column index must exist [OK]
Quick Trick: ORDER BY column index must be within selected columns [OK]
Common Mistakes:
MISTAKES
  • Using ORDER BY column index out of range
  • Thinking UNION disallows ORDER BY
  • Ignoring column count in ORDER BY

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes