'Is there a way to check what columns in one dataset exist in another dataset? sql

For example, I have table 1 and table 2, is there a way I can check the which columns from table 1 exist in table 2? Thanks!



Solution 1:[1]

While the best solutions will be RDBMS specific (which you have not indicated), a general way that's supported by most databases is to use the information_schema views, for example the following will list the column names in table1 that are also in table2:

select column_name 
from information_schema.columns t1
where t1.table_schema='schemaName' and t1.table_name='table1'
and exists (
  select * from information_schema.COLUMNS t2
    where t2.column_name = t1.column_name 
      and t2.table_schema = t1.table_schema 
      and t2.column_name ='table2'
);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stu