title: how to maintain order while doing join in sql
body: <p>table 1 and table 2 have their primary key (key1,key2) to be the same. table 3 is a user defined table with one column field5 which is common to table2. I need to select the rows from table 1 and table 2 where field5 in table2 is in table3. I need to obtain the rows in the same order as table3.</p>
  
  <h2>table 1</h2>
  
  <p>key1 key2 field1 field2</p>
  
  <h2>table 2</h2>
  
  <p>key1 key2 field3 field4 field5</p>
  
  <h2>user defined table</h2>
  
  <p>field5</p>
  
  <p>the query i am thinking is</p>
  
  <pre><code>select a.key1, a.key2, a.field1, a.field2, b.field3, b.field4
  from table1 as a INNER JOIN table2 as b ON a.key1 = b.key1 AND a.key2 = b.key2
  where b.field5 in (select field5 from table3)
  </code></pre>
  
  <p>this will not give me the rows in the same order as in table3.</p>
  
  <p>I would need something like this to maintain the order? </p>
  
  <pre><code>select a.key1, a.key2, a.field1, a.field2, d.field3, d.field4 from
  table1 as a INNER JOIN
   (select b.key1, b.key2, b.field3, b.field4
    from table2 as b INNER JOIN table3 as c
    ON b.field5 = c.field5) as d
  WHERE a.key1 = d.key1 AND a.key2 = d.key2
  </code></pre>