SubQuery in Qracle SQL
SubQuery in Qracle SQL
--===================
TABLE STRUCTURE =======================
XXSD_EMP_TL
EMPNO
|
ENAME
|
JOB
|
MGR
|
SAL
|
COMM
|
DEPTNO
|
7369
|
SMITH
|
CLERK
|
7902
|
800
|
80
|
20
|
7499
|
ALLEN
|
SALESMAN
|
7698
|
1600
|
300
|
30
|
7521
|
WARD
|
SALESMAN
|
7698
|
1250
|
500
|
30
|
7566
|
JONES
|
MANAGER
|
7839
|
2975
|
1200
|
20
|
7654
|
MARTIN
|
SALESMAN
|
7698
|
1250
|
1400
|
30
|
7698
|
BLAKE
|
MANAGER
|
7839
|
2850
|
1100
|
30
|
7782
|
CLARK
|
MANAGER
|
7839
|
2450
|
900
|
10
|
7788
|
SCOTT
|
ANALYST
|
7566
|
3000
|
750
|
20
|
7839
|
KING
|
PRESIDENT
|
|
5000
|
2000
|
10
|
7844
|
TURNER
|
SALESMAN
|
7698
|
1500
|
300
|
30
|
7876
|
ADAMS
|
CLERK
|
7788
|
1100
|
220
|
20
|
7900
|
JAMES
|
CLERK
|
7698
|
950
|
110
|
30
|
7902
|
FORD
|
ANALYST
|
7566
|
3000
|
700
|
20
|
7934
|
MILLER
|
CLERK
|
7782
|
1300
|
720
|
10
|
XXSD_DEPT_TL
DEPTNO
|
DNAME
|
LOC
|
DEPT_YEAR
|
10
|
ACCOUNTING
|
NEW YORK
|
2020
|
20
|
RESEARCH
|
DALLAS
|
2019
|
30
|
SALES
|
CHICAGO
|
2018
|
40
|
OPERATIONS
|
BOSTON
|
2017
|
XXSD_SAL_INFO_TL
YEAR_NAME
|
SAL_GRADE
|
2015
|
1000
|
2016
|
1500
|
2017
|
2000
|
2018
|
2200
|
2019
|
2500
|
2020
|
3000
|
--====================================================
--Single Row Sub
Query use with "="
Operator
--====================================================
--WHERE--
select
DEPTNO from XXSD_DEPT_TL WHERE DNAME in ( 'RESEARCH')
select * from XXSD_EMP_TL
WHERE
DEPTNO = ( )
select * from XXSD_EMP_TL
where
DEPTNO = ( select
DEPTNO from XXSD_DEPT_TL WHERE DNAME in ( 'RESEARCH') )
--====================================================
--Multi Row Sub Query use with "IN"
Operator
--====================================================
select
DEPTNO,
DNAME, LOC from XXSD_DEPT_TL A
where
DEPTNO IN ( select
DEPTNO from XXSD_EMP_TL where JOB = 'SALESMAN')
select DEPTNO from XXSD_EMP_TL where JOB = 'SALESMAN'
--====================================================
--Order of
Execution of Sub Query (Last Inner to
First Outer Query)
--====================================================
select *
from XXSD_EMP_TL
WHERE
DEPTNO = (
select
DEPTNO
from XXSD_DEPT_TL
where
DEPT_YEAR = (
SELECT
YEAR_NAME
FROM XXSD_SAL_INFO_TL
WHERE
SAL_GRADE = 3000
)
)
EMPNO
|
ENAME
|
JOB
|
MGR
|
HIREDATE
|
SAL
|
COMM
|
DEPTNO
|
7782
|
CLARK
|
MANAGER
|
7839
|
09-Jun-81
|
2450
|
900
|
10
|
7839
|
KING
|
PRESIDENT
|
|
17-Nov-81
|
5000
|
2000
|
10
|
7934
|
MILLER
|
CLERK
|
7782
|
23-Jan-82
|
1300
|
720
|
10
|
Comments
Post a Comment