title: SQL Server Nested Queries on multiple tables
body: <p>I have a database with tables <strong>GridData</strong>, <strong>FormList</strong> and <strong>FormData</strong>.</p>
  
  <p>I need to pull a field called <strong>Value</strong> from the <strong>FormData</strong> table, but cannot figure out how to structure the query. I can run it one-by-one and it gives me the result, but need to include it together in a table output.</p>
  
  <p>The query, when I run it step-by-step is:</p>
  
  <p><em>Select * from <strong>GridData</strong> where GridName = 'uwg'</em> -- returns:</p>
  
  <pre><code>FormId      GridName        Example Field
  1244135     uwg             9
  1244135     uwg             10
  1244135     uwg             11
  1244135     uwg             7
  1244135     uwg             66
  </code></pre>
  
  <p>Using that <strong>FormID</strong> field, I lookup the <strong>FormList</strong> table</p>
  
  <p><em>Select * from <strong>FormList</strong> where FormID = 1244135</em> -- returns:</p>
  
  <pre><code>JobId       FormName        FormId
  1241899     ExampleForm     1244135
  </code></pre>
  
  <p>Then querying the SAME <strong>FormList</strong> table where FormID = JobID from above..</p>
  
  <p><em>Select * from <strong>FormList</strong> where FormID = 1241899</em> -- returns:</p>
  
  <pre><code>JobId       FormName        FormId
  1241894     ExampleForm2    1241899
  </code></pre>
  
  <p><strong>AGAIN..</strong> querying the SAME <strong>FormList</strong> table where FormID = JobID from above..</p>
  
  <p><em>Select * from <strong>FormList</strong> where FormID = 1241894</em> -- returns:</p>
  
  <pre><code>JobId       FormName        FormId
  1241893     ExampleForm3    1241894
  </code></pre>
  
  <p>The JobID from this result is the ID I need to reference in the <strong>FormData</strong> table to return the value I need...</p>
  
  <p><em>Select <strong>Value</strong> from <strong>FormData</strong> where FormID = 1241893 and Name = 'ProductName'</em> -- returns:</p>
  
  <pre><code>Value
  12345
  </code></pre>
  
  <p>My goal here is to be able to return all the values from the <strong>GridData</strong> table, with the corresponding entry in the <strong>FormData</strong> "Value" field.</p>
  
  <p>I'm really struggling with how to write this query! </p>
  
  <p>I've tried the following, but it returns multiple duplicate entries. The <strong>Value</strong> field contains multiple data types, and I'm only interested on the value from this table where <strong>FormData.Name = 'ProductName'</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>SELECT Value,
          RecordId,
   GridData.FormId,
   GridName,
   ExampleField
        
    FROM GridData
  
    JOIN FormData
  
    ON GridData.FormId = FormData.FormID
     
    WHERE GridData.FormId IN
  
    (SELECT FormList.FormId FROM FormList WHERE FormList.FormId IN 
    (SELECT FormList.FormId FROM FormList WHERE FormList.FormId IN 
    (SELECT FormList.FormId FROM FormList WHERE FormList.FormId IN 
    (SELECT FormData.FormID FROM FormData))))
  
    AND FormData.DataItemName = 'JobProductName'</code></pre>
  </div>
  </div>
  </p>