in Databases retagged by
396 views
2 votes
2 votes

Consider the following relation schema:

Student (RollNo, Sname, Scity)
Packages (Pcode, Pname, Price)   
Registers (RollNo PCode, Date)    

What does the following SQL query return?

Select distinct S.RollNo From Student S
Where NOT EXISTS ( (Select P.Pcode From Packages P) Minus
(Select R.Pcode From Registers R Where R.RollNo = S.RollNo))
  1. RollNos of students who did not register for any of the packages.
  2. RollNos of students who did not register for at least one package.
  3. RollNos of students who registered for at least one package.
  4. RollNos of students who registered for all the packages.
in Databases retagged by
by
396 views

2 Comments

edited by
@Bikram Sir, i have a doubt, in the inner query we are selecting pcode and in outer query we are comparing it with srollno, how can it be done ?
1
1

@Surabhi Kadur In this question that doesn't matter because when inner query "Except operation (-)" returns "null" then "NOT EXISTS" became "TRUE" and that srollno will be printed.

2
2

1 Answer

6 votes
6 votes
Best answer
Select distinct S.RollNo 
From Student S 
Where NOT EXISTS ( 
    (Select P.Pcode From Packages P)  
    Minus  
    (Select R.Pcode From Registers R Where R.RollNo = S.RollNo) 
)
(Select P.Pcode From Packages P)

This selects Pcode of all the packages.

(Select R.Pcode From Registers R Where R.RollNo = S.RollNo) 

This selects Pcode of packages that belong to that particular student.

Subtracting second from first will give, Pcode of packages that do not belong to that student. If this does not exist, then that student will be selected. We can say that a student is selected if there does not exist any package which does not belong to that student. 

Or, every package belongs to that student.

Or, student who registered for all the packages.

Option (D)

selected by
Answer:

Related questions