title: SQL - left join generate duplicates
body: <p>I have code to select some applications but LEFT JOIN is creating duplicates.</p>
  
  <p>Question: How to get rid of duplicates generated by </p>
  
  <pre><code>LEFT JOIN
      attachments as att
  ON 
      (a.ssn = att.ssn AND att.doc_type = 'id_copy' AND att.source = 'cpt3')
  </code></pre>
  
  <p>My Work:</p>
  
  <p>I am considering <strong>SELECT DISTINCT</strong> or <strong>GROUP BY</strong> but, can't figure out where to put them.</p>
  
  <p>This is the query:</p>
  
  <pre><code>SELECT
          a.*, c.code, l.who, l.locked_time, att.id AS attnew, 
          ( SELECT 
              COUNT(*) 
            FROM 
              applications as a2 
            WHERE 
              a2.approved = 'Y' 
            AND 
              a2.paid = 'Y' 
            AND 
              a2.paid_back = 'Y' 
            AND 
              a2.ssn = a.ssn 
          ) AS previous_apps
      FROM 
          applications as a
      LEFT JOIN 
          locked_by as l 
      USING(id) 
      LEFT JOIN 
          campaign_codes as c 
      ON 
          c.id = a.campaign
      LEFT JOIN
          attachments as att
      ON 
          (a.ssn = att.ssn AND att.doc_type = 'id_copy' AND att.source = 'cpt3')
      WHERE 
          a.closed='N' 
      AND 
          a.paid = 'N' 
      ORDER BY 
          a.arrived_date 
      DESC
  </code></pre>