LMSouq
moodle-core Open

SQL query for Courses Enrolment on Moodle

CO
Community Contributor
1 month ago
3 views
Problem Description
I was looking for the proper SQL queries for retrieving all students enrolled in a certain course, or all courses a certain student has enrolled in, on **Moodle**. I have found a few solutions from the Internet, and most of them suggest joining these tables: **context, role_assignments, course, user, role** But then when I looked at the database, I found that there is a table named `user_enrolments`, and it seems to me that I could get the results by joining the following tables: **user_enrolments, user, course, enrol** For example, SELECT u.id, c.id FROM mdl_user u INNER JOIN mdl_user_enrolments ue ON ue.userid = u.id INNER JOIN mdl_enrol e ON e.id = ue.enrolid INNER JOIN mdl_course c ON e.courseid = c.id and SELECT u.id, c.id FROM mdl_user u INNER JOIN mdl_role_assignments ra ON ra.userid = u.id INNER JOIN mdl_context ct ON ct.id = ra.contextid INNER JOIN mdl_course c ON c.id = ct.instanceid INNER JOIN mdl_role r ON r.id = ra.roleid WHERE r.id = 5 *(where 5 is the id for the role `student`)* These 2 queries give me the **SAME** set of results. (tested on a small set of data only) So I would like to ask what are the differences between the two approaches? Thank you for any assistance in advance.

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base