It's unclear what you want the columns for. To find matching column names you can query the data dictionary, e.g.
select column_name
from all_tab_cols
where table_name in ( 'EMPLOYEES', 'DEPARTMENTS' )
and owner = 'HR'
group by column_name
having count (*) = 2;
COLUMN_NAME
----------------
DEPARTMENT_ID
MANAGER_ID
However if you're trying to get the data from the tables as well you can't in a single query.
From 19.7 you can do this by creating a table SQL macro. This iterates through the column names in each table to find any in common. Then constructs the query you want using these matching names.
For example, this finds the intersection of the rows with the same names in two tables:
create or replace function matching_columns (
t1 dbms_tf.table_t,
t2 dbms_tf.table_t
) return clob sql_macro as
column_list clob;
stmt clob;
begin
<<t1loop>>
for t1col in 1 .. t1.column.count loop
for t2col in 1 .. t2.column.count loop
if t1.column ( t1col ).description.name =
t2.column ( t2col ).description.name then
column_list :=
column_list || t1.column ( t1col ).description.name || ',';
continue t1loop;
end if;
end loop;
end loop;
if column_list is not null then
stmt := 'select ' || rtrim ( column_list, ',' ) || ' from t1
intersect
select ' || rtrim ( column_list, ',' ) || ' from t2';
else
stmt := 'select null from t1
intersect
select null from t2';
end if;
return stmt;
end;
/
select * from matching_columns ( hr.employees, hr.departments );
MANAGER_ID DEPARTMENT_ID
---------- -------------
201 20
114 30
121 50
103 60
145 80
100 90
108 100
205 110
select * from matching_columns ( hr.employees, hr.regions );
NULL
------
<null>
find same column names? There's noa JOIN B on same_columns. To find a table's columns you can use theINFORMATION_SCHEMA.COLUMNStable and filter by the two table names. Finding the common columns is the same as trying to find any duplicate values. You could eg useWHERE TABLE_NAME IN ('A','B') GROUP BY COLUMN_NAME HAVING COUNT(*)=2to get names that appear in both tables.