Gemma (and Gareth), you have me thinking a little more about this.
First, we have the "mdl_users" table that contains "ALL" users in my Moodle. In my case, there are 1496 records in this table. However, when I go into my Moodle, Users|Accounts|Browse, I get 1285 users. So there are some apparent discrepancies.
I think that you (and I) would like to begin with this table and start eliminating some users from it.
First, this table does contain "Deleted" users, and these should go. If I execute the following SQL, I end up with a very close match, 1286 versus 1285. Maybe the "guest" user is not included in Moodle's User|Browser list.
SELECT * FROM mdl_user
where deleted = 0
Now, from this remaining list, you want to eliminate:
1) Users who are enrolled in any course.
2) Users who have some system role anywhere in Moodle.
3) Suspended users (these could be students who were once in a course)
After doing this, we believe that we will end up with "bogus" users.
Currently, the query that I shared with you produces 2 bogus users in my Moodle. If I work on this new query, I would expect 2 (or more) users to show.
I think that I have another group of people who I must find, those who I have added (typically and administrator who wants to see one of my courses) who might have once had a student role, but was removed from the course of interest, but I still want this person in my Moodle. For these people, I put "99999" into their "idnumber" but this might not be the best approach (especially if one of these administrators actually enrolls into one of my courses as a student.) I am not sure what to do with these people. You might have some special cases, too.