Skip to content
Advertisement

Oracle SQL: How to select only ID‘s which are member in specific groups?

I want to select only those ID‘s which are in specific groups. For example:

Here I want to select the ID’s which are in the groups 11 and 12 but in no other groups. So the result should show just the ID 1 and not 2. Can someone provide a SQL for that? I tried it with

But that didn’t work.

Advertisement

Answer

You can use aggregation:

This having condition ensures that the given id belongs to groupIDs 11 and 12, and to no other group. This works because 11 and 12 are sequential numbers.

Other options: if you want ids that belong to group 11 or 12 (not necessarily both), and to no other group, then:

If numbers are not sequential, and you want ids in both groups (necessarily) and in no other group:

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