QuestionHow do I get all rows in Question table (regardless of policyNumber's existence), with Selected column from PolicyAnswers table given a specific policyNumber (if in table).TablesCREATE TABLE Questions(QuestionID tinyint, QuestionText varchar(255), BadIfTrue bit)CREATE TABLE PolicyAnswers(PolicyNumber varchar(11), QuestionID tinyint, Selected bit)
Sample DataINSERT INTO PolicyAnswers (PolicyNumber, QuestionID, Selected)VALUES ('XOP12346578', 1, 1), ('XOP12346578', 2, 1), ('XOP12346578', 3, 0), ('QWS87654321', 1, 0)INSERT INTO Questions(QuestionID tinyint, QuestionText varchar(255), BadIfTrue bit)VALUES (1, 'Is building more than ten years old', 1),(2, 'Is building made from brick', 0),(3, 'Is alcohol served on premises', 1)(4, 'Are security cameras present at each entrance/exit', 0)My attemptSELECT Questions.QuestionText, Questions.BadIfTrue, PolicyAnswers.SelectedFROM Questions LEFT OUTER JOIN PolicyAnswers ON Questions.QuestionID = PolicyAnswers.QuestionIDWHERE (PolicyAnswers.PolicyNumber = 'GFB12378654')
Expected ResultsFor a Policy Number not in PolicyAnswers table: (all questions - no answers)'Is building more than ten years old', null'Is building made from brick', 0, null'Is alcohol served on premises', 1, null'Are security cameras present at each entrance/exit', 0, null
For a Policy Number in PolicyAnswers table: (XOP12346578 from above)'Is building more than ten years old', 1'Is building made from brick', 0, 1'Is alcohol served on premises', 1, 0'Are security cameras present at each entrance/exit', 0, null