暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

【每日一练 013】 SQL : set集合运算

原创 水清木华 2020-07-10
1066

问题
如下哪两个说法正确:
A.The expressions in the SELECT lists must match in number.
B.Parentheses may not be used to alter the sequence of execution.
C.The data type of each column in the second query must match the data type of its corresponding column in the first query.
D.The ORDER BY clause can be used only once in a compound query, unless a UNION ALL operator is used

解析:答案为A,C
对于集合运算,必须有相同个数的列,数据类型可以不用完全一样,但是要能够互相转换;
括号可以用来改变执行的顺序。
因此 A正确;B说括号可能不改变执行的顺序,因为没有说一定不能,所以保留B选项
;针对C,第二个查询中每个列的数据类型必须与第一个查询中对应列的数据类型匹配,测试发现数据类型还真是必须要一致,否则会报错,我认为C正确,因为我一时没想出反例,但与数据类型可以不用完全一样,但是要能够互相转换不符,所以我保留选C。例如
create table student(
student_id number,
student_name varchar2(10),
faculty_id varchar2(10),
location_id number);

create table faculty(
faculty_id number,
faculty_name varchar2(10),
location_id number);

SELECT faculty_id FROM student
union all
select faculty_id from faculty;

image.png
针对D,ORDER BY子句只能在复合查询中使用一次,除非使用UNION ALL运算符,错误,比如
SELECT employee_id, job_id, department_id
FROM employees order by employee_id
UNION all
SELECT employee_id, job_id, department_id
FROM job_history order by employee_id;
image.png

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论