Link to home
Start Free TrialLog in
Avatar of andieje
andieje

asked on

get intersection of 2 subqueries

Hi

If i had these 2 queries:

select * from a inner join b inner join c where field = X

select * from a inner join b inner join c where field = Y

what is the best way to find the rows from table a that are in both result sets? would i have to do a self join on the query results? Please could you advise how and how basic example query showing how to do this

thanks
Avatar of Jagadishwor Dulal
Jagadishwor Dulal
Flag of Nepal image

It will be better if you can post field name of your both table. Yes you must use a join query for this.
you are applying where criteria on the same field and join fields are same. then i believe there should not be any overlap between first query result and second query result.
Both the queries will give different result set and there won't be common records because field value is different in both queries and you have * in SELECT clause. post the result of both the queries and the expected result
like in the other query, you can "join" 2 times the same table...

select * 
from a 
join b  on a.field = b.field
join c c1 on c1.field = b.field and c1.field = X
join c c2 on c2.field = b.field and c2.field = Y

Open in new window


c1 and c2 are aliases in the query for the same table, make the table "c" is used 2 times independantly (like 2 copies of the same table, but only logically, of course)

hope this helps
Avatar of andieje
andieje

ASKER

ok, sorry. I posted the question late at night and i can see i have messed up the question. Sorry about that.

select a.* from table a inner join b inner join c where 'some conditions'

select a.* from table a inner join d inner join e where 'some conditions'

So i have 2 queries which are selecting records from table a by totally different criteria based on joins to different tables

I want to find the records which are in the intersection of the 2 queries
well, some posts with syntax suggestions have been given.
if you cannot give more details about the actual query, we cannot give more concrete query syntax ...

hope it helps nevertheless
ASKER CERTIFIED SOLUTION
Avatar of msk_apk
msk_apk
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of andieje

ASKER

thanks - the union idea seems to work