Skip to content
Advertisement

Query table with composite primary key, excluding specific rows

I would like to know which unique ids belong to category A but NOT category B. In the example above, only id: 2 would be a valid result

I came up with this which does not work as intended:

A valid approach would be to use a subquery

Is it possible to do this with a single query?

Advertisement

Answer

Use a self-join:

Another way is with grouping:

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