Click here to Skip to main content
15,886,017 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
Dear Experts !!

Am fresher to Sqlserver 2005 Database.

How to join 3 tables... to one table in sqlserver

so, here is my requirement :

I have 3 tables in my Database...as StudentCodes,Names,Marks

1)From First table[StudentCodes Table], i want to take StudentId
2)From second Table[Names Table], i want to take Student Name , StudentFathersName
3)From 3rd Table[Marks Table], I want to take Total Marks, Percentage

and Finally i want to make these 3 tables into one table.

Please can u suggest me, how to join these 3 tables to one Table.

Thanks a lot in Advance
Posted
Comments
OriginalGriff 27-May-12 6:18am    
From that information, we can't really help a lot.
You need to look at (or explain) your structure a bit better: for example, how do you know that the StudentId from StudentCodes ties to StudentName in the Names table? And the Marks Table?
And if you have a relationship already, why are you creating a new table? Doesn't that just duplicate information and reduce the maintainability of your data?
Use the "Improve question" widget to edit your question and provide better information.

 
Share this answer
 
Comments
Wendelius 27-May-12 7:20am    
Good links
Sander Rossel 27-May-12 8:05am    
W3Schools is my main reference for SQL queries.
MSDN is also a standard reference.
Nice links, all of them. My 5.
Mohamed Mitwalli 27-May-12 9:38am    
5+
Dear Ranjith,

1st u should have common id in all 3 tables, suppose Studentid.
SQL
Select a.studentid,b.name,b.fathername,c.marks,c.percent from 
StudentCodes a With (Nolock), Names b With (Nolock), Marks c With (Nolock)
where a.studentid=b.studentid and b.studentid=c.studentid and a.studentid=c.studentid


this is basic eg. hope it will help you.

Regards,
AP
 
Share this answer
 
v2
Comments
Mohamed Mitwalli 27-May-12 9:38am    
5+
Most likely you don't mean that you want to join (create) a single table from those tables. Do you want to create a result set based on them. If that is the case, as Sandeep pointed out, you need a join.

So to select a single result set, your query could look something like:
SQL
SELECT sc.StudentId,
       n.StudentName,
       n.StudentFathersName,
       m.TotalMarks,
       m.Percentage
FROM   StudentCode sc 
       INNER JOIN Names n ON n.StudentId = sc.StudentId
       INNER JOIN Marks m ON m.StudentId = sc.StudentId
 
Share this answer
 
Comments
Sander Rossel 27-May-12 8:05am    
Nice answer, my 5.
devachrismohammed 14-Dec-13 4:32am    
thanks a lot
Wendelius 27-May-12 9:21am    
Thanks :)
Wendelius 14-Dec-13 14:24pm    
You're welcome
Mohamed Mitwalli 27-May-12 9:38am    
5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900