title: Using DISTINCT inner join in SQL
body: <p>I have three tables, A, B, C, where A is many to one B, and B is many to one C. I'd like a list of all C's in A. </p>
  
  <p>My tables are something like this: A[id, valueA, lookupB], B[id, valueB, lookupC], C[id, valueC]. I've written a query with two nested SELECTs, but I'm wondering if it's possible to do INNER JOIN with DISTINCT somehow.</p>
  
  <pre><code>SELECT valueC
  FROM C
  INNER JOIN
  (
      SELECT DISTINCT lookupC
      FROM B INNER JOIN
      (
          SELECT DISTINCT lookupB
          FROM A
      ) 
      A2 ON B.id = A2.lookupB
  ) 
  B2 ON C.id = B2.lookupC
  </code></pre>
  
  <p>EDIT:
  The tables are fairly large, A is 500k rows, B is 10k rows and C is 100 rows, so there are a lot of uneccesary info if I do a basic inner join and use DISTINCT in the end, like this:</p>
  
  <pre><code>SELECT DISTINCT valueC
  FROM 
  C INNER JOIN B on C.id = B.lookupB
  INNER JOIN A on B.id = A.lookupB
  </code></pre>
  
  <p>This is very, very slow (magnitudes times slower than the nested SELECT I do above.</p>