Here are the tables:
CREATE TABLE [dbo].[Classes](
[ClassId] [int] NOT NULL,
[ClassName] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Classes] PRIMARY KEY CL
Without having to add the group by
clause, you can do the following:
Create a function to get the students count:
go
CREATE FUNCTION [dbo].GetStudentsCountByClass(@classId int) RETURNS INT
AS BEGIN
declare @count as int
select @count = count(*) from STUDENTS
where ClassId = @classId
RETURN @count
END
then use it in your select
statement
SELECT * , dbo.GetStudentsCountByClass(ClassId) AS StudentsCount
FROM Classes