Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
get number of students enrolled in the same course as a specific student

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?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!