edited by
1 flag 28,924 views
68 68 votes

Consider the following relational schema:

  • $\text{Student} (\underline{\text{school-id}, \text{sch-roll-no}}, \text{sname}, \text{saddress})$
  • $\text{School} (\underline{\text{school-id}}, \text{sch-name}, \text{sch-address}, \text{sch-phone})$
  • $\text{Enrolment}(\underline{\text{school-id}, \text{sch-roll-no}}, \text{erollno}, \text{examname})$
  • $\text{ExamResult}(\underline{\text{erollno}, \text{examname}}, \text{marks})$

What does the following SQL query output?

SELECT	sch-name, COUNT (*)
FROM	School C, Enrolment E, ExamResult R
WHERE	E.school-id = C.school-id
AND
E.examname = R.examname AND E.erollno = R.erollno
AND
R.marks = 100 AND E.school-id IN (SELECT school-id
                                FROM student
                                GROUP BY school-id
                                 HAVING COUNT (*) > 200)
GROUP By school-id
  1. for each school with more than $200$ students appearing in exams, the name of the school and the number of $100s$ scored by its students 

  2. for each school with more than $200$ students in it, the name of the school and the number of $100s$ scored by its students

  3. for each school with more than $200$ students in it, the name of the school and the number of its students scoring $100$ in at least one exam 

  4. nothing; the query has a syntax error

  • 🚩 Edit necessary | 👮 Arjun | 💬 “better answer needed”

7 Answers

Best answer
97 97 votes

Correct Answer: D

If Select clause consist aggregate and non - aggregate columns. All non aggregate columns in the Select clause must appear in Group By clause. But in this query Group by clause consists of school-id instead of school-name

http://weblogs.sqlteam.com/jeffs/archive/2007/07/20/but-why-must-that-column-be-contained-in-an-aggregate.aspx

https://dba.stackexchange.com/questions/319724/sqlite-how-does-count-work-without-group-by

edited by
7 7 votes
4 it has a syntax error coz u cannot select non aggregate attribute with aggregate function
4 4 votes

D

there are two reasons for syntax error:-

  1. aliasing S cannot be found in the from clause
  2. attribute present in group by clause is not appering in select clause
1 1 vote
Answer is B as per key provided by IIT.
1 flag:
✌ Spam (Sameer Bawane “answer provided by IIT was D itself”)
1 1 vote
All the non-group expressions(non-aggregate functions) that exist in the select clause along with group functions(aggregate functions) must and should be present in "Group By " Clause but it is not vice-versa.

Therefore, option is D.
1 1 vote

If aggregated attribute is used in the select clause then it can have only those unaggregated attributes which are in group by clause. Here the group by clause uses school-id and select clause has sch-name as unaggregated attribute hence this syntax is not correct.

https://gateoverflow.in/47/gate-cse-2012-question-15 this gate pyq is also based on the same concept.

edited by
Answer:
Position:
Show:

Related questions

70 70 votes
10 answers 10 answers
26.0k
26.0k views
Ishrat Jahan asked Oct 29, 2014
26,049 views
Consider the following relational schema:$\text{Student} (\underline{\text{school-id}, \text{sch-roll-no}}, \text{sname}, \text{saddress})$$\text{School} (\underline{\tex...
64 64 votes
10 answers 10 answers
27.3k
27.3k views
Ishrat Jahan asked Oct 28, 2014
27,300 views
Consider the following three schedules of transactions T1, T2 and T3. [Notation: In the following NYO represents the action Y (R for read, W for write) performed by trans...
39 39 votes
7 answers 7 answers
15.1k
15.1k views
Ishrat Jahan asked Oct 28, 2014
15,147 views
Let $R (A, B, C, D, E, P, G)$ be a relational schema in which the following functional depen­dencies are known to hold: $AB \to CD, DE \to P, C \to E, P \to C$ and $B \to...
138 138 votes
5 answers 5 answers
50.9k
50.9k views
Ishrat Jahan asked Oct 28, 2014
50,882 views
Let $R (A, B, C, D)$ be a relational schema with the following functional dependencies :$A → B$, $B → C$, $C → D$ and $D → B$. The decomposition of $R$ into $(A, B), (B, ...