Delete duplicate keys with relation algebra 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.
I would do something like the following (see the fiddle here):

Create and populate a table:

CREATE TABLE test
(
  key INTEGER NOT NULL,
  value INTEGER NOT NULL
);

INSERT INTO test VALUES
(1, 5), (2, 6), (2, 7), (2, 8), (4, 9);


and then:

SELECT * FROM test;


Result:

key     value
  1         5
  2         6
  2         7
  2         8
  4         9


and then run the following SQL:

SELECT key, value
FROM test WHERE key IN
(
  SELECT key
  FROM test
  GROUP BY key
  HAVING COUNT(key) = 1
  ORDER BY key
);


Result:

key     value
  1         5
  4         9


Which is the desired result. I have used PostgreSQL - the Open Source database which I would recommend that you use to learn relational database principles - it has a very high level of standards compliance and is a great learning tool.

The inner loop of SQL 

SELECT key
FROM test
GROUP BY key
HAVING COUNT(key) = 1
ORDER BY key


produces those keys which have a COUNT of only 1 (in this case, the integers 1 and 4) and then the outer loop selects the values corresponding to those two integers (see the fiddle).