title: SQL query returning cartesian product
body: <p>I have some tables:</p>
  
  <pre>
  Employee: id, name, id_suc, id dep, id_sec
  Suc     : id_suc, name
  Dep     : id_dep, id_suc, name
  Sec     : id_sec, id_dep, id_suc, name
  </pre>
  
  <p>Don't blame on me, this is an existing application, I didn't create the Database and can't touch the structure since there is too much data inside and reports depending on it. I'm just trying to modify a report as asked. </p>
  
  <p>I do a query:</p>
  
  <pre><code>SELECT DISTINCT 
    s.name as sucurs, 
    d.name as depart, 
    c.name as section, 
    e.name AS emp 
  FROM 
    employee e 
    join suc s on (e.id_suc = s.id_suc) 
    join dep d on (e.id_dep = d.id_dep) 
    join sec c on (e.id_sec = c.id_sec) 
  ORDER BY 
    sucurs, depart, section, emp
  </code></pre>
  
  <p>and brings me a cartesian product. I want:</p>
  
  <pre><code>sucurs1, depart1, section1, emp1
  sucurs1, depart1, section1, emp2
  .....
  </code></pre>
  
  <p>(then on the report I group by suc, then dep, then sec)</p>
  
  <p>instead, I got: </p>
  
  <pre><code>sucurs1, depart1, section1, emp1
  sucurs2, depart1, section1, emp1
  </code></pre>
  
  <p>and so on. It brings ALL <code>sucurs</code>, ALL <code>depart</code>, ALL <code>section</code> and sometimes duplicated <code>emp</code>.</p>
  
  <p>I'm missing something, but don't know what. Any clues?</p>