I have two sql queries
select course from enrolments where student = 101;
select count(student) as course from enrolments
GROUP by course
The first produces a list of course where student with id 101 is enrolled The second return a total count of students enrolled in each course
How do I get the total enrolments for the courses student 101 is enrolled in?
Use bool_or() in having to ensure at least one of the students is 101 in the group:
select count(student) as course
from enrolments
group by course
having bool_or(student = 101)
For a less PostgreSQL-specific solution, you would need to use a JOIN (or EXISTS). Something like this:
select count(student) as course
from enrolments e
where exists(select 1
from enrolments s
where s.course = e.course
and s.student = 101)
group by course
I originally misunderstood the question. Here is an approach that uses two levels of aggregation:
select sum(numstudents)
from (select course, count(*) as numstudents
from enrolments
group by course
having sum( (student = 1)::int) > 0
) c;
The subquery gets the value per course.
It looks like you just have to add 'where' condition to your second query:
SELECT
count(student) as course
FROM
enrolments
WHERE
student = 101
GROUP by course;