This is a significant issue, but not in the way you think. Having an SQL query to run against the DB (to remove the empty ratings) would not really help.
Think about this: How do you intend to handle the case where someone removes the “rating” tag from a topic? Or in any other way removes a topic from being a rated one?
I would argue that the only sane way for you to handle that case is to treat it as a vanilla topic: No stars, no possibility to rate, no restrictions in the amount of posts. So you don’t only look whether there are ratings when deciding on whether to draw stars or not, you always check whether the topic fulfills the criteria of being a rated one. (I have honestly no idea what you intend to do when someone tags an existing topic with “rating”, the setting to restrict it to one post per user is active, and users have already posted more than once…)
And if you have implement that, I won’t have any problems with stale ratings either.
I’ve got a discourse docker instance lying around on my desktop. Tell me when you have a version which you think should work, and I’ll test it there.