Table A is a list of IDs.
ID
---
1
5
9
...
Table B has IDs and another Y/N value.
ID Value
----------
1 0
2 1
3 0
...
I'd the count of Value for IDs that are:
i) both in tables A & B,
ii) in A but not B,
iii) in B but A.
One way is to use union:
SELECT Value, COUNT(*) AS count FROM tableA INNER JOIN tableB USING (id) GROUP BY Value
UNION
SELECT Value, COUNT(*) AS count FROM tableA RIGHT JOIN tableB USING (id) WHERE tableA.id IS NULL GROUP BY Value
Is there a better way?
You want ids from both tables, which reads like a
full join(if your database, which you did not tag, does support it). We can then compute the counts with conditional expressions.This assumes that ids are unique in both table, as shown in your sample data - otherwise we would need to deduplicate both datasets first.
In databases that do not support full joins, we would likely emulate it with
unionand subqueries.