Selecting Distinct Entries From Table Based on Another

Let say I have to tables:

tbl_communities:
id
name

tbl_homes:
id
community
name

the integer in tbl_homes.community would correspond to tbl_communities.id.

So how would I draw a query that would pull a distinct list of names BUT ONLY if there is a record in the tbl_homes table.

I tried this:

SELECT DISTINCT name FROM tbl_communities

But that pulled names from the table even though there weren’t corresponding records in the homes table.

Can someone please help!!

SELECT name
  FROM tbl_communities
 WHERE EXISTS
       ( SELECT 1
           FROM tbl_homes
          WHERE community = tbl_communities.id )

:slight_smile:

Bless you!