<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Oracle Database Tips blog &#187; oracle subquery</title>
	<atom:link href="http://oracle-database-tips.com/wp/tag/oracle-subquery/feed" rel="self" type="application/rss+xml" />
	<link>http://oracle-database-tips.com/wp</link>
	<description>News for Oracle professionals</description>
	<lastBuildDate>Fri, 03 Feb 2012 10:37:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Oracle Subquery</title>
		<link>http://oracle-database-tips.com/wp/sql/oracle-subquery</link>
		<comments>http://oracle-database-tips.com/wp/sql/oracle-subquery#comments</comments>
		<pubDate>Fri, 23 Oct 2009 11:07:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[accounting]]></category>
		<category><![CDATA[better solution]]></category>
		<category><![CDATA[boston]]></category>
		<category><![CDATA[emp]]></category>
		<category><![CDATA[ford]]></category>
		<category><![CDATA[illustration]]></category>
		<category><![CDATA[job]]></category>
		<category><![CDATA[oracle subquery]]></category>
		<category><![CDATA[subquery sql]]></category>
		<category><![CDATA[subquery syntax]]></category>

		<guid isPermaLink="false">http://oracle-database-tips.com/wp/?p=74</guid>
		<description><![CDATA[Oracle Subquery syntax and examples]]></description>
			<content:encoded><![CDATA[<p><strong>Contents</strong></p>
<ul>
<li><a href="#0">Definition</a></li>
<li><a href="#1">Simple subquery</a></li>
<li><a href="#correlated">Correlated subquery</a></li>
<li><a href="#update">Update with correlated subquery</a></li>
<li><a href="#delete">Delete with correlated subquery</a></li>
</ul>
<p><a id="0" name="0"></a><br />
<strong>Definition</strong></p>
<p>A subquery is basically a select clause which is used instead of another statement.</p>
<p>For example : Have a look at the following 2 tables : DEPT and EMP</p>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; select * from dept order by deptno;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

SQL&gt; select empno,ename,job,sal,deptno from emp;

     EMPNO ENAME      JOB              SAL     DEPTNO
---------- ---------- --------- ---------- ----------
      7369 SMITH      CLERK            800         20
      7499 ALLEN      SALESMAN        1600         30
      7521 WARD       SALESMAN        1250         30
      7566 JONES      MANAGER         2975         20
      7654 MARTIN     SALESMAN        1250         30
      7698 BLAKE      MANAGER         2850         30
      7782 CLARK      MANAGER         2450         10
      7788 SCOTT      ANALYST         3000         20
      7839 KING       PRESIDENT       5000         10
      7844 TURNER     SALESMAN        1500         30
      7876 ADAMS      CLERK           1100         20
      7900 JAMES      CLERK            950         30
      7902 FORD       ANALYST         3000         20
      7934 MILLER     CLERK           1300         10

14 rows selected.</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --><a id="1" name="1"></a><br />
<strong>Simple Oracle subquery</strong></p>
<p>A simple subquery is evaluated once for each table.</p>
<p>You would like to select all employees whose department is located in Chicago.</p>
<p>A join would be a better solution for this select,<br />
but for the purposes of illustration we will use a subquery.<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">
SQL&gt; select empno,ename,job,sal,deptno from emp where
     deptno in (select deptno from dept
	 where loc = 'CHICAGO');

     EMPNO ENAME      JOB              SAL     DEPTNO
---------- ---------- --------- ---------- ----------
      7900 JAMES      CLERK            950         30
      7844 TURNER     SALESMAN        1500         30
      7698 BLAKE      MANAGER         2850         30
      7654 MARTIN     SALESMAN        1250         30
      7521 WARD       SALESMAN        1250         30
      7499 ALLEN      SALESMAN        1600         30

6 rows selected.</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --><br />
You can use a subquery in the place of a table name in the &#8216;from&#8217; clause.</p>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; Select ename, deptno from
     (select ename, deptno from emp
	 where job = 'CLERK' )
     where
     deptno &gt; 20;

ENAME          DEPTNO
---------- ----------
JAMES              30</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --><br />
You can even replace a column name with a subquery:<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; select ename,
            (select max(sal) from emp) "maxsal" ,
            sal,
            ((select max(sal) from emp ) - sal )
			"difference"  from emp;

ENAME          maxsal        SAL difference
---------- ---------- ---------- ----------
SMITH            5000        800       4200
ALLEN            5000       1600       3400
WARD             5000       1250       3750
JONES            5000       2975       2025
MARTIN           5000       1250       3750
BLAKE            5000       2850       2150
CLARK            5000       2450       2550
SCOTT            5000       3000       2000
KING             5000       5000          0
TURNER           5000       1500       3500
ADAMS            5000       1100       3900
JAMES            5000        950       4050
FORD             5000       3000       2000
MILLER           5000       1300       3700

14 rows selected.</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --><br />
<a id="correlated" name="correlated"></a><br />
<strong>Correlated Oracle subquery</strong></p>
<p>A correlated Oracle subquery is evaluated once FOR EACH ROW as opposed to a normal subquery which is evaluated only once for each table.</p>
<p>You can reference the outer query inside the correlated subquery using an alias which makes it so handy to use.</p>
<p>Let&#8217;s select all employees whose salary is less than the average of all the employees&#8217; salaries in the same department.<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; select ename ,sal ,deptno from emp a where
     a.sal &lt; (select avg(sal) from emp b
	           where a.deptno = b.deptno)
     order by deptno;

ENAME             SAL     DEPTNO
---------- ---------- ----------
CLARK            2450         10
MILLER           1300         10
SMITH             800         20
ADAMS            1100         20
WARD             1250         30
MARTIN           1250         30
TURNER           1500         30
JAMES             950         30

8 rows selected.</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --><br />
<a id="update" name="update"></a><br />
<strong>Using a correlated subquery in an update</strong></p>
<p>Let&#8217;s give these people (whose salary is less than their department&#8217;s average)  a raise.<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">
SQL&gt; select ename, sal, deptno  from emp
     order by deptno, ename;

ENAME             SAL     DEPTNO
---------- ---------- ----------
CLARK            2450         10
KING             5000         10
MILLER           1300         10
ADAMS            1100         20
FORD             3000         20
JONES            2975         20
SCOTT            3000         20
SMITH             800         20
ALLEN            1600         30
BLAKE            2850         30
JAMES             950         30
MARTIN           1250         30
TURNER           1500         30
WARD             1250         30

14 rows selected.

SQL&gt;  UPDATE emp a
      set sal = (select avg(sal)
                from emp b
	        where
	        a.deptno = b.deptno)
      where sal &lt;
	   (select avg(sal) from emp c
	   where a.deptno = c.deptno);

8 rows updated.

SQL&gt; select ename, sal, deptno  from emp
     order by deptno, ename;

ENAME             SAL     DEPTNO
---------- ---------- ----------
CLARK         2916.67         10
KING             5000         10
MILLER        2916.67         10
ADAMS            2175         20
FORD             3000         20
JONES            2975         20
SCOTT            3000         20
SMITH            2175         20
ALLEN            1600         30
BLAKE            2850         30
JAMES         1566.67         30
MARTIN        1566.67         30
TURNER        1566.67         30
WARD          1566.67         30

14 rows selected.

SQL&gt; commit;

Commit complete.</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --><a id="delete" name="delete"></a><br />
<strong>Using a correlated subquery in a delete</strong></p>
<p>Let&#8217;s delete the highest earning employees in each department.<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">
SQL&gt;  delete from emp a where
      a.sal = (select max(sal) from emp b
	  where a.deptno = b.deptno);

4 rows deleted.

SQL&gt; select ename, sal, deptno  from emp
     order by deptno, ename;

ENAME             SAL     DEPTNO
---------- ---------- ----------
CLARK         2916.67         10
MILLER        2916.67         10
ADAMS            2175         20
JONES            2975         20
SMITH            2175         20
ALLEN            1600         30
JAMES         1566.67         30
MARTIN        1566.67         30
TURNER        1566.67         30
WARD          1566.67         30

10 rows selected.

SQL&gt; commit;

Commit complete.</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --><br />
Lastly: do not forget to commit to make your changes permanent when using the oracle subquery statement.<br />
<!-- ============================================================================================================================================================ --></p>
<h3>Recommended reading:</h3>
<div id="content">
<li><a title="opens new window" onclick="window.open('http://www.oracle-database-tips.com/cgi-bin/counter.pl?url=http%3A%2F%2Fdownload.oracle.com%2Fdocs%2Fcd%2FB19306_01%2Fappdev.102%2Fb14261%2Fsqloperations.htm%23i3317&amp;referrer=http%3A%2F%2Fwww.oracle-database-tips.com%2Foracle_subquery.html'); return false;" href="http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/sqloperations.htm#i3317" target="_blank">Oracle Online Documentation: SQL Reference, Using Subqueries</a></li>
</div>
]]></content:encoded>
			<wfw:commentRss>http://oracle-database-tips.com/wp/sql/oracle-subquery/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

