ByulTaeng
ByulTaeng

Reputation: 1269

Join two column with the same number of row

I want to combine 2 tables into one. Let say I have:

Table1

ID       Name
1        A
2        B
3        C

Table2

ID       Name
4        D
5        E
6        F

I want to make Table3

Name1    Name2
A        D
B        E
C        F

How can I do this in SQL Server? Any help is greatly appreciated.

Upvotes: 3

Views: 501

Answers (2)

Quassnoi
Quassnoi

Reputation: 425803

WITH    t1 AS
        (
        SELECT  a.*, ROW_NUMBER() OVER (ORDER BY id) AS rn
        FROM    table1 a
        ),
        t2 AS
        (
        SELECT  a.*, ROW_NUMBER() OVER (ORDER BY id) AS rn
        FROM    table2 a
        )
SELECT  t1.name, t2.name
FROM    t1
JOIN    t2
ON      t1.rn = t2.rn

Upvotes: 5

Rashmi Pandit
Rashmi Pandit

Reputation: 23858

select t1.Name Name1, t2.Name Name2
from Table1 t1, table2 t2
where t1.ID = t2.ID

OR

select t1.Name Name1, t2.Name Name2
from Table1 t1 join table2 t2
     on t1.ID = t2.ID

Upvotes: 1

Related Questions