Solved

Set notation vs SQL

Posted on 2011-09-14
2
380 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 59

Accepted Solution

by:
Kevin Cross earned 500 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server How-To Show Notes In First Row of Results 4 32
Error when creating an UPDATE Trigger in SQL 6 24
SQL, add where clause 5 24
xml files 7 29
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

820 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