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: Dscott@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列能隐式转换为数字类型时,这两个查询语句才可以查询成功。