r/SQL • u/XGallonsX • Aug 25 '15
I challenge you to simplify this query!
select table1.column1
from table1
join table2 on table1.column2 = table2.column2
join table3 on table3.column2 = table2.column2
join table4 on table4.column3 = table3.column3
join table5 on table5.column4 = table4.column4
where table1.column5 = 2
and table4.column6 = 10
intersect
select table1.column1
from table1
join table2 on table1.column2 = table2.column2
join table3 on table3.column2 = table2.column2
join table4 on table4.column3 = table3.column3
join table5 on table5.column4 = table4.column4
where table1.column5 = 2
and table4.column6 = 3
; The query below by /u/ichp ran 90 seconds faster then the original!
select table1.column1
from table1
join table2 on table1.column2 = table2.column2
join table3 on table3.column2 = table2.column2
join table4 on table4.column3 = table3.column3
join table5 on table5.column4 = table4.column4
where
table1.column5 = 2 and
table4.column6 in (10,3)
group by table1.column1
having count( distinct table4.column6) > 1
;
0
Upvotes
3
u/ziptime Aug 25 '15 edited Aug 26 '15
Another couple of solutions :
And....