Attach two columns with the same number of rows

I want to combine 2 tables into one. Say I have:

Table 1

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 to do this in SQL Server? Any help is appreciated.

+3
source share
2 answers
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
+5
source
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
+1
source

Source: https://habr.com/ru/post/1709968/


All Articles