title: Efficient way to check if row exists for multiple records in postgres
body: <p>I saw answers to a related <a href="https://stackoverflow.com/questions/7471625/fastest-check-if-row-exists-in-postgresql">question</a>, but couldn't really apply what they are doing to my specific case.</p>
  
  <p>I have a large table (300k rows) that I need to join with another even larger (1-2M rows) table efficiently. For my purposes, I only need to know whether a matching row exists in the second table. I came up with a nested query like so:</p>
  
  <pre><code>SELECT 
    id, 
    CASE cnt WHEN 0 then 'NO_MATCH' else 'YES_MATCH' end as match_exists
  FROM 
    (
     SELECT 
       A.id as id, count(*) as cnt
     FROM
       A, B
     WHERE 
       A.id = B.foreing_id
     GROUP BY A.id
    ) AS id_and_matches_count
  </code></pre>
  
  <p>Is there a better and/or more efficient way to do it?</p>
  
  <p>Thanks!</p>