That query makes several assumptions that are probably not true for most people: e.g. that the quiz is using random questions, and that the questionid contains 5 characters.
It will also only work for certain question types.
In later Moodle versions, the replacement for mdl_quiz.questions is the mdl_quiz_slots table. That partly answers your specific questions. However, to get a working query, you are going to have to make some assumptions specific to your situation.