I am trying to get the number of students enrolled in courses via a single SQL statement, but not using sub-queries. So far I can only figure out how to do it using sub-queries. Is there another way?
Consider the following database setup:
create table student (id integer not null primary key); create table course_enrolment (student integer not null references student, course integer not null); insert into student values (1); insert into student values (2); insert into student values (3); insert into course_enrolment values (2,20); insert into course_enrolment values (2,30); insert into course_enrolment values (3,10);
I want to get the number of students enrolled in courses. In this case, it's 2.
I can achieve this easily using sub-queries:
SELECT COUNT(*) FROM (SELECT DISTINCT STUDENT FROM COURSE_ENROLMENT) AS DATA;
I want to get the count without using a sub-query.
I am using Postgresql 8.3 but am looking for a vendor agnostic solution.