in Databases edited by
1 flag 18,221 views
52 votes
52 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”
in Databases edited by
1 flag
18.2k views

4 Comments

As per the rule, Any Attribute that is present in the having clause without being aggregated must appear in the group by clause, otherwise query is erroneous

0
0
If suppose alias “s” was present in the querry, then option “d” would be correct right?

@Ishrat Jahan
0
0

 

 

If suppose alias “s” was present in the query, then option “d” would be correct right?
 

 here in this query there are more than one error! so for both the reasons the answer will be D.

 

0
0

5 Answers

76 votes
76 votes
Best answer

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
1 flag:
✌ Low quality (wizardx5)

4 Comments

for each school with more than 200 students in it, the name of the school and the number of 100s scored by its students.
Isn't this statement is correct instead of 200 students appearing in exam?

1
1

If GROUP BY sch-name is given

Output will be:

 There are two conditions. First, for each school more than 200 students in it [SELECT school-id FROM student GROUP BY school-id HAVING COUNT (*) > 200] AND Second, for each school display the count where R.marks=100 i.e count of 100s for each school.

So, answer would be (B) if every thing was correct.

2
2

All the attributes used in group by clause need to appear in the select clause.

Any Attribute that is not present in the group by clause must appear only inside the aggregate function in the select clause..

This means if other attribute(which are not present in group by clause) have to apeare on the select clause then it should with the aggregate function...

Am i right @pC sir...

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

4 Comments

If school-id was present in select clause along with school name would it generate an error?

@Arjun
0
0
answer would be B or C if the query ran successfully?
0
0
Actually such version-dependent questions shouldn’t be asked.
1
1
1 vote
1 vote
Answer is B as per key provided by IIT.

2 Comments

Before 2011 official keys are not released by gate organizing institute..
13
13
edited by
How you got key provided by IIT?. It would be B if GROUP BY sch-name is present instead of school-id.
1
1
0 votes
0 votes
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.

2 Comments

D.

simple logic alias of 's' not present in query

2
2

is it the neccessary condition?

0
0
Answer:

Related questions