155. Evaluate the following SQL statement:
SQL> SELECT promo_id, promo_category
FROM promotions
WHERE promo_category = 'Internet'
ORDER BY 2 DESC
UNION
SELECT promo_id, promo_category
FROM promotions
WHERE promo_category = 'TV'
UNION
SELECT promo_id, promo_category
FROM promotions
WHERE promo_category ='Radio';
Which statement is true regarding the outcome of the above query?
A. It executes successfully and displays rows in the descending order of PROMO_CATEGORY.
B. It produces an error because positional notation cannot be used in the ORDER BY clause with SET
operators.
C. It executes successfully but ignores the ORDER BY clause because it is not located at the end of the
compound statement.
D. It produces an error because the ORDER BY clause should appear only at the end of a compound
query-that is, with the last SELECT statement. ORDER BY必须要在句子最后
Answer: D
答案解析:
ORDER BY 子句应该放在复合sql查询语句的最后。
sh@TEST0924> SELECT promo_id, promo_category
2 FROM promotions
3 WHERE promo_category = 'Internet' ORDER BY 2 DESC
4 UNION
5 SELECT promo_id, promo_category
6 FROM promotions
7 WHERE promo_category = 'TV'
8 UNION
9 SELECT promo_id, promo_category
10 FROM promotions
11 WHERE promo_category ='Radio';
UNION
*
ERROR at line 4:
ORA-00933: SQL command not properly ended
正确的是:
sh@TEST0924> SELECT promo_id, promo_category
2 FROM promotions
3 WHERE promo_category = 'Internet'
4 UNION
5 SELECT promo_id, promo_category
6 FROM promotions
7 WHERE promo_category = 'TV'
8 UNION
9 SELECT promo_id, promo_category
10 FROM promotions
11 WHERE promo_category ='Radio'
12 ORDER BY 2 DESC;
PROMO_ID PROMO_CATEGORY
---------- ------------------------------
35 TV
42 TV
47 TV
49 TV
54 TV
...
532 TV
534 TV
115 rows selected.