Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 752
ByulTaeng Avatar asked Feb 02 '26 04:02

ByulTaeng


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
like image 123
Quassnoi Avatar answered Feb 04 '26 16:02

Quassnoi


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
like image 42
SO User Avatar answered Feb 04 '26 18:02

SO User



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!