Skip to content
Advertisement

How to exclude certain entries from a query [SQL]

I do have the following issue: Let’s say you have a table that looks like this:

"ExampleTable"

NotPrimID     Number
0             13
0             13
0             14
1             14
1             14
2             13
2             13

Question: I want to have an query which will deliver all the NotPrimID‘s which do have the Number as 13, however if the Number of a NotPrimID is also 14 etc. it should be automatically excluded from the list.

Advertisement

Answer

You could use exists logic here:

SELECT DISTINCT NotPrimID
FROM ExampleTable t1
WHERE Number = 13 AND NOT EXISTS (SELECT 1 FROM ExampleTable t2
                                  WHERE t2.NotPrimID = t1.NotPrimID AND t2.Number = 14);

Demo

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement