Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8685889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:41:35+00:00 2026-06-12T22:41:35+00:00

I have two tables, one that contains questions (that are considered ‘templates’): |QuestionID|QuestionText |

  • 0

I have two tables, one that contains questions (that are considered ‘templates’):

|QuestionID|QuestionText      |
|----------|------------------|
|1         |What is your name?|
|2         |What is your age? |
|-----------------------------|

And one that contains answers to those questions, keyed by a separate Customer ID value:

|CustID    |QuestionID|Answer |
|----------|------------------|
|1000      |1         |John   |
|1000      |2         |22     |
|2000      |1         |Alice  |
|-----------------------------|

As you can see, Customer #1000 answered questions 1 and 2, and customer #2000 answered only question 1.

I need to load these into a grid on a web page by customer, and I thought I’d just do a left outer join between the template and answer tables to get something like this:

|CustID    |QuestionID|Answer |
|----------|------------------|
|2000      |1         |Alice  |
|2000      |2         |<null> |
|-----------------------------|

So in this case, cust #2000 would come back with the Answer column for question #2 as null and the application would know that it needs to be answered. This is an example of the SQL I tried but no matter how many combinations of OUTER and LEFT and so on I use for the JOIN, I just can’t get this to work with a single query:

select 
    q.QuestionID,
    q.QuestionText,
    a.CustID,
    a.Answer,
from 
    SurveyAnswers a  
        full outer join SurveyQuestions q
        on a.QuestionID = q.QuestionID
    where 
        a.CustID = @CustomerID

Basically I need all the records in the SurveyQuestions table, and the value of the Answer column from any records in SurveyAnswers that match the CustomerID.

Instead I just get the one record in SurveyAnswers that has an answer, and therefore is a match (I guess?) in SurveyQuestions. My SQL-fu is not strong.

I also tried UNION and friends, but the two tables have different structures so that didn’t work too well.

I could of course pull the questions and answers separately and do the ‘coalescing’ in my C# code but I wanted to see if I could do it directly from the stored procedure. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:41:37+00:00Added an answer on June 12, 2026 at 10:41 pm

    The problem you are having is that after an outer join on a mismatching row (Question 2 in your example) all columns for that row of the mismatched table (SurveyAnswers) are valued NULL. That includes the CustId column. The WHERE that executes later sees NULL = @CustomerId and excludes that row. To fix that you need to move the condition into the join clause. So you need to ask SQL Server for all questions together each’s answer given by “@CustomerId” if such answer exist. Your query is asking for (all questions and all their answers) but return only those records that “@CustomerId” answered.

    SELECT 
        q.QuestionID,
        q.QuestionText,
        a.CustID,
        a.Answer,
    FROM 
        SurveyAnswers a  
            RIGHT outer join SurveyQuestions q
            on a.QuestionID = q.QuestionID
        AND
            a.CustID = @CustomerID;
    

    should do the trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, one that has 450 rows, the other is a Sort
I have two tables, one that has a foreign key from the other. I
I want to delete multiple records at once. I have two tables, one that
I have two tables that link together through an id one is submit_moderate and
Suppose I have two tables that are linked (one has a foreign key to
I was thinking that I would have two tables for mysql. One for storing
I was making one table that have two foreign key to another tables. But
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables questions and answered . answered contains the answers for all
I have one table that has two fields - ID1 and ID2 ID1 can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.