OCP-1Z0-051-V9.02-122题

122. View the Exhibit for the structure of the STUDENT and FACULTY tables.

You need to display the faculty name followed by the number of students handled by the faculty at the

base location.

Examine the following two SQL statements:

Statement 1

SQL>SELECT faculty_name,COUNT(student_id)    

FROM student JOIN faculty    

USING (faculty_id, location_id)    

GROUP BY faculty_name;

Statement 2

SQL>SELECT faculty_name,COUNT(student_id)    

FROM student NATURAL JOIN faculty    

GROUP BY faculty_name;

Which statement is true regarding the outcome? 

A. Only s tatement 1 executes successfully and gives the required result.

B. Only statement 2 executes successfully and gives the required result.

C. Both statements 1 and 2 execute successfully and give different results.

D. Both statements  1 and 2 execute successfully and give the same required result.

Answer: D
答案解析:
实验验证:
1、首先创建这两张表

scott@TEST0924> create table student
  2  (student_id number(2) not null,
  3  student_name varchar2(20),
  4  faculty_id varchar2(2),
  5  location_id number(2)
  6  );

Table created.

 

scott@TEST0924> create table faculty
  2  (faculty_id number(2) not null,
  3  faculty_name varchar2(20),
  4  location_id number(2)
  5  );

Table created.

2、向这两张表中插入数据

scott@TEST0924> insert into student values(2,'zhansan3','20',11);

1 row created.

scott@TEST0924> insert into faculty values(20,'zhansan2',11);

1 row created.

3、执行查询语句,可成功执行

scott@TEST0924> SELECT faculty_name,COUNT(student_id) FROM student JOIN faculty
  2  USING (faculty_id, location_id) GROUP BY faculty_name;

FACULTY_NAME         COUNT(STUDENT_ID)
-------------------- -----------------
zhansan2                             1

scott@TEST0924> SELECT faculty_name,COUNT(student_id) FROM student NATURAL JOIN faculty
  2  GROUP BY faculty_name;

FACULTY_NAME         COUNT(STUDENT_ID)
-------------------- -----------------
zhansan2                             1

4,如果向student表中的faculty_id插入字符类。

scott@TEST0924> insert into student values(2,'zhansan3','a1',11);

1 row created.

 

5、查询失败

scott@TEST0924> SELECT faculty_name,COUNT(student_id) FROM student NATURAL JOIN faculty
  2  GROUP BY faculty_name;
SELECT faculty_name,COUNT(student_id) FROM student NATURAL JOIN faculty
*
ERROR at line 1:
ORA-01722: invalid number


scott@TEST0924> SELECT faculty_name,COUNT(student_id) FROM student JOIN faculty
  2  USING (faculty_id, location_id) GROUP BY faculty_name;
USING (faculty_id, location_id) GROUP BY faculty_name
       *
ERROR at line 2:
ORA-01722: invalid number

 

总结,当且仅当student表中的faculty_id列能隐式转换为数字类型时,这两个查询语句才可以查询成功。


因为NATURAL JOIN是将两个表 中具有相同名称的所有 列都连接起来,即FACULTY_ID和LOCATION_ID,而USING子句也使用了这两个字段来连接,故结果相同。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值