EDIT: My question was deficient because it was misunderstood. I’m rephrasing it. The original question is below the HR for the curious…
Given several SQL queries that operate on the same data, how can I assert in the type system, object model or test code that each possible row is selected by exactly one of the queries:
i.e. in this Venn diagram (thank you @Imre_L for the image) what is the best way to assert that queries A, B and U do not overlap?

Note the problem space is all possible data and not just the existing dataset.
The original question
I’m trying to devise a safe and easy to understand mechanism for composing a set of where clauses where the subsets of selected rows never overlap, yet execution of all clauses would select the set of all rows.
For example given a clause (in pseudo SQL) like:
... where name like 'Bob%'
and surname not like '%Smith'
and postcode in (2000, 2010, 2020)
and another like
... where name in ('Alice', 'Jane')
and postcode < 9000
and married=True
and a final query to catch the remainder
... where not (name like 'Bob%' and surname not like '%Smith' and postcode in (2000, 2010, 2020))
and not (name in ('Alice', 'Jane') and postcode < 9000 and married=True)
How can I assert that each possible row is selected by exactly one of the possible queries?
My team is slowly migrating behaviour from one part of the system to another and need, at each stage, to alter the set of statements so that some rows selected by one statement are selected by a different one after the change. The assertion above needs to hold true before and after each change.
I’m looking for a neat solution for Java. Whether it’s checked in the type system, or controlled by some creation pattern or even checked during testing it doesn’t matter. I’m sure some boffins have some good ideas out there.
This is wrong – the question is for all possible data.
I am not sure I understand, but SQL includes
unionanddistinctso you could check the count for:All queries unioned together
All queries unioned together, distinct rows
All rows in the table
And they should all be equal. If you are missing a row completely then 2 will be less than 3; if you are duplicating a row anywhere then 2 will be less than 1.
The queries for 1 and 2 would be: