-1

I have two tables with lots of columns, so I want to write dynamic query to find the same column names from both tables in single query in SQL.

I have tried using intersect - but it cannot work. Is there any option for finding same column names from two different tables?

2
  • 1
    What do you mean find same column names? There's no a JOIN B on same_columns. To find a table's columns you can use the INFORMATION_SCHEMA.COLUMNS table and filter by the two table names. Finding the common columns is the same as trying to find any duplicate values. You could eg use WHERE TABLE_NAME IN ('A','B') GROUP BY COLUMN_NAME HAVING COUNT(*)=2 to get names that appear in both tables. Commented Apr 9 at 7:42
  • Please provide a minimal reproducible example with sample data and desired results and the correct RDBMS. Commented Apr 15 at 9:15

3 Answers 3

4

How do you mean, "intersect doesn't work"? It does for me.

I marked common columns with asterisks (***):

SQL> desc a1_rk
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID_KBR                                             NUMBER
 ID_ULICA           ***                             NUMBER
 ID_POSTA                                           NUMBER
 KBR                                                VARCHAR2(10)
 UPISANO            ***                             DATE
 DEAKTIVIRANO       ***                             DATE

SQL> desc a1_ru
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID_ULICA           ***                             NUMBER
 ID_NASELJE                                         NUMBER
 NAZIV                                              VARCHAR2(200)
 UPISANO            ***                             DATE
 DEAKTIVIRANO       ***                             DATE

SQL> select column_name from user_tab_columns where table_name = 'A1_RK'
  2  intersect
  3  select column_name from user_tab_columns where table_name = 'A1_RU';

COLUMN_NAME
--------------------------------------------------------------------------------
DEAKTIVIRANO
ID_ULICA
UPISANO

SQL>
Sign up to request clarification or add additional context in comments.

Comments

1

You can self-join on the user_tab_columns (or all_tab_columns if you're querying it with a user that doesn't own the tables) table from the data dictionary:

SELECT a.column_name
FROM   user_tab_columns a
JOIN   user_tab_columns b ON a.table_name = 'TABLE_NAME_1' AND
                             b.table_name = 'TABLE_NAME_2' AND
                             a.column_name = b.column_name

EDIT:
As noted in the comments, although the questions was tagged with , OP seems to be using . If that is indeed the case, you can't use user_tab_columns, which is a table from Oracle's data dictionary, but instead can use the standard information_schema tables:

SELECT a.column_name
FROM   infromation_schema.colunms a
JOIN   infromation_schema.colunms b ON a.table_name = 'TABLE_NAME_1' AND
                                       b.table_name = 'TABLE_NAME_2' AND
                                       a.column_name = b.column_name

3 Comments

select column_name from user_tab_columns where table_name = 'customers' intersect select column_name from user_tab_columns where table_name = 'bankinfo'; i am doing same but facing this error :> SQL Error [42P01]: ERROR: relation "user_tab_columns" does not exist Position: 31
@PrasadAbhang this means you aren't using Oracle, despite the question being tagged as so. What rsbms are you using?
Error 42P01 indicates you are using PostgreSql, not Oracle.
0

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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.