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

# Another WHERE EXISTS example

I selected what I need from PARTDATA table and put some conditions and this is what my data looks like so far...

```PDate   Part   Data    Results  Final
11-Feb  11234  Length   22.6    OK
11-Feb  11234  Width     2.3    NOT OK
11-Feb  11234  Height   34.67   OK
14-Feb  11222  Length   25.60   OK
14-Feb  11222  Width     4.20   OK
14-Feb  11222  Height   35.00   OK
15-Feb  11546  Length   23.00   OK
15-Feb  11546  Width     4.30   OK
15-Feb  11546  Height   45.00   NOT OK
16-Feb  12789  Length   24.50   OK
16-Feb  12789  Width     5.00   OK
16-Feb  12789  Height   33.70   OK```

But I'm trying to get a list of Parts that have a "NOT OK" under Final column.

```PDate   Part   Data    Results  Final
11-Feb  11234  Length   22.6    OK
11-Feb  11234  Width     2.3    NOT OK
11-Feb  11234  Height   34.67   OK
15-Feb  11546  Length   23.00   OK
15-Feb  11546  Width     4.30   OK
15-Feb  11546  Height   45.00   NOT OK```

Is it possible? Thanks!

Yes, it's easy. Basically, your query looks something like this:

```select PDate
, Part
, Data
, Results
, Final
from PARTDATA
where some conditions
order
by PDate
, Part```

Now all you have to do is give the table a table alias, which you then use as a correlation variable in a correlated subquery:

```select PDate
, Part
, Data
, Results
, Final
from PARTDATA as O
where some conditions
and exists
( select *
from PARTDATA
where PDate = O.PDate
and Part  = O.Part
and some conditions
and Final = 'NOT OK' )
order
by PDate
, Part```

The subquery examines all the rows in the same group as each row in the outer query. The groups are defined by the same values of PDate and Part. Each row in the subquery is correlated to each outer row in its group. All that's required to get a TRUE result for the EXISTS condition is for any one of the correlated rows in the group to match the 'NOT OK' condition. If none of the correlated rows matches, then all the rows from that particular PDate and Part group will be excluded from the results.

A correlated subquery thus performs grouping. It just doesn't use a GROUP BY clause to do so.

#### Have a question for an expert?

Please add a title for your question

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

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

### 1 comment

Send me notifications when other members comment.
Actually, since you don't really need any of the data coming from the subquery, it has been considered a good practice to not use "select *", but something like "select 1" or similar.
Cancel

## SearchDataManagement

• ### Hitachi Vantara acquires data catalog vendor Waterline Data

With the acquisition of Waterline Data, Hitachi Vantara is bringing new data catalog capabilities that will expand the Lumada ...

• ### New Confluent Platform release boosts event streaming quality

Based on the open-source Kafka event streaming platform, the Confluent Platform 5.4 update adds new capabilities to help meet ...

• ### Where InfluxDB time series database is going

Users need more than SQL for querying databases, according to Paul Dix, co-founder and CTO of InfluxData. That's why the vendor ...

## SearchBusinessAnalytics

• ### ThoughtSpot IPO could be coming after vendor adds first CFO

Hiring of a CFO for the first time signals that ThoughtSpot may be positioning itself for an IPO and comes six months after what ...

• ### 5 ways enterprises adapt to the data scientist shortage

Where are all the data scientists? Coping with the data scientist shortage is a struggle for many enterprises. Here are five ways...

• ### Storytelling using data makes information easy to digest

In a Q&A, Nate Nichols and Anna Schena Walsh of AI-based analytics vendor Narrative Science talk about how data storytelling can ...

## SearchSAP

• ### SAP S/4HANA migration: Critical advice for moving off ECC

With the end of SAP ECC support looming in 2025, organizations must make some tough decisions. Here's a look at your choices.

• ### New SAP leadership faces big challenges in 2020

Industry analysts discuss SAP's biggest issues in 2020, including how the two new CEOs will guide the company deeper into the ...

• ### SAP Data Hub opens predictive possibilities at Paul Hartmann

When medical supply firm Paul Hartmann AG tested a supply chain analysis system built on SAP Data Hub, it found that it could ...

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

• ### Don't ever put a non-Java LTS release into production

Development teams should avoid non-long-term support releases at all costs. Pay attention to the Java release cycle to make sure ...

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

## SearchDataCenter

• ### Top data center skills admins can use in 2020

The 2019 tech job sector saw consistent growth and job availability. In 2020, admins should develop expertise on cloud ...

• ### Organizations try to predict the effect of 5G infrastructure

With more 5G and IoT devices emerging, admins must prepare their data centers to support low-latency apps and edge computing with...

• ### Top infrastructure and operations technology myths of 2019

Admins are consistently evaluating technology to improve I&O efficiency. Cost, integration and business goals are key components ...

## SearchContentManagement

• ### 4 popular content collaboration platforms to consider

Companies need to be organized if they want to be efficient. Content collaboration platforms are useful, but first, ensure that ...

• ### AI can enhance content security with a bit of planning

Microsoft and Box both use AI technologies to keep content secure in the cloud. But before using such tools, businesses first ...

• ### Ex-SAP exec steers Episerver CMS toward digital experience market

Alex Atzberger discusses leaving the helm of SAP's CX platform to become Episerver CEO. Now, Episerver looks to reinvent itself ...

## SearchHRSoftware

• ### Critical tips for managing contingent workers

Contingent workers save companies both time and money, so it's important to manage them in a win-win way. Here is what HR teams ...

• ### Impact of AI on jobs goes on the presidential campaign trail

The impact of AI on jobs is a major issue for employers, who are struggling with how to address it. Robots, automation and AI ...

• ### Why mobile recruiting is the future

Recruiters can use text recruiting to connect with great candidates. Here's a look at how mobile recruiting works, why it's ...

Close