I want to select survey_id, question_id, question_text and answer_id from the following three tables.
In SurveyTable I have:
Survey{survey_id,survey_title}
In QuestionTable I have:
Question{survey_id,question_id,question_text}
In AnswerTable:
Answer{question_id,answer_id,answer_text}
I want to select from these tables using joins. When survey_id is equal to the value in QuestionTable and SurveyTable.
Well, you can start off with something like
The
INNER JOINs will ensure that you only have surveys where there are questions and answers available.If you wish to return all surveys, regardless of if they have questions or answers, or even all surveys with question regardless of answers you could use
LEFT JOINSYou have to try and remember that a LEFT JOUN states
Return all the data from the table on the left, and only those in the right that matches thos on the left.
Have a look at this article, it does a nice graphical explanation.
SQL SERVER – Introduction to JOINs – Basic of JOINs