 SELECT DISTINCT c.id, c.fullname, c.shortname, ctx.id AS contextid,
                                       (SELECT COUNT(userid) FROM {role_assignments}
                                         WHERE contextid = asg.contextid GROUP BY contextid) AS alunos
                                 FROM {role_assignments} asg
                                 JOIN {context}          ctx ON asg.contextid = ctx.id
                                 JOIN {course}           c   ON ctx.instanceid = c.id
                                WHERE asg.roleid       = 5
                                  AND ctx.contextlevel = 50 