Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
10 Sub Queries

10 Sub Queries

Ratings: (0)|Views: 167|Likes:
Published by api-3827483

More info:

Published by: api-3827483 on Oct 18, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as RTF, PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

sub-queries
*one of the advanced query techniques in sql
*you can use the results of one query as part of another
query
*subqueries let you break a query down into pieces and then
put the pieces together
*subqueries are used in the where or having clause
*example :
1. list the employees belonging to the department of
\u2018miller\u2019.
2. list all the employees who have the same job as `scott\u2019.

*guidelines for using subqueries :
*the subquery must be enclosed in the parenthesis
*you can reference columns from the outer query in the

inner query
*columns common to both queries are to be prefixed with
their table names

*oracle generates error if
*the subquery returns no value
*the subquery returns more than one values

*special operators can be used in these cases
*these operators are used in queries where the inner query
returns more than one value

*operators are
*in or not in
*exists
*any
*all

in
*in or not in operator :
*example :
*list the details of employees in department 10 who have
the same job as in department 30.
exists
*exists operator :
*existence of rows in the inner query may be used to
qualify rows of an outer query
*this operator produces a boolean result
*it takes a subquery as an argument and evaluates it to
true if it produces any output or false, if it does not
*examples :
i.list the employee details if and only if
more than 2 employees
are present in department 20.
ii.find the employees who have at least
one person reporting to them.
sol.select empno,job,deptno
from emp e1

where exists (select \u2018x\u2019
from emp
where mgr = e1.empno)

iii.find all the departments who don\u2019t have
any employees
sol.select deptno, dname
from dept d1
where not exists (select \u2018x\u2019
from emp
where deptno = d1.deptno)
)
any, all

*any, all operator :
*used alongwith relational operators in subqueries
*if a subquery returns more than one row and the comparison other

than equality is required
*examples :

1. list the employee names whose salary is equal to the
salary of an employee belonging to department 30.
2. list the employee names whose salary is greater than

the salary of all employees belonging to department 30.
sub-queries

*objectives :
*explain & demonstrate nested sub-queries
*explain & demonstrate co-related sub-queries

nested queries
*the result of one query is dynamically substituted in the
condition of another
*sql first evaluates the inner query within the where
clause
*the nested select clause in the inner query can in turn
have another nested select clause
co-related sub-queries
*co-related sub-queries :
*a co-related sub-query refers to a column selected by the
main query
*sometimes table used in inner query refers to the same
table used in outer query. table aliases are used in such
cases
*having clause is also used in some cases

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->