Search code examples
sql-serverdatabaset-sqljoinsubquery

Rewrite a T-SQL query containing subqueries to using joins


I need to select a column from the Application table in the main outer query, which means I have to rewrite the following query to use joins rather than subqueries in order to move the Application table into the scope of the main outer query.

SELECT XMLValue
FROM DynamicField
WHERE ParentID IN (
   SELECT DynamicFieldID
   FROM DynamicField
   WHERE ParentID IN (
      SELECT DynamicFieldID
      FROM Application
      )
   )

Does anyone have a suggestion on how to solve this?


Solution

  • So something like this:

    SELECT s.*
    FROM DynamicField t
    INNER JOIN DynamicField t1 on(t.parentID = t1.DynamicFieldID)
    INNER JOIN Application s ON(t1.parentID = s.DynamicFieldID)
    

    This will select all from Application table.