Skip to content
Advertisement

Get exclusive users in each table

I have 4 tables as shown below

enter image description here

For each table I want the count of users that are present exclusively in that table (not present in other tables). The result should look something likes this

enter image description here

I have one way of getting desired result as shown below:

First Column:

Second Column:

Third Column:

Fourth Column:

But I wanted to know if there is any efficient and scalable way to get same result. Just for 4 tables the amount of code is too much.

Any ways of optimizing this task will be really helpful.

Sample fiddle. (This fiddle is for mysql, I am looking for a generic SQL based approach than any db specific approach)

P.S.: There is no complusion on the result needs to be in column wise. It can be row wise as well, as shown below:

enter image description here

Advertisement

Answer

I would approach this by combining the data from all tables. Then aggregate and filter:

Note: In your sample data, the ids are unique in each table. This solution assumes that is true, but can easily be tweaked to handle duplicates within a table.

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