I am trying to solve an sql exercise.
Here’s the schema
PC
code int
model varchar(50)
speed smallint
ram smallint
hd real
cd varchar(10)
price money
The problem :
Find the pairs of PC models having
similar speeds and RAM. As a result,
each resulting pair is shown only
once, i.e. (i, j) but not (j, i).
I have written a query but it displays (i, j) along with (j, i).
My query :
select t1.model,t2.model,t1.speed,t1.ram from pc t1 , pc t2
where t1.speed = t2.speed and t1.ram = t2.ram and t1.model != t2.model
Output :
model model speed ram
1121 1233 750 128
1232 1233 500 64
1232 1260 500 32
1233 1121 750 128
1233 1232 500 64
1260 1232 500 32
Required output:
model model speed ram
1233 1121 750 128
1233 1232 500 64
1260 1232 500 32
So how do I avoid (j ,i) in my output?
Thanks.
The difference between your output and the required output are exactly the rows that have
t1.model < t2.model. To remove those, just add anotherAND t1.model >= t2.model. But because you already require thatt1.model != t2.model, the complete query is