
277
sQL
9.10.2 Subquery as an Alternative to GROUP BY
A subquery can be used as an alternative to the GROUP BY clause. For example, the
query in Listing9.23 is to show each student’s name along with the number of courses
she/ he has taken using a subquery.
Listing9.23: Subquery for Groups
SELECT tblStudent.StudentName,
(SELECT COUNT(*)
FROM tblGrading
WHERE tblStudent.StudentID=tblGrading.StudentID)
AS NumberOfCourses
FROM tblStudent;
e use of subquery for groups could cause confusion if the design of the subquery
is incorrect. For the beginner, the GROUP BY clause would be better than this type
of subquery.
9.10.3 Subquery—Determining an U