Why can't the last sc_2=2, in the subquery be replaced with sc=2?

query the student numbers and names of students who have studied "001" and also learned the course number "002";

select student.sid,student.sname from student,sc
where student.sid=sc.sid and sc.cid=1 and exists
(select * from sc as sc_2 where sc.sid=sc_2.sid and sc_2.cid=2)

Why does the last part of the subquery section have to be sc_2.cid=2, and empty with sc.cid=2?
thanks for your anser.

Mar.28,2021

sc.cid has been limited to 1 . sc_2 you can think of it as exactly the same table as sc , but not sc . The subquery select * from table1 where [exist] subquery traverses the table table1 to see if it can pass the subquery, and if so, the current row meets the condition.
assume that the sc table saves the selection of students who have taken odd-numbered courses (001003), and the sc_2 table saves the selections of students who have taken even-numbered courses (002004.)

select student.sid,student.sname from student,sc
where student.sid=sc.sid and sc.cid=1 and exists (select * from sc_2 where sc.sid=sc_2.sid and sc_2.cid=2)

this makes it clearer.

take a look at the usage of the exists subquery , Chinese translation .

Menu