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 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 for each school with more than $200$ students in it, the name of the school and the number of $100s$ scored by its students 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 nothing; the query has a syntax error Databases gateit-2008 databases sql normal + – 🚩 Edit necessary | 👮 Arjun | 💬 “better answer needed” Ishrat Jahan 28.9k views answer comment Share Follow Print See all 9 Comments 9 9 Comments reply Show 6 previous comments akshay_123 commented Aug 23, 2025 reply Follow flag https://youtu.be/esSG9AnMiSc?si=nMMcKWAafcRgxjlE best explanation 1 1 replyShare Tushar Rana commented Dec 17, 2025 reply Follow flag why option b is incorrect, all logical flow is valid here. @Deepak Poonia sir check this 1 1 replyShare Ronit_Balouria commented Aug 22 reply Follow flag If we think logically, like school id can be mapped to school name, cuz each school name has a unique school id in the best case scenario, but there could be two schools with same names and different school id because sch-name id not the primary key if we group by school id and try to display school name it should work(logically speaking) because each school id may have a school name, but what if it was other way around like group by school name and select school id, there maybe conflicts and errors cause two schools could have same names but different school ids, so it cant be displayed thats why the query looks like it will work but in theory it wont!! @Tushar Rana 0 0 replyShare Please log in or register to add a comment.
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 erravi90 answered Dec 25, 2014 • edited Jun 22, 2018 by Milicevic3306 erravi90 comment Share Follow See all 20 Comments 20 20 Comments reply pC commented Sep 9, 2016 reply Follow flag 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 S.school-id IN (SELECT school-id FROM student GROUP BY school-id HAVING COUNT (*) > 200) GROUP By school-id All non aggregate coloums in SELECT caluse must appear in GROUP BY Caluse 19 19 replyShare vijaycs commented Sep 11, 2016 reply Follow flag here ans D is okay, but can anyone tell me what does internal query returns ?? or please explain, what if it was select school-id, count(*) in the begning ... 3 3 replyShare Priya Brata commented Sep 13, 2016 reply Follow flag Then answer should be c. 12 12 replyShare gate-17 commented Nov 12, 2016 reply Follow flag @pc boss here what is mean by non aggregate coloums ? 0 0 replyShare Daddy commented Jan 31, 2017 reply Follow flag every SQL query terminates by a semicolon. Isn't it reason for sytax error?? @pc I read in an answer by arjun sir that All non aggregate coloums in SELECT caluse must appear in GROUP BY Caluse is not necessary now. It's given in books but practically not necessary 2 2 replyShare sushmita commented Feb 1, 2017 reply Follow flag give the link.. 0 0 replyShare Madhab commented Feb 3, 2017 reply Follow flag Aggergate collums comes from the word aggregate functions such as avg(average),min,max,sum here when one of these collums present in select clause these need not be in group by clause. 0 0 replyShare Prashant. commented Apr 6, 2017 reply Follow flag 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 S.school-id IN (SELECT school-id FROM student GROUP BY school-id HAVING COUNT (*) > 200) GROUP By school-name then answer will be 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. 31 31 replyShare rishi71662data4 commented Nov 23, 2017 reply Follow flag http://searchsqlserver.techtarget.com/answer/ISO-ANSI-SQL-and-the-GROUP-BY-clause Expressions in the GROUP BY clause can contain columns of the tables, derived tables or views in the FROM clause. The columns are not required to appear in the SELECT clause <select> list. Each table or view column in any nonaggregate expression in the <select> list must be included in the GROUP BY list: Not all attributes used in the GROUP BY clause need to appear in the SELECT clause Discussion: https://gateoverflow.in/47/gate2012_15 0 0 replyShare mehul vaidya commented Jul 7, 2018 reply Follow flag @prashant I don't think that is correct answer (SELECT school-id FROM student GROUP BY school-id HAVING COUNT (*) > 200) don't check schools with " school with more than 200 students appearing in exams" It just check no of students must be greater than 200. Also each student can enroll in multiple schools and courses (as no primary key constraint given for student school-id, sch-roll-no -> can be like(1,1) and (2,1) i.e. student with id 1 is with school 1 and 2.) (he can enroll for multiple courses because , as no PK constraints given Enrolment(school-id sch-roll-no, erollno) -> can be (1,1,1) (1,1,2) (2,1,1) i.e. (1,1,1) -> student with sch-roll-no =1 which is with school-id =1 enrolled for erollno=1 (1,1,2) -> student with sch-roll-no =1 which is with school-id =1 enrolled for erollno=2 (2,1,1) -> student with sch-roll-no =1 which is with school-id =2 enrolled for erollno=1 Hence correct answer would be 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 . Please reply and correct me if my answer is wrong also did you noticed apart from mistake in group by clause there is S.school-id S is not alias of anything 3 3 replyShare divanshu commented Sep 4, 2018 reply Follow flag Answer is B as per key provided by IIT. 2 2 replyShare shubham0109 commented Nov 2, 2018 reply Follow flag Yes, exactly. S is not an alias of anything. Doesn't that also makes the query syntax wrong? 0 0 replyShare Abhisek Tiwari 4 commented Nov 15, 2018 i edited by Abhisek Tiwari 4 Nov 16, 2018 reply Follow flag Can u plz elaborate these terms? Group by X X can be super set of non aggregate attribute? or exactly all non aggregate attribute?? Aggregate and non aggregate column? 0 0 replyShare Punit Sharma commented Aug 13, 2019 reply Follow flag @mehul yeah that's a valid point...these will also be one of the reasons for error .. 0 0 replyShare scholaraniket commented Oct 23, 2019 reply Follow flag 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? 2 2 replyShare Gaurav Yadav commented May 19, 2020 reply Follow flag 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. 3 3 replyShare satyaAchar commented Feb 3, 2024 reply Follow flag 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... 2 2 replyShare halfcodeblood commented Aug 20, 2024 reply Follow flag @satyaAchar yes you are right. Note- "If we use atleast one aggregate function (in select clause) then "No Unaggregate attribute allowed"(unless there is a group by clause). 0 0 replyShare Anshima_Singh commented Jan 7, 2025 reply Follow flag But their should be at leat 100 in one exam 0 0 replyShare Ronit_Balouria commented Aug 22 reply Follow flag All the attributes used in group by clause need to appear in the select clause.this statement is false @satyaAchar 0 0 replyShare Please log in or register to add a comment.
7 7 votes 4 it has a syntax error coz u cannot select non aggregate attribute with aggregate function Shreyans Dhankhar answered Oct 29, 2014 Shreyans Dhankhar comment Share Follow See all 14 Comments 14 14 Comments reply Show 11 previous comments Abhineet Singh commented Nov 25, 2020 reply Follow flag answer would be B or C if the query ran successfully? 1 1 replyShare ankit3009 commented Dec 14, 2021 reply Follow flag Actually such version-dependent questions shouldn’t be asked. 4 4 replyShare Anshima_Singh commented Jan 7, 2025 reply Follow flag How do you know its version dependent 0 0 replyShare Please log in or register to add a comment.
4 4 votes D there are two reasons for syntax error:- aliasing S cannot be found in the from clause attribute present in group by clause is not appering in select clause PRIYANSHU10 answered Jun 28, 2021 PRIYANSHU10 comment Share Follow See 1 comment 1 1 comment reply morechaitanya60 commented Jan 15, 2025 1 flag: ✌ Edit necessary (P0535_Yedidyah_Sagar “Answer is D”) reply Follow flag The query essentially performs the following steps:Joins the School, Enrolment, and ExamResult tables based on matching school-id, examname, and erollno.Filters the results to include only rows where the marks is 100.Uses a subquery to filter school-id values that have more than 200 students enrolled (COUNT (*) > 200).Groups the final results by school-id and selects the school name and the count of 100s scored by its students.Given this understanding, let's evaluate the multiple-choice options:Option A: 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.This option closely matches the logic of the query.Option B: for each school with more than 200 students in it, the name of the school and the number of 100s scored by its students.This option does not match as it doesn't specifically state that the students are appearing in exams.Option C: 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.This option doesn't fully match the logic as it mentions the number of its students scoring 100, not just the count of 100s scored.Option D: nothing; the query has a syntax error.This option is incorrect as the query has no syntax errors.So, the best match is Option A: 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. 0 0 replyShare Please log in or register to add a comment.
1 1 vote Answer is B as per key provided by IIT. divanshu answered Sep 4, 2018 1 flag: ✌ Spam (Sameer Bawane “answer provided by IIT was D itself”) divanshu comment Share Follow See all 2 Comments 2 2 Comments reply Verma Ashish commented Oct 29, 2018 reply Follow flag Before 2011 official keys are not released by gate organizing institute.. 16 16 replyShare Gaurav Yadav commented May 19, 2020 i edited by Gaurav Yadav May 19, 2020 reply Follow flag How you got key provided by IIT?. It would be B if GROUP BY sch-name is present instead of school-id. 1 1 replyShare Please log in or register to add a comment.
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. Naseer answered Jan 14, 2020 Naseer comment Share Follow See all 2 Comments 2 2 Comments reply fenil_2022 commented Jul 1, 2020 reply Follow flag D. simple logic alias of 's' not present in query 3 3 replyShare Pranavpurkar commented Oct 30, 2021 reply Follow flag fenil_2022 is it the neccessary condition? 0 0 replyShare Please log in or register to add a comment.
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. Madhav107 answered Jul 9, 2025 • edited Jul 9, 2025 by Madhav107 Madhav107 comment Share Follow 0 reply Please log in or register to add a comment.