Relational Algebra – How to Delete Duplicate Keys

relational-algebrarelational-calculusrelational-theory

Hi I am new to databases and relational algebra. I was wondering if there is a way to remove the tuples from a table using relational algebra that have the same keys but different value.

e.g. I want to keep only [1, 5] and [4, 9] but remove everything else.

Key    | Value
-------|-------
 1     | 5
 2     | 6
 2     | 7
 2     | 8
 4     | 9

Thanks.

Best Answer

There are many variants and extensions of relational algebra described in books. I assume that you have a group by operator, which is an extension of the classical relational algebra, and it is written as:

a1... an γ f1...fm

where each ai is a grouping attribute, and fi is an aggregation function.

Using this operator, your query could be answered by the following expression (assuming that the name of your relation is R):

R ⨝ πKeyCOUNT(*)=1(Key γ COUNT(*) (R)))

First we group by Key and keep only the groups with a unique value for it, and then we perform a natural join on R itself to maintain only the tuples with just one value of Key.