I have following query:
select id from t1
intersect
select id from t2
intersect
select id from t3
id could be non unique in some tables, so I need to use distinct.
In general what is better:
select distinct id from (
select id from t1
intersect
select id from t2
intersect
select id from t3)
or
select distinct id from t1
intersect
select id from t2 -- here id is unique
intersect
select distinct id from t3
There is no need for the
DISTINCT. TheINTERSECToperator automatically produces a distinct set of values. As you can see in this example,xhas two rows with anIDof 1,yonly has one row with anIDof 1 and theINTERSECTIONof the two produces just a single rowEven if you take the
INTERSECTof the two row table with itself, you still get a single row in the output