Sorry, I suppose I wasn't being entirely clear with my question.
I need a list of ALL the users in the site who are enrolled in ANY course whatsoever. For example, if John is enrolled in Beginning Squash and Jane is enrolled in Basic Spanglish, then my query needs to return both John and Jane, regardless of the fact that they are enrolled in entirely different courses. Furthermore, I only want one row returned for each of them. The query:
select userid from {role_assignments} where roleid = 5
gives me duplicate roles. I don't want that.
I hope that, given this explanation, the reasoning behind my query makes more sense. Again, I need the information on all users who are enrolled in any courses on a sitewide level, not merely a course level.
Obviously, $DB->execute() is not what I need. I said that. My preference is always to use the higher-level APIs when possible. You're quite right in that examining the source code for pages such as user/index.php would give me the starting point I would need if I needed a list of students in a *given course*, but I could not find an equivalent function to find a list of users who are students in any course throughout the site. If that functionality exists in Moodle, I have not found it.
ETA: I should add that the results of this query will inform a larger process where we purge our site of all users who are not instructors or program administrators or admin users. Since we have well over three thousand students and close to one hundred courses, doing this by hand is not feasible.