Lets suppose Database Name is MYDB and TABLE1, TABLE2, TABLE3 are tables in MYDB database

Sql query to retrieve all the column names in a table :

select COLUMN_NAME from MYDB.INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = ‘TABLE1’

Sql query to get common Columns in two tables in Sql Server :
select COLUMN_NAME from MYDB.INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = ‘TABLE1’
intersect
select COLUMN_NAME from MYDB.INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = ‘TABLE2’

Sql query to get common Columns in three tables in Sql Server :

select COLUMN_NAME from MYDB.INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = ‘TABLE1’
intersect
select COLUMN_NAME from MYDB.INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = ‘TABLE2’
intersect
select COLUMN_NAME from MYDB.INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = ‘TABLE3’

Leave a Reply

Your email address will not be published. Required fields are marked *