📜  门| GATE-CS-2015(模拟测试)|问题 17

📅  最后修改于: 2021-09-26 04:24:06             🧑  作者: Mango

考虑以下三个表来存储不同课程的学生注册。

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)非上述答案:(乙)
解释:

背景阅读:上述查询是嵌套查询的一个示例,即查询中的查询。首先解决内部查询,然后根据内部查询的结果解决外部查询。

  • 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 提供的。
这个问题的测验