retagged by
465 views
2 votes
2 votes
Consider the label student (sid, sname, class, avg_marks).

Q1: Select sid from student S1
Where not exists
(Select sid from student S where S.class=5 and S1.avg_marks <= S.avg_marks)

Q2: Select sid from student S1
Where s1. avg_marks>Any
(Select distinct avg_marks from student S where S.class = “5”)

(Assume, class 5 has more than 1 student)

which option is correct?
A.Both Q1 and Q2 will produce same output
B. will show sid of students who has maximum avg_marks.
C. will show sid of student whose avg_marks is second highest.
D.None
retagged by

2 Answers

3 votes
3 votes

Let us take the given relation Student(sid, sname, class, avg_marks) with some tuples:

STUDENT
SID SNAME CLASS AVG_MARKS
1 Ajay 1 80
2 Adhi 2 90
3 Charan 5 60
4 Sneha 5 70
5 Sonu 5 75
6 Monu 3 71
7 Ram 4 65

 

 

Query 1 gives all the SIDs of the students who got avg_marks more than all of the students of class 5 irrespective of their classes

SID
1
2

 

Query 2 gives all the SIDs of the students who got avg_marks more than atleast one student of class 5 irrespective of their classes

SID
1
2
4
5
6
7

 

Option A is False as it is obvious from the produced outputs. Option B and C are are also False .

Option D must be True

0 votes
0 votes

Let's break down the two queries:

Q1: This query selects the sid from student where there does not exist a student in class 5 with an average mark higher or equal to the current student's average mark. In other words, it will return the sid of students whose avg_marks are higher than all students in class 5.

Q2: This query selects the sid from student where the student's avg_marks are greater than the average marks of any student in class 5. This means it will return the sid of students whose avg_marks are higher than at least one student in class 5.

Given these explanations, the correct answer is:

A. Both Q1 and Q2 will produce the same output.

This is because both queries are looking for students whose average marks are higher than those of at least one student in class 5. However, it's important to note that while Q1 is looking for students whose average marks are higher than all students in class 5, Q2 is looking for students whose average marks are higher than at least one student in class 5.

Related questions

198
views
1 answers
0 votes
465
views
1 answers
0 votes
tishhaagrawal asked Dec 16, 2023
465 views
My doubt here is, if NOT EXISTS gets an empty set as the input then every tuple of the table in the outer query must satisfy the condition. Am I right?For example, in the...
384
views
1 answers
2 votes
equimanthorn asked Sep 15, 2023
384 views
A SQL query is written in its format as clauses are arranged in a specific sequence and these clauses are executed in different sequence. If we’re writing a query using...
304
views
0 answers
1 votes
abhinowKatore asked Jan 17, 2023
304 views