Skip to content Skip to sidebar Skip to footer

How To List Each Pair Of Tuple Only Once Irrespective Of Column Order In Sql And Relational Algebra?

I'm working on some book exercises and can't find an explanation on how to express the following in relational algebra. I did find an answer for SQL though but I'm interested in wh

Solution 1:

Just use the fact that if PC.model != PC1.model, then one is smaller than the other. So if you need one of these pairs, just use either PC.model < PC1.model or PC.model > PC1.model (depending on which pair you want to preserve).

SELECT PC.model, PC1.model
FROM   PC, PC AS PC1 
WHERE  PC.model < PC1.model AND PC.speed = PC1.speed AND PC.ram = PC1.ram;

Solution 2:

Here is one option:

SELECTDISTINCT LEAST(pc1.model, pc2.model),
                GREATEST(pc1.model, pc2.model)
FROM PC pc1
INNER JOIN PC AS pc2
    ON pc1.model <> pc2.model
WHERE pc1.speed = pc2.speed AND
      pc1.ram = pc2.ram;

Post a Comment for "How To List Each Pair Of Tuple Only Once Irrespective Of Column Order In Sql And Relational Algebra?"