?
Solved

Set notation vs SQL

Posted on 2011-09-14
2
Medium Priority
?
391 Views
Last Modified: 2012-06-21
Scenario: In my organisation I want to identify the PCs having only authorised software installed.

To do this I could compare the applications installed on each PC with my list of authorised applications.  Using set theory / notation the solution is easy to express i.e. a PC is considered authorised if its applications are a subset of the authorised list.
What I’m trying to get my head around is how this ‘solution’ could be implemented using SQL? I’m after some sound pointers or approaches.
Thanks.
0
Comment
Question by:antonms
2 Comments
 
LVL 21

Expert Comment

by:JestersGrind
ID: 36536965
Instead of looking at the PCs that only have authorized software installed, you could exclude the PCs that do not have authorized software installed.  That would be an easier query to write.

Greg



0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 36536969
antonms,

The principal is the same. Another way to look at this under set theory / notation is that no member of the PC's set of applications is outside the set of authorised ones.

Therefore, in SQL you can use EXISTS and NOT EXISTS to your favor. Basically, you can query for the rows where there does not exist another row for the same PC that has an application not listed on the authorised list, i.e., an application that does not match to a row in your authorised applications table.

In general terms:

SELECT {column list}
FROM A a1
WHERE NOT EXISTS (
   SELECT 1
   FROM A a2
   LEFT OUTER JOIN B b1 ON a2.AppID = b1.AppID
   WHERE a2.PcID = a1.PcID AND b1.AppID IS NULL
)

You can do the second piece with a NOT EXISTS as well, but wanted to show OUTER JOIN is also an option. But with NOT exists, you can find the PCs that have an invalid app but finding that the app does not exist in the authorised apps table. Then to find PCs with only authorised apps, you check for the inverse of that list. So top level could also use NOT IN if you replace SELECT 1 with SELECT PcID and remove filter on PcID in the inner queries WHERE clause.

Hopefully that isn't too convoluted an explanation and everything makes sense.

Kevin
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question