This is a little complicated. You want a "vertical" list but have nothing to match the columns. You can use row_number()
and union all
:
select max(t1_col1), max(t1_col2), max(t2_col1), max(t2_col2)
from ((select t1.col1 as t1_col1, t1.col2 as t1_col2,
null as t2_col1, null as t2_col2, row_number() over () as seqnum
from table1 t1
) union all
(select null, null, t2.col1, t2.col2, row_number() over () as seqnum
from table2 t2
)
) t
group by seqnum;
Here is a db<>fiddle.
Note that this will keep all rows in both tables, regardless of which is longer. The specific ordering of the rows in each column is not determinate. SQL tables represent unordered sets. If you want things in a particular order, you need a column that specifies the ordering.
If you want to save this in a new table, put create table as table3
before the select
. If you want to insert into an existing table, use insert
.