For storing and organising questions, the key links are question -> question_category -> context.
However, when questions are used, you get links quiz -> quiz_question_instances -> questions. And when random questions are involved, you get really hard-to-find links, which is something that has to be improved some day.
Also, it is, unfortunately, possible to add questions to a quiz in one course, the move things around in the question bank, so the question ends up in somewhere unrelated to the quiz. That nasty possibility is something that was only realised recently: MDL-14802.
However, I will apply the SQL syntax fix. Thanks for that.