考虑以下三个表格,以存储不同课程的学生入学人数。
Student(EnrollNo, Name)
Course(CourseID, Name)
EnrollMents(EnrollNo, CourseID)
以下查询做什么?
SELECT S.Name
FROM Student S, Course C, Enrollments E
WHERE S.EnrollNo = E.EnrollNo AND
C.Name = "DBMS" AND
E.CourseID = C.CourseID AND
S.EnrollNo IN
(SELECT S2.EnrollNo
FROM Student S2, Course C2, Enrollments E2
WHERE S2.EnrollNo = E2.EnrollNo AND
E2.CourseID = C2.CourseID
C2.Name = "OS")
(A)参加“ DBMS”或“ OS”课程的所有学生的姓名
(B)参加“ DBMS”和“ OS”的所有学生的姓名
(C)报名参加“ DBMS”或“ OS”或两者兼有的所有学生的姓名。
(D)以上都不是答案: (B)
解释:
背景知识:上面的查询是嵌套查询的示例,即查询中的查询。首先解决内部查询,然后根据内部查询的结果解决外部查询。
- WHERE IN返回与列表或子查询中的值匹配的值。
- WHERE IN是多个OR条件的简写。
Here, firstly the inner query is solved. It returns all the Enrollment
Numbers (SELECT S2.EnrollNo) of students where the students’ enrollment
number matches with the enrollment number of the courses
(WHERE S2.EnrollNo = E2.EnrollNo) which have the course IDs whose Course
Name is “OS” (E2.CourseID = C2.CourseID and C2.Name = “OS”).
因此,将为参加“ OS”课程的学生过滤掉所有的入学ID。
The outer query works similarly and filters out all the all tuples where
the Students Enrollment Number matches with the Enrollment Number where the
course ID’s are for the course names “DBMS”
(S.EnrollNo = E.EnrollNo AND C.Name =”DBMS” AND E.CourseID = C.CourseId) and
additionally matches with the ones that are returned by the inner query i.e.
Enrollment Number of students who are enrolled for the course “OS”.
因此,以上查询将返回已注册“ DBMS”和“ OS”两个课程的所有学生的姓名(SELECT S.Name)。
因此,选项(B)。
该解释由Yashika Arora提供。
这个问题的测验