the SQL needs to be fixed to fetch userids from both tables mentioned in my comment. The userid field is a foreign key in
. In case this is not enough for the RDBMS at moodle.org and it will refuse to use the implicit index to obtain DISTINCT userid, we may try to create the explicit index yet.
Otherwise, the code looks ok to me (as long as we do not want to prepare a more complex SQL that would already look into
and would return just those who are missing. For now, I would prefer your solution though.