title: How to perform a OUTER JOIN on two complex SQL queries?
body: <p>I have two relatively complex queries that I am trying to join together into one result set.</p>
  
  <p>Result set 1:</p>
  
  <pre><code>SELECT  sq.question_id,     
      COUNT(ra.question_option_id) AS TotalAnswers
  
  FROM    dbo.survey_question sq
      LEFT OUTER JOIN dbo.question_option qo
          ON sq.question_id = qo.question_id
      LEFT OUTER JOIN dbo.form_response_answers ra
          ON qo.question_option_id = ra.question_option_id
  GROUP BY sq.question_id
  </code></pre>
  
  <p>Result set 2:</p>
  
  <pre><code>SELECT  p.program_id, 
      p.pre_survey_form_id, 
      p.post_survey_form_id, 
      fq.form_id, 
      sq.question_id, 
      sq.question_text, 
      qo.question_option_id, 
      qo.option_text, 
      G.Total
  
  FROM    dbo.program p
      LEFT OUTER JOIN dbo.form_question fq
          ON p.pre_survey_form_id = fq.form_id OR p.post_survey_form_id = fq.form_id
      LEFT OUTER JOIN dbo.survey_question sq
          ON fq.question_id = sq.question_id
      LEFT OUTER JOIN dbo.question_option qo 
          ON sq.question_id = qo.question_id
      LEFT OUTER JOIN (
          SELECT ra.question_id, ra.question_option_id, COUNT(*) AS Total
          FROM dbo.form_response_answers ra
          GROUP BY ra.question_option_id, ra.question_id 
      ) G
          ON G.question_id = sq.question_id AND G.question_option_id = qo.question_option_id
  
  ORDER BY p.program_id, fq.form_id, sq.question_id, qo.question_option_id
  </code></pre>
  
  <p>I need to join them on the rows where question_id matches. Please help.</p>