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

# Comparing one set of rows with another

I have a question about comparing one set of rows with another set of rows for equality. The rows can be in the same table, different tables, pointed to by different cursors or result sets. Specifically, given this tip on trees from Joe Celko: Trees in SQL.

If I have a new node with subordinate nodes in a separate table called NewNode, how do I check to see if such a node already exists in my 'real' Personnel table? In other words, what is the best way to compare one set of rows to another set of rows? Do I have to programmatically iterate through both cursors/result sets/etc., comparing column values explicitly? Or is there some SQL magic you can share?

You know, that adjacency tree stuff is way over my head, and I'm not just trying to make a joke. No offence, Joe, but nested ovals, while easy to visualize, just don't translate into SQL very easily for me. I'll take your word that the increase in efficiency is worth the effort. But advice like "To convert an adjacency list model into a nested set model, use a push down stack algorithm" makes me shudder. I have a lot of respect for people who can handle that kind of code in their stride.

Let's focus on the question "What is the best way to compare one set of rows to another set of rows?" For me, the best way to operate on sets is to use the set operators provided in SQL-92: UNION, EXCEPT, and INTERSECT.

For example, to find the rows of table1 that are not also in table2, you just "subtract out" the rows that are (in fact, in Oracle the EXCEPT operator is called MINUS) --

```     select * from table1
except
select * from table2```

If you get no rows back, then all the rows of table1 are also in table2, which is what you wanted.

You can achieve the same effect using an old-fashioned OUTER JOIN --

```     select table1.*
from table1
outer join table2 on commonid
where table2.commonid is null```

Again, you'd get no rows returned if all the rows of table1 are also in table2.

To ensure that table2 has no extra rows that aren't in table1, you'd also want to do the EXCEPT or OUTER JOIN in the other direction.

• The Best SQL Web Links: tips, tutorials, scripts, and more.
• Have a SQL tip to offer your fellow DBA's and developers? The best tips submitted will receive a cool prize--submit your tip today!
• Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### PlanetScale extends multi-cloud database as a service

Open source-based PlanetScaleDB adds support for Microsoft Azure, enabling a multi-cloud database as a service platform that ...

• ### Oracle NoSQL database comes to the cloud

Another Oracle database is coming to the cloud with the general availability of Oracle NoSQL Database Cloud Service as the demand...

• ### MariaDB SkySQL enables cloud-native database as a service

MariaDB brings back an old name for its cloud-native database-as-a-service platform. MariaDB CEO Michael Howard details how he's ...

• ### Importance of analytics clear amid COVID-19 crisis

With health organizations and business leaders alike trying to minimize the effects of COVID-19, analytics is playing a critical ...

• ### AI tools in analytics software key in fighting COVID-19

With data skewed by incomplete testing, augmented intelligence and machine learning tools are helping data scientists create ...

• ### Should your company hire a freelance data scientist?

Enterprises need to make the right hiring choices when it comes to supporting their data projects. Here are some reasons your ...

## SearchSAP

• ### SAP S/4HANA deployment options: Legacy ERP vs. cloud

Take this tour of the on-premises S/4HANA and its two cloud siblings and get expert advice on major differences in cost and ...

• ### Floating the possibilities of SAP S/4HANA Cloud migration

When weighing the advantages of abandoning legacy ERP systems for SAP S/4HANA Cloud, factors include implementation and support ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Eclipse launches Theia 1.0 as alternative to VS Code

The Eclipse Foundation's Theia framework for building desktop and cloud IDEs has the potential to be as big as the initial ...

• ### Developing a COVID-19 application? Design it the right way

Application developers with skills and an idea could design an app to combat the virus. Follow these insights to effectively ...

• ### How developers can volunteer during the COVID-19 pandemic

What resources can developers offer to go against COVID-19? Explore some promising developer volunteer and open source project ...

## SearchDataCenter

• ### Build a migration plan for enterprise edge infrastructure

Micro data centers are essential to edge computing use cases. Build an effective migration plan through site selection, ...

• ### Tech giants aim supercomputers, clouds at finding a COVID-19 cure

The new COVID-19 HPC consortium comprised of tech giants, national labs and academia are using supercomputers and clouds to speed...

• ### Top vendor SDDC certification programs for admins

If you decide to grow your knowledge of software-defined data center technologies, VMware, Nuage Networks and Microsoft offer ...

## SearchContentManagement

• ### Creating digital experiences key to driving exceptional CX

The way in which businesses build digital experiences is ever-evolving. It's a race to create easier-to-use customer interfaces ...

• ### Vendors offer free remote work technology for telecommuting

It's more than a marketing ploy: Cloud technology vendors, seeing their own issues enabling remote work, extend paid products to ...

• ### How businesses should deal with enterprise search issues

Enterprise search issues frequently complicate user experience with ECM systems. While users may face many problems, they also ...

## SearchHRSoftware

• ### How to create an employee journey map

This handy guide shows you how to build your own employee journey map, which is more important than ever as the coronavirus ...

• ### Remote team tech Sneek keeps co-workers in view

Sneek, a remote team platform, uses frequently updated photographs of remote workers to connect co-workers together.

• ### Best-case, worst-case pandemic scenarios for India service providers

India's IT and business support services will be tested during that country's pandemic lockdown. Many workers are now operating ...

Close