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

  • Home
  • SEARCH
  • 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 8665685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:32:43+00:00 2026-06-12T17:32:43+00:00

Say I have three tables: User Table { UserId INT, Username NVARCHAR … }

  • 0

Say I have three tables:

User Table
{
    UserId INT,
    Username NVARCHAR
    ...
}

Questions
{
    QuestionId INT
    QuestionText NVARCHAR
}

Answers
{
    AnswerId INT,
    QuestionId INT,
    UserId INT,
    Answer NVARCHAR
}

This structure is obviously overly simplified, but for the purpose of this example it should suffice.

What would be the best way to select users who have specific answers for specific questions, for instance – Assuming the tables are populated with the following data:

User Table

UserId              Username                ...
--------------------------------------------------------------------------------------------------------
1                   User1                   ... 
2                   User2                   ... 
3                   User3                   ...
4                   User4                   ...
5                   User5                   ...
6                   User6                   ...
7                   User7                   ...
8                   User8                   ...
9                   User9                   ...
10                  User10                  ...
...                 ...                     ...

etc

Questions Table

QuestionId              QuestionText
--------------------------------------------------------------------------------------------------------
1                       What is your favorite color?
2                       What do you prefer cats or dogs?
3                       Do you prefer if it is too hot or too cold?
4                       What is your favorite season (Summer, Autumn (Fall), Winter, Spring)?
5                       How Old Are you?
...                     ...

etc

Answers Table

AnswerId                QuestionId              UserId          Answer
--------------------------------------------------------------------------------------------------------
1                       1                       1               Red 
2                       1                       2               Red
3                       1                       3               Blue
4                       1                       4               Green
5                       1                       5               Black
6                       2                       6               Cats
7                       2                       1               Dogs
8                       3                       1               Too Cold
9                       4                       1               Spring
10                      5                       1               22
11                      2                       4               Dogs
12                      3                       4               Too Hot
13                      3                       3               Too Cold
14                      5                       6               46
15                      1                       8               Purple

If I wanted to select users who liked dogs and red or purple and under the age of 50 etc

Would the best (most efficient) way to do this be to have multiple joins from the user table to the answers table (one for each answer condition required)

For example:

If I were wanting to get the users who liked dogs and the color red I could use the following MSSQL:

SELECT * 
FROM 
Users 
JOIN Answers As a1 
ON Users.UserId = a1.UserId 
JOIN Answers as a2 
ON Users.UserId = a2.UserId 
WHERE 
    (
        a1.QuestionId = 1 AND 
        a1.Answer = 'Red'
    ) AND 
    (
        a2.QuestionId = 2 AND 
        a2.Answer = 'Dogs'
    )

There could be numerous answer conditions.

Basically the question I am asking is what is the best way to write a query where you have conditions on multiple rows from the same table with the same columns…

Sorry if this is confusing, feel free to ask any questions I will try and answer them as best as I can…

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-12T17:32:44+00:00Added an answer on June 12, 2026 at 5:32 pm

    Your basic query looks just fine. As you get more elaborate, you would construct your WHERE clauses slightly differently depending on how your conditions need to be combined.

    For example, in the example you provided, of either red or purple being acceptable answers, you could construct the WHERE clause like this:

    WHERE (a1.QuestionId = 1 AND (a1.Answer IN ('Red','Purple')) 
      AND (a2.QuestionId = 2 AND a2.Answer = 'Dogs')
    

    It gets more complicated if only certain sets of answers are acceptable, so if ‘Red’ and ‘Dogs’ or ‘Purple’ and ‘Cats’ are acceptable, it would look more like this:

    WHERE 
      (
              (a1.QuestionId = 1 AND a1.Answer = 'Red')
          AND (a2.QuestionId = 2 AND a2.Answer = 'Dogs')
      )
      OR
      (
              (a1.QuestionId = 1 AND a1.Answer = 'Purple')
          AND (a2.QuestionId = 2 AND a2.Answer = 'Cats')
      )
    

    If your conditions get more complicated, you might want to read Dynamic Search Conditions in T-SQL. While your conditions aren’t dynamic, there’s a lot of useful information there.

    Finally, since it’s easy to get confused with what question ID goes with what answers, particularly if they aren’t nice, human-recognizable values, it can help to use CTEs to pre-select the answers:

    WITH Colors
    AS   (
        SELECT *
        FROM   Answers
        WHERE  QuestionID = 1
    )
    ,    Animals
    AS   (
        SELECT *
        FROM   Answers
        WHERE  QuestionID = 2
    )
    SELECT   *
    FROM     Users 
       JOIN  Colors
           ON  Users.UserID = Colors.UserID
       JOIN  Animals
           ON  Users.UserID = Animals.UserID
    WHERE   (
            Colors.Answer = 'Red'
        AND Animals.Answer = 'Dogs'
        )
        OR  (
            Colors.Answer = 'Purple'
        AND Animals.Answer = 'Cats'
        )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have 2 tables in my db user: id, username userpost: userid,
Say I have three tables, a table of users, a table of around 500
Say I have three tables: Fruit (Table 1) ------ Apple Orange Pear Banana Produce
I have three tables, 1-Users, 2-Softwares, 3-UserSoftwares. if suppose, Users table having 6 user
Let's say you have three tables named Item, Event, and Seat, constructed as such:
Lets say that I have three tables, customers, orders and orderDetails. I'm doing this:
I have user and group tables set up as the following. users id int
Let's say I have three tables (with the columns in each): USERS: id, name,
I have three tables, we'll call them table1, table2, and table3. Lets say each
Say I have a table with the following layout: Id Int PRIMARY KEY IDENTITY

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.