SUB QUERYS
===============
Query within another query is called as Sub query or Nested query.
Sub querys are used to retrieve data from single or multiple tables by using
more than one step process.
All relational databases having two types of sub querys.
These are
1. Non- Correlated sub querys
2. Correlated sub querys
In Non-correlated sub querys child query is executed first then
only parent query is executed, whereas in correlated sub querys
parent query is executed first then only child query is executed.
1. Non-Correlated sub querys:-
==============================
Non Corelated sub querys having two parts.
1)Child query
2)Parent query
1. Child Query:- A query which provides values to the another query
============== is called Child Query or Inner Query.
2. Parent Query:- A query which receiving values from another query
================= is called Parent Query or Outer Query.
.In non-correlated sub query maximum limit is up to 255 querys.
NON-CORRELATED SUB QUERY:-
=========================
|----------->1)single row sub querys
|----------->2)multiple row sub querys
|----------->3)multiple column sub querys
|----------->4)inline views (or)subquery are used in from
clause.
1)single row sub querys
=======================
This is a single row sub query because here child query
returns single value. In single row sub query's we are
using =,<,>,<=,>=, between operators.
RULES:-
=======
1)90% --------->same column
2)5% --------->group function
3)3% --------->expressions
4)2% --------->different column
select * from emp where condition=(select ............)
EG:-
===
SQL> select max(sal) from emp where sal<(select max(sal) from emp);
Display mechanism in Task in right hand
left hand side side
--------------------------------------------------------------------------------------------------------------------------------------
--------------
1.Write a query to display senior most employee details
from emp table?
SELECT * FROM EMP WHERE HIREDATE=(SELECT MIN(HIREDATE) FROM EMP);
2.Write a query to display the employees who are working in
SMITH department number from emp table?
3.Write a query to display the employees who are getting
more sal than the highest paid employee in 20th
department from emp table?
4.Write a query to display the employees who are getting
more sal than the lowest paid employee in 10th
department from emp table?
5.Write a query to display second maximum salary or second
highest salary from emp table?
6.Write a query to display second highest sal employee
details from emp table?
7.Write a query to display the employees who are getting
more salary than the avg(sal)from emp?
SQL> SELECT ENAME FROM EMP WHERE SAL >(SELECT AVG(SAL) FROM EMP);
8.Write a query to display the employee records who are
earning more salary than the BLAKE salary?
SQL> SELECT * FROM EMP WHERE SAL>(SELECT SAL FROM EMP WHERE ENAME='BLAKE');
9.Write a query to display the employee records whose job
title is same as ALLEN?
SQL> SELECT * FROM EMP WHERE JOB=(SELECT JOB FROM EMP WHERE ENAME='ALLEN');
10.Display the employee records who are senior to blake?
SQL> SELECT * FROM EMP WHERE HIREDATE<(SELECT HIREDATE FROM EMP WHERE ENAME
='BLAKE');
11.Write a query to display the employees who are getting more
salary than the Allen salary and job same as Jones job?
SQL> SELECT *
2 FROM emp
3 WHERE sal > (SELECT sal FROM emp WHERE ENAME = 'ALLEN')
4 AND job = (SELECT job FROM emp WHERE ENAME = 'JONES');
12.Write a query to display the employees who are working in sales
department from emp,dept tables.
SQL> SELECT* FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE DNAME='SAL
ES');
13.Write a query to display the employees who are working
under Blake as a Manager from emp table by using
empno,mgr columns?
SQL> SELECT * FROM EMP WHERE MGR=(SELECT EMPNO FROM EMP WHERE ENAME='BLAKE')
14.Write a query to display longest ename from emp table?
SQL> SELECT ENAME FROM EMP WHERE LENGTH(ENAME)=(SELECT MAX(LENGTH(ENAME)) FR
OM EMP);