< Blog

The Subselect Query

By Garth Jones

In my blog posts from last week I talked about how queries are processed and how a Not Equal To operator is processed.

In this blog post I will help you to write a subselect query in order to determine what PCs are missing a specific type of software.

A subselect query is made up of two parts: the positive query and the negative query. This might seem backwards, but in order to ensure that you get the results that you are looking for you need to exclude all PCs with a positive result.

Let’s use the same table from the previous blog posts for all of our example queries. This table is comprised of 4 PCs and their add/remove program (ARP) details.

PC Name

PC1

PC2

PC3

PC4

1

Adobe Reader XI

Microsoft Project 2010

Visio 12

Adobe Reader X

2

Visio 12

CorelDraw

ITunes

Microsoft Project 2010

3

ITunes

Office 2010

WordStar

DataStar

4

Microsoft Office 2013

Lotus123

5

Kix 2010

Using pseudocode I will write the negative query this way:

Find all PCs where the ARP display name is Not Equal To Microsoft Project 2010

A lot of people would expect to get PC1 and PC3 returned in the result set, but that is not the case as I demonstrated in my previous blog post.

However, in order to achieve the desired results we need to break the query into two parts as I explained earlier; a positive query and a negative query.

Here’s the pseudocode for the positive query:

Find all PCs where the ARP display name is Equal To Microsoft Project 2010

If you think that PC2 and PC4 should be returned to the result set then you would be correct, but we only want to know what PCs do not have Microsoft Project 2010 installed.

This brings us to the subselect query. After using the positive query, we know which PCs have Microsoft Project 2010 installed. Now, combining it with the negative query we will find out which ones do not have Microsoft Project 2010 installed.

I re-write the pseudocode combining both the positive and negative queries:

Find all PCs where the PC is not in (Find all PCs where the ARP display name is Equal To Microsoft Project 2010)

First this query will find all of the PCs with Microsoft Project 2010 installed and then it will exclude those PCs from the list of all PCs. Therefore the end result will only display those PCs which are not in the bracketed part of the query. Finally, PC1 and PC3 show up in the result set!

Up until this point, we have talked about how to write queries in a generic sense, but these same principles can be applied to either SQL or WQL queries. Now that you know more about how queries are processed, the next step is to create queries for customized reports (SQL) or collections (WQL).

In my next two blog posts, I will show you how to create queries for both of these items.

The Subselect Query