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

# Query 2 should not include results from Query 1

## We have a Users table with over 3 million user records in it (like a data warehouse). The requirement is to execute two queries on the Users table, where the return of the second query should not have any users that were returned in the first query.

We have a Users table with over 3 million user records in it (like a data warehouse). The requirement is to execute two queries on the Users table, where the return of the second query should not have any users that were returned in the first query.

For example, consider a sweepstake user list, where you want to send flyers to people, notifying them about prizes, but do not want to send two flyers to the same person, even if he fulfills the selection criteria for both sweepstakes.

So --

Query 1 has conditions which state: All Users from New York, with age=25 (independently run, returns 500,000 users)

Query 2 has conditions, which state: All Users of age=25 (independently run, returns 700,000 users)

Expected Output:

Query 1 should return 500,000

Query 2 should return 700,000 MINUS those users that were already returned in Query 1, i.e., only users of age 25, but who are not from New York

Any inputs will be much appreciated!

Fascinating question. Let's assume we need separate mailout lists, one for each query, and let's assume we need separate result sets (different columns) and separate filter conditions for each query. There are three ways to write the queries. Here's one way, using NOT EXISTS:

```Query 1
select query-1-columns
from Users
where query-1-conditions
```
```Query 2
select query-2-columns
from Users as X
where query-2-conditions
and not exists
( select userid
from Users
where userid = X.userid
and query-1-conditions )```

In addition to NOT EXISTS, you can also use NOT IN or LEFT OUTER JOIN syntax. See my recent answer The difference between NOT IN and NOT EXISTS.

However, if we need just one result set, if we were doing just one mailout but still had different conditions for different sets of users, just with no overlap, then another look is warranted.

In the given example, the specific conditions were inclusive. In other words, if we need only one result set, we can run just one query with age=25 and get everybody, New York or not. It's always nice to eliminate a step that isn't necessary.

If the conditions aren't inclusive, but it's still okay to return only one result set (same columns), just not the same person twice, then a simple UNION will suffice:

```select query-columns
from Users
where query-1-conditions
union
select query-columns
from Users
where query-2-conditions```

UNION removes duplicates.

An equivalent query is:

```select query-columns
from Users
where ( query-1-conditions )
or ( query-2-conditions )```

These two queries will not necessarily perform differently, but they might.

#### 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

• ### Amazon cloud database and data analytics expand

AWS expands its Redshift data warehouse capabilities including managed storage and query acceleration at the re:Invent 2019 ...

• ### Vendors move away from open source database software licensing

While open source is a key to success for many database vendors, it can also potentially lead to a competitive threat from a ...

• ### Oracle looks to grow multi-model database features

Decade after decade, Oracle continues to be relevant in the database market as it pivots to include an expanding list of ...

• ### Sisense analytics platform update features AI, data prep tool

Augmented intelligence features and an in-warehouse data prep tool highlight Sisense's latest update, its second since acquiring ...

• ### Operation Fistula uses data to treat childbirth injuries

Aided by an analytics stack developed by Exasol, nonprofit Operation Fistula is working to end obstetric fistula in targeted ...

• ### Automated tools broaden the future scope of data science

Automated data science tools may make their way into the data science process, but are unlikely to ever be able to do everything ...

## SearchSAP

• ### Partners need to target SAP SMB market for cloud transition

In this Q&A, SAP executive Claus Gruenewald discusses how SAP partners are instrumental in servicing the SMB market and how they ...

• ### S/4HANA Cloud integrates Qualtrics for continuous improvement

The latest version of SAP S/4HANA Cloud embeds Qualtrics XM capabilities that provide a way for users to give direct feedback ...

• ### SAP Data Warehouse Cloud may democratize data analytics

SAP's new SAP Data Warehouse Cloud promises to make data analytics more accessible to the business. It's a needed addition to ...

## 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

• ### Public API strategy considerations for enterprise adoption

As organizations look for more cost-effective ways to manage data, an evolving landscape with APIs has made the technology more ...

• ### Ideas on how to hold a successful code hackathon

Want to host a hackathon? Here are some ideas on what a company can do to host an event that solves problems and reenergizes the ...

• ### A Jenkins tutorial for beginners with examples

If you're ready to try Jenkins, this quick start tutorial lays out the specific steps you'll need to take to download and install...

## SearchDataCenter

• ### Top 4 open source automation tools for admins

Open source offerings are an easy way to bring automation into your organization. When selecting software, evaluate the user ...

• ### U.S. facility may have best data center PUE

The data center energy efficiency metric, power usage effectiveness, or PUE, is not improving. But one supercomputer data center ...

• ### Increase efficiency with data center temperature monitoring

Data centers must track multiple temperature components. Organizations can use ASHRAE standards and on-premises hardware for ...

## SearchContentManagement

• ### New capabilities added to Alfresco Governance Services

The updates are intended to secure electronically stored information residing across different locations in light of recent ...

• ### Top 5 Office 365 skills for SharePoint administrators

Microsoft SharePoint administrators need to branch out in their Office 365 skill set to support products tightly integrating with...

• ### Box cloud content management helps simplify digital work

Box currently has three main areas of focus: integration with enterprise applications, content security and collaboration. The ...

## SearchHRSoftware

• ### 3 keys to employee engagement measurement

Far too many organizations and their HR departments conduct employee engagement measurement in a way that does more harm than ...

• ### Cornell researchers call for AI transparency in automated hiring

Researchers at Cornell University want vendors to disclose how automated hiring systems are making hiring recommendations. They ...

• ### Why reducing hiring bias isn't easy

Because people may harbor hidden biases, diversity and inclusion initiatives can be difficult to execute. Take an in-depth look ...

Close