Click here to Skip to main content
15,210,670 members
Rate this:
Please Sign up or sign in to vote.
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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

   
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+
Rate this:
Please Sign up or sign in to vote.

Solution 2

Dear Ranjith,

1st u should have common id in all 3 tables, suppose Studentid.
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
   
v2
Comments
Mohamed Mitwalli 27-May-12 9:38am
   
5+
Rate this:
Please Sign up or sign in to vote.

Solution 3

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:
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
   
Comments
Sander Rossel 27-May-12 8:05am
   
Nice answer, my 5.
Wendelius 27-May-12 9:21am
   
Thanks :)
Mohamed Mitwalli 27-May-12 9:38am
   
5+
Wendelius 1-Jun-12 0:37am
   
Thanks :)
devachrismohammed 14-Dec-13 4:32am
   
thanks a lot
Wendelius 14-Dec-13 14:24pm
   
You're welcome

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100