I've never tried to do what you're doing. We don't have a lot of unenrolled users, so this is hard for me to test. I'm sure it's not impossible to retrieve grades for unenrolled users. The question is how do we get them?
I used the user_enrolments and enrol tables because there aren't any tables called user_enrolments_history or enrol_history.
Does it help to add the following as the last line in the query? A '1' indicates "unenrolled"