DB Table Structure:
Session Table (aka Exam Table)
SessionId(auto) SessionName
137 XULWQ
Question Table:
SessionId QuestionId QuestionContent QuestionNo QuestionMarks OptionId
137 1 Name 2 Things 1 5 5
137 2 Name 3 Things 2 5 2
Option_Table Table:
OptionId OptionType
1 A-C
2 A-D
3 A-E
4 A-F
5 A-G
6 A-H
Answer Table:
AnswerId(auto) SessionId QuestionId Answer
200 137 1 B
201 137 1 F
202 137 2 D
203 137 2 A
204 137 2 C
I am having trouble compiling the query below. I want to select the following fields undeneath in the query but the problem I am having is with my joins. If you look at the last join I am trying to retrieve OptionID from Option_Table Table but I have to go through the Answer table which doesn’t have that field. My problem is that I have 3 fields which actually rely on joining with the Question` Table.
My question is what is the correct way of joining the tables below so that it displays the answers for each question with the fields I want to SELECT?
SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionNo, q.QuestionContent, an.Answer, an.AnswerId, q.QuestionMarks, q.OptionId, o.OptionType
FROM Session s
INNER JOIN Question q ON s.SessionId = q.SessionId
JOIN Answer an ON q.QuestionId = an.QuestionId
JOIN Option_Table o ON an.OptionId = o.OptionId
WHERE s.SessionName = ?
ORDER BY q.QuestionId, an.Answer
Output required:

1 Answer