Total Enrolled Students in a course

Total Enrolled Students in a course

by Malini Lakshmanan -
Number of replies: 2

I am new to Moodle. I try to get the total number of enrolled students in a particular course. I am using the below query I got from community

SELECT c.fullname AS 'Course Name'

,COUNT(u.username) AS '# of Users'

 FROM mdl_role_assignments AS r

   JOIN mdl_user AS u on r.userid = u.id

   JOIN mdl_role AS rn on r.roleid = rn.id

   JOIN mdl_context AS ctx on r.contextid = ctx.id

   JOIN mdl_course AS c on ctx.instanceid = c.id

 WHERE rn.shortname = 'student' and c.idnumber like "%202110%"

 GROUP BY c.fullname;

But the numbers I got differs greatly from the front end admin site. Where am I going wrong? 



Average of ratings: -
In reply to Malini Lakshmanan

Re: Total Enrolled Students in a course

by Rick Jerz -
Picture of Particularly helpful Moodlers Picture of Testers
Don't know. 😖

I use this query in my Moodle.  You can apply more filters for any special situations the require further refinement.

# Counts all users in a course, no filtering
SELECT c.fullname AS Course,
       c.idnumber AS Course_number,
       c.id AS CourseID,
       COUNT(ue.id) AS Participants
FROM prefix_course AS c
JOIN prefix_enrol AS en ON en.courseid = c.id
JOIN prefix_user_enrolments AS ue ON ue.enrolid = en.id
GROUP BY c.id
ORDER BY c.idnumber DESC
Average of ratings: Useful (1)