SQL Server - joining 3 select queries - Asked By muthuraman alexander on 05-Sep-11 03:20 AM

hi all
 i need to join three select queries like

my select queries are

select * from table1
order by table1.ID

select * from table2
order by table2.ID

select * from table3
order by table3.ID

ID is a primary key which is common in all of these tables

thanks in advance
Reena Jain replied to muthuraman alexander on 05-Sep-11 03:23 AM

Join is possible if you have one command field in both table which you are joining. since you have to provide any field that are existing in both table. I am assuming that you have id field is common in the 2 table and according to it here is the eg query for you

select a.*, b.*, c.* from table1 as a
inner join table2 as b
on a.id=b.id
inner join table3 as c
on b.id=c.id

Hope this will help you
Anoop S replied to muthuraman alexander on 05-Sep-11 03:33 AM
Follow this example-

I have the following tables (and example values):

user: user_id (1, 2, 3) username (john33, reddiamond...) password (pass1, pass2...)

session: session_id (4,5, 6) user_id (1, 2, 3)

activity activity_id (1, 2) name (running, walking...)

user_activity user_activity_id (1, 2, 3, 4, 5) session_id (4, 5) activity_id (1, 2)


SELECT u.username, a.name
FROM user_activity ua
INNER JOIN session s
ON ua.session_id = s.session_id
ON s.user_id = u.user_id
INNER JOIN activity a
ON ua.activity_id = a.activity_id

Refer this for more details
Rohan Dave replied to muthuraman alexander on 05-Sep-11 03:45 AM
You can use Join to join your three tables and display the result..

your query looks like below

Select tbl1.* , tbl2.* , tbl3.*
Table1 tbl1
Inner Join Table2 tbl2 ON tbl1.ID = tbl2.ID
Inner Join Table3 tbl3 ON tbl2.ID = tbl3.ID