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)
 Select From Two Tables

Author  Topic 

n3xus
Starting Member

6 Posts

Posted - 2011-04-29 : 07:42:40
[CODE]Grade_table
******************
Student_ID | Grade
------------------
t106 | 100
t107 | 85
t108 | 75
t109 | 60[/CODE]

[CODE]Student_archive
*******************
Student_ID | Gender
-------------------
t106 | Male
t109 | Female[/CODE]

[CODE]Student
Student_ID | Gender
*******************
t108 | Female
t107 | Male[/CODE]

Now I need to Join 3 tables which would look like following

[CODE]Student_ID | Gender | Grade
---------------------------
t106 | Male | 100
t107 | Male | 85
t108 | Female | 75
t109 | Female | 60[/CODE]

I believe I should check for ISNULL and if it's NULL select gender From other tables. But I think there are smarter people in this forum than me who would have better Idea.
Any help?

n3xus
Starting Member

6 Posts

Posted - 2011-04-29 : 07:57:34
[code]SELECT * FROM Grade_table
LEFT OUTER JOIN
(SELECT Student_ID, Gender FROM Student
UNION ALL
SELECT Student_ID, Gender FROM Student_archive) As Students
ON Grade_table.Student_id = Students.Student_ID[/code]

Got it.
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-04-29 : 07:59:06
Try unioning the Student_Archive and Student tables and then joining to that union.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-04-29 : 08:08:58
If this isn't the only query that needs information from Student and also Student_archive then it is maybe a good idea to create a view "Student_all" or so using the UNION query inside the view...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -