Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Table differences

I have 2 tables (1/2 mil. rows each) that should be identical, but one has more records, for whatever reason. What...

is the best, most efficient way to determine which records are different? (There are no duplicates in either table.) I have heard subselects, joins, etc. from others.

"Best" and "most efficient" are not necessarily congruent. Sometimes a good solution (easy to write, easy to understand, easy to maintain) performs horribly. Sometimes the most efficient solution requires query gyrations that I would not classify as a good solution. In your case, where the tables are of reasonable size, indexes will be important no matter what you do.

You are right that you can achieve what you want several ways -- subselects, joins, and special operators.

Let's use table1 and table2 as our example tables, and let's assume we want to check for different records in both of them.

The subselect method goes like this --

```select table1.columns
from table1
where not exists
(select 1 from table2
where table2.id = table1.id)```

This gives you all the rows in table1 that don't have matching rows in table 2. Note that in the subselect after the word SELECT it is necessary to select something, so conveniently choose the integer 1 instead of a table column -- it could be anything, really (including the asterisk, but that's a different subject for another day). Since a NOT EXISTS will always evaluate only true or false, the subselect doesn't need to return anything other than an indication that a row was or was not found. (If this sounds familiar, it's my standard spiel about the EXISTS subselect, which I last used in this answer.)

We also want to check for rows in table2 that don't have matching rows in table 1, and this second query is like the previous one, but with the tables reversed --

```select table2.columns
from table2
where not exists
(select 1 from table1
where table1.id = table2.id)```

The second method involves using left joins instead of subselects --

```select table1.columns
from table1 left join table2
on table1.id = table2.id
where             table2.id is null```

This may sound a little weird, joining on a column and checking it for nulls, but that is exactly what to do to find those rows of table1 which do not have a matching row from table2. In a left join, the database places nulls into all the columns from table2 when there is no matching row from table2.

And to find all the rows from table2 that are different, that don't have a match in table1, we could use "table1 right join table2" instead of a left join, but that just confuses things unduly and I prefer to write left joins in all cases --

```select table2.columns
from table2 left join table1
on table2.id = table1.id
where             table1.id is null```

The third method is the "best" solution in my opinion, because it uses SQL language operators intended for just this situation. However, not all databases implement these operators.

To find all the rows of table1 that do not exist in table2, use this query --

```select table1.columns
from table1
except
select table2.columns
from table2```

The EXCEPT operator is called MINUS in Oracle.

We also want the rows of table2 that aren't in table1, and the query for that is, yup, you guessed it --

```select table2.columns
from table2
except
select table1.columns
from table1```

As for efficiency, the database will determine its own access strategy -- for instance, subselects are usually implemented as though they were joins anyway. I haven't seen how the EXCEPT operator is implemented, but it's fair to assume that it will be just as efficient as the other methods. Don't forget your indexes on the primary keys!

This was last published in May 2001

## Content

Find more PRO+ content and other member only offers, here.

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Hyperledger Fabric offers path to enterprise blockchain future

Blockchain arose from bitcoin, but it's looking to find a place in the enterprise. Frameworks like Hyperledger Fabric could ...

• ### MongoDB 4.0 takes ACID transactions to multi-document level

MongoDB is taking a deeper step into SQL-style processing waters with a 4.0 update that brings increased support for ...

• ### Data lake concept needs firm hand to pay big data dividends

Data lakes pose technology deployment and data management challenges that can leave analytics users high and dry if the ...

• ### AI functionality limited today but could be a game-changer

Limited AI capabilities could soon give way to technology that is truly transformative for enterprises, surpassing the overhyped ...

## SearchSAP

• ### ControlPanelGRC app eases Steelcase's compliance pain

When Steelcase's SAP environment grew in size and complexity, it turned to Symmetry ControlPanelGRC to save time, have more ...

• ### Translytical data platforms emerge with SAP HANA as a leader

SAP HANA is a leading translytical platform, according to Forrester, and consulting firm Convergent IS says the combination of ...

• ### SAP HANA and Esri combine for geospatial database platform

SAP and Esri are combining SAP HANA's in-memory database capabilities with Esri geospatial applications, and utilities are taking...

## SearchSQLServer

SQL Operations Studio simplifies routine administration of SQL Server and Azure SQL databases, making database development and ...

• ### Meltdown and Spectre fixes eyed for SQL Server performance issues

Microsoft has responded to the Spectre and Meltdown chip vulnerabilities with patches and other fixes. But IT teams need to sort ...

• ### Five SQL Server maintenance steps you should take -- ASAP

Putting off SQL Server administration tasks can lead to database problems. Enact these often-neglected maintenance items to help ...

## TheServerSide.com

• ### Pluralsight IQ, Stack Overflow boost developer street cred

Tying the Pluralsight IQ skills test to the Stack Overflow Developer Story helps developers measure their technical skills and ...

• ### Why this quantum computing breakthrough is a security risk

Quantum computing will void pretty much all security encryption techniques and open the door to hackers. Here's how to protect ...

• ### Database automation drives DevOps into the persistence layer

A new breed of software tools is driving DevOps processes deep into the persistence layer, as database automation and continuous ...

## SearchDataCenter

• ### Evaluate read-intensive and write-intensive SSD use cases

Consider write wear, performance and other factors when choosing between read-intensive, write-intensive and mixed-use ...

• ### Some hyper-converged infrastructure use cases pose pitfalls

Hyper-converged infrastructure adoption is skyrocketing, but that doesn't mean that the technology is the best choice for every ...

• ### Dell hyper-converged reorg streamlines products, ups CI odds

Market pressures and manufacturing synergies drove Dell to integrate its HCI and CI products with its core business units, but ...

## SearchContentManagement

• ### Content management in the cloud a main theme in 2018

The future of content management resides in the cloud and with AI, as several 2018 conferences will assure you.

• ### Six things to know about today's SharePoint implementations

As companies migrate their on-premises Microsoft SharePoint sites to the cloud, here are some things they should know about the ...

• ### Upgrades for the SharePoint Online portal

As more organizations migrate SharePoint sites to the cloud, Microsoft has increased at-a-glance dashboard data and analytics to ...

## SearchHRSoftware

• ### Don't overlook the many benefits of Microsoft Excel for HR

The maligned spreadsheet tool is no substitute for enterprise apps like HRMS and people analytics, but it will do in a pinch and ...

• ### HR is failing to use people analytics tools, new report says

Human resource departments fail to use people analytics tools effectively, a new global study concludes. The findings were called...

• ### Does your company need new human resource management tools?

Finding the best human resource management tools starts with identifying your company's present and future needs, before ...

Close