Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Intersection of many SELECT DISTINCT queries

I have a table recruiter with columns company and location. I want to find all the distinct locations where there is at least one recruiter from each company in a list.

I can find all the locations where a recruiter from a given company works

SELECT DISTINCT location 
    FROM recruiter 
        WHERE company='Google'

but I want to do this for a bunch of different companies and get the intersection of them.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I found a previous question which seemed to ask something similar: Intersection of two select.
However, the question asks specifically about the intersection of the results of two SELECT queries, and the answers don’t seem to generalize to an arbitrary number.

>Solution :

A reproducible example with sample data and expected results would be quite helpful here. My guess is that you want something like

select location, count(distinct company)
  from recruiter
 where company in ('Google', 'Microsoft', 'Amazon')
 group by location
having count(distinct company) = 3;

which would return one row for every location where there is at least one row for each of the three companies. Of course, if you add additional companies to the list, you’d need to adjust the literal in the having clause.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading