Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Comparing 2 Tables

Author  Topic 

gcarolino
Starting Member

2 Posts

Posted - 2011-12-05 : 14:50:22
Im not sure if its actually comparing so hear is my question.

i have 2 tables. A USERS table and an USER_ANSWERS table.

USERS_ANSWER
userID Answer Question_no
1 yes 1
3 no 1
5 yes 1
1 no 2
3 yes 2
5 no 2


USERS
id user_name
1 bill
2 jim
3 kelly
4 allen
5 sammy
6 patrick

I am trying to write a query that will result in a list of the ALL the users with the answers regardless if they answered for a curtian question number.

id user_name answer question_no
1 bill yes 1
2 jim null 1
3 kelly no 1
4 allen null 1
5 sammy yes 1
6 patrick null 1


I have only been able to get a list of users that have answered the question. Any help will be much appreciated.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-12-05 : 14:52:31
You should be able to switch your query so that it uses an OUTER JOIN to get the result set that you need. INNER JOIN just provides the matches between the two tables. OUTER JOIN allows you to return the other rows where a match doesn't exist.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

gcarolino
Starting Member

2 Posts

Posted - 2011-12-06 : 08:03:39
Thanks tkizer!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-12-06 : 12:22:27
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -