Sometimes its needed to check where two tables having the same number of columns?

To demonstrate this first create two tables as EMP1 and EMP2.

Lets we have two Tables as EMP1 and EMP2 as below,

Suppose table EMP1 is as below

USE [VirendraTest]


CREATE TABLE [dbo].[Emp1]
( [ID] [int] IDENTITY(1,1NOT NULL,
[Name] [nchar](10NULL,
[Basic] [numeric](18, 2NULL


and table EMP2 is as below,

USE [VirendraTest]


CREATE TABLE [dbo].[Emp2]
[Name] [varchar](10NULL,
[Basic] [numeric](18, 2NULL,
[Age] [int]

Now to find the extra columns from EMP2 to EMP1 , we can use below query,

Select c2.table_name,c2.COLUMN_NAME from [INFORMATION_SCHEMA].[COLUMNS] c2
table_name=‘Emp2’ and c2.COLUMN_NAME not in (select column_name from [INFORMATION_SCHEMA].[COLUMNS] where table_name=’emp1′)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s