Question
Download Solution PDFWhich of the following statement is correct about the following SQL query?
SELECT CourseName, Fees, StartDate, Duration FROM Students, Courses
WHERE Students.CourseID = Courses.CourseID
GROUP BY BookID
Answer (Detailed Solution Below)
Detailed Solution
Download Solution PDFThe correct answer is Option 1) It is a wrong query as attribute in the GROUP BY clause is not specified in the SELECT clause.
Key Points
- In SQL, when using the
GROUP BY
clause, all selected columns that are not aggregated must appear in theGROUP BY
clause. - In the given query:
SELECT CourseName, Fees, StartDate, Duration FROM Students, Courses WHERE Students.CourseID = Courses.CourseID GROUP BY BookID
BookID
is used in theGROUP BY
clause but is not present in theSELECT
list, and it is also not a part of any aggregation (likeCOUNT()
,SUM()
, etc.).- This violates the SQL standard rule and will result in an error in most RDBMS systems like MySQL, PostgreSQL, and SQL Server.
Additional Information
- Option 2 – It is a wrong query since is used in place of: This statement is unclear and grammatically incorrect; it doesn’t specify any real SQL issue.
- Option 3 – It is a wrong query as multiple tables are used in the SELECT clause: Not true. Joining multiple tables using WHERE is valid SQL.
- Option 4 – It is correctly formed query: ❌ Incorrect. The mismatch between SELECT and GROUP BY columns makes the query invalid.
Conclusion: The SQL query is incorrect because it uses GROUP BY on a column that is neither in the SELECT clause nor used in an aggregate function.
Last updated on Nov 25, 2024
-> BELTRON Programmer 2024 Notification has been released on the official website.
-> The Bihar State Electronics Development Corporation Limited (BELTRON) has announced a recruitment drive for Programmer positions on a contractual basis.
-> Specific vacancy details will be shared separately.
-> Interested candidates can apply online from November 11, 2024, to December 10, 2024.
-> The Minimum age of the candidates should be 21 years and maximum age should be 59 year of age.