 <p><code>Method 1</code> </p> <pre><code>SELECT comp_small_name FROM table_company WHERE companyid IN ( SELECT agentid FROM ( SELECT * FROM table_comp_price WHERE companyid = #compid ORDER BY companyid DESC WHERE &lt;![CDATA[ ROWNUM &lt;= 2 ]]&gt; ) ) </code></pre> <p>or you can also use as below <code>Method 2</code>:</p> <pre><code> SELECT comp_small_name FROM table_company WHERE companyid IN ( SELECT agentid FROM ( SELECT * FROM table_comp_price WHERE companyid = #compid ORDER BY companyid DESC WHERE ROWNUM &amp;lt;= 2 ) ) </code></pre>