02JUN2020 Subquery
02JUN2020 Subquery
1)Inline view: Inline view to used to make alias work in where clause
2)analytical functions
Flow of sequence:
1)From
2)WHere
3)group by
4)having
5)select
6)order by
select ename,sal,sal*12 annsal from emp where annsal>40000;--->This will not work
*
ERROR at line 1:
ORA-00904: "ANNSAL": invalid identifier
SQL> select * from (select ename,sal,sal*12 annsal from emp) where annsal>30000;
ROWNUM: It is pseudo column also called as a magic column.It is hidden and belongs
to all the tables in a database.These columns get created whenever
installing oracle server
no rows selected
no rows selected
select....from...where rownum>number
1>number
rownum=1
1=1-->True
rownum<=4
1<=4---->True
rownnum=2
1=2--->False
rownum>4
1>4---->False
select * from (select * from emp order by sal desc ) where rownum<=5;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ----------
----------
7839 KING PRESIDENT 17-NOV-81 5000
10
7788 SCOTT ANALYST 7566 19-APR-87 3000
20
7902 FORD ANALYST 7566 03-DEC-81 3000
20
7566 JONES MANAGER 7839 02-APR-81 2975
20
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
analytical functions:
These functions are used to find the rank(nth highest/lowest)
1)row_number()
2)rank()
3)dense_rank()
Nth Highest
select * from (select col1,col2,col3,...,dense_rank() over (order by rank_col desc)
as rank from table_name) where rank=n;
Nth Lowest
select * from (select col1,col2,col3,...,dense_rank() over (order by rank_col asc)
as rank from table_name) where rank=n;
===================================================================================
=======
Correleted subquery
---------------------
Parent query passes data to child query
operator
1)exists
2)not exists
select * from dept where not exists(select deptno from emp where
dept.deptno=emp.deptno);
DEPTNO DNAME LOC
---------- -------------- -------------
40 OPERATIONS BOSTON
dept emp
10 10
20 20
30 30
40