Skip to content

How do I add specific columns from different tables onto an existing table in postgresql?

I have an original table (TABLE 1):

A B C D
1 3 5 7
2 4 6 8

I want to add column F from the table below (Table 2) onto table 1:

A F G H
1 29 5 7
2 30 6 8

As well as adding Column J,L and O from the table below (Table 3) onto column 1:

A I J K L M N O
1 9 11 13 15 17 19 21
2 10 12 14 16 18 20 22

How do I go about adding only the specific columns onto table 1?

Expected Result:

A B C D F J L O
1 3 5 7 29 11 15 21
2 4 6 8 30 12 16 22

Answer

Use following query

SELECT T1.A,
       B,
       C,
       D,
       F,
       J,
       L,
       O
FROM   table1 T1
       JOIN table2 T2
         ON T1.A = T2.A
       JOIN table3 t3
         ON T1.A = T3.A