The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+21 votes

Consider the set of relations shown below and the SQL query that follows.

Students: (Roll_number, Name, Date_of_birth)

Courses: (Course_number, Course_name, Instructor)

Grades: (Roll_number, Course_number, Grade)

Select distinct Name
from Students, Courses, Grades
where Students.Roll_number=Grades.Roll_number
	and Courses.Instructor = 'Korth'
	and Courses.Course_number = Grades.Course_number
	and Grades.Grade = 'A'

Which of the following sets is computed by the above query?

  1. Names of students who have got an A grade in all courses taught by Korth
  2. Names of students who have got an A grade in all courses

  3. Names of students who have got an A grade in at least one of the courses taught by Korth

  4. None of the above

asked in Databases by Veteran (59.4k points) | 1.7k views

1 Answer

+23 votes
Best answer
C. Names of the students who have got an A grade in at least one of the courses taught by Korth.
answered by Veteran (342k points)
selected by
can someone explain why it is not A???
option a is said if person tuaght by all couses teach by korth then only slect but query slect if a person there who join course taught by korth need not all course.

In short ur case fail when studnt attend all couse taught by korth expect one... ( if korth taught n course then student join n-1 ).
how do we evaluate the predicate??all conditions are neccessary right??there is "and" between the conditions so all should be true.According to me the above query must select all tuples where korth is the instructor of the course and grade is A.May be i am not able to understand how predicate can be solved.Plz help.
yes, you are correct. And that is option C rt?
i am sorry bt i just can't understand how it is option c.
make any table with random input and for one student assign all korth subject and for remaining student assign one only korth ..u get the ans why not a is ans..

ans will be sid of studnt who register for any course of korth (one or more)
Also as distinct is used, whether student take single course of 10000, only one result will come out. So C is correct !

option a is telling us to select a student only if he gets an A grade in all the subjects taught by KORTH.

so student need to get A grade in all subjects taught by korth.

for eg.

suppose Korth teaches 3 subjects Dbms, OS and Algo and a student1 gets A grade in DBMS and OS but not in Algo. so student1 won't be selected if u choose option A.
but this is not correct.

If a student gets A grade in any sub. taught by Korth should be selected.
so the option is C

i have one question . please give a query for students who will be selected for getting grade 'A' in all the subjects taught by korth (so that it'd make sense)?

anyone plz ??

thanks in advance.
i think then you need to apply group by course instructor =korth having grades.grade=A

@khush No. Never use having unless we need a group property like MAX or a property of attribute used in group by. 

Select distinct Name
from Students
where Students.Roll_number NOT IN
    (Select Grades.Roll_number from Grades, Courses 
    where Grades.Course_number = Courses.Course_number
        and Courses.Instructor = 'Korth'
        and Grades.Grade != 'A');



thanx arjun sir, just clear my one more silly question :P :P (just to clarify),

1. if we have 10 tuples each in all the 3 relations , then the above two queries during its computation , produces an intermediate cartesian product, that has 1000 tuples and then applies these conditions right ?? 

2. is it anyway possible to see that  intermediate cartesian product result(using the db tools like sql fiddle), so that we could understand sql more clearly (because thats how we get confused between ("at least" , "any" and "all " keywords)??

thanks in advance.

1. yes, or do for 100 first apply conditions and then do next join. Query plan selection is a difficult problem and I do not remember the common strategies.

2. Sorry, no idea. I have seen some DBMS like MSSQL providing some intermediate results but as a novice it wasnt easy to use. May be someone expert in SQL can help.
thnx again
@Arjun  why not option a)?
^Because Korth can instruct more than one course, and nowhere in the query it says which one course. So it will take all the courses by Korth.

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

35,499 questions
42,768 answers
42,151 users