********************************
Order By : Used to display result in particular order (Ascending or Descending)
. By default Desc.
********************************************************************************
********************************
Group By : Go for a group by only when we need a group function along with singl
e column in select statement.
Does not Sort Data.In order to sort data the Order By Clause is used.
We can have nested group function along with single column in sql que
ry.
e.g:
select roll_no,sum(marks) from student group by roll_no having sum(mar
ks)=(select max(sum(marks)) from
student group by roll_no.
********************************************************************************
*********************************
Sub Query: A subquery is basically a select clause which is used instead of anot
her statement.
e.g:
select * from sal where basic < (select avg(basic) from sal);
we can use maximum 16 subquery.
Nested query executed first and returns single column.
result only contains column which referenced in outermost query.
********************************************************************************
*********************************
Multiple Row SubQuery : In this case query returns multiple values in single col
umn. To compare values returned by
multiple row subquery we can use multiple row operator l
ike IN , ANY , ALL
********************************************************************************
*********************************
Multiple Column Subquery : In this case subquery returns more than one column.We
can use only one operator i.e IN
in case of multiple column subquery.
e.g: select * from emp where(deptno , sal) in (select deptno,max(sal) from emp
group by deptno);
********************************************************************************
********************************
Co Related SubQuery : Both Outer and Inner Query are dependent on each other.
For every execution of outer query the inner query is goin
g to be executed once.
Each and every record of outer query is known as candidate
record which result is going to
be decided by o/p of inner query.
You have to give alias name to outer query by means of whi
itive.
3rd,4th option is optional , by default oracle will assume 1,1.
select instr('NEW DELHI','E',3,1) from dual; o/p: 6
********************************************************************************
********************************
NVL : substitutes the NULL Value by means of any given value
The Datatype of both argument has to be same.
e.g: select sal,comm,sal+nvl(comm,0) from emp;
********************************************************************************
********************************
Decode : works as if..else condition
select decode(mod('&num',2),0,'EVEN','ODD') from dual;
********************************************************************************
********************************
Index is used for Fast Data Retrival and oracle by default uses B* Tr
Concepts : Indexing creates a pointer for a column for which index is created an
d the pointer stores the memory location of the value of that particular column
of that row and
it also maps to the row id of the corresponding values. So that during retrival
oracle has to go to memory location where value is stored with the help of point
er which makes
the retrival faster.
Simple Index: It is created on only one column of the table.
SYNTAX:
Bitmap Index: This is created on those column having less different values i.e
low cardinality.
In bitmap index oracle assign a value 0,1,2.... to the unique val
ue of the column ..so that during the processing of the query oracle does not go
for the searching the values like M/F , it goes for 0,1.
Create bitmap index I_BITMAP on emp(deptno);
Function Based Index: This index is created on those situatiuon where you need
to access a value of any column on the
basis of any function like substr , instr...etc.
create index I_substr on emp(substr(ename,1,3)) ;
Cluster Index: Cluster is used to store the common data of multiple tables in a
common location.
It is Database Object which acts as a storage object to store th
e data physically.
This table can be linked to the cluster in a logical way so that
the value for the common
column will be logically stored in table and physically in clust
er.
1:- create cluster <Name Of Cluster>(<col name> <data type> , <col nam
e> <data type>)
create cluster c_roll (roll_no Number);
2:- Create table student_c(RNO number primary key,name varchar2(20) ) c
luster C_ROLL(ROLLNO);
Create table batch_c(bcode char(4) , batch_rno Number refrences st
udent_c) cluster c_roll(batch_rno);
3:-
********************************************************************************
*********************************************
VIEW : Virtual table , Used to selecting data from table or more than one table
without applying join condition,selecting the aggregate data from a table withou
t writing the
group function every time.
Simple View: This view is based on only one table.
e.g: create or replace view v_emp as select empno,empname,deptno,job fr
om emp;
1:- You can do all the DML Operation on the table through simple view. B
ut make sure the view contains all the
not null column of the table if you want to insert records into the
table through the view.
e.g: update v_emp set sal=sal+1000 where deptno=10;
2:- View is virtual DB Objects means it does not occupy any space in the
memory in the since it does not have any
Create or replace view v_read as select * from wmp with Read Onl
y;
Check View: This View allows DML Operations only on those records which satisf
y the where condition of the check condition.
e.g: create or replace view v_check as select * from emp where deptno=
30 with check option constraint c_check;
Force View: This View is based on DB Object which does not exist. Force view b
ecomes useful once the DB Object is created.
e.g:
Case : Decode performs eqality check while case can work with all logical compar
isions.
case
when sal < 1000
Decode is shorter than case.
CASE expects datatype consistency, DECODE does not.
SQL> select decode(2,1,1,
2
'2','2',
3
'3') t
4 from dual;
T
---------2
SQL> select case
2
3
4
end
5 from dual;
when
ERROR at line 2:
ORA-00932: inconsistent datatypes: expected NUMBER got CHAR
DESCRIPTION
SUM(SALARY)
--------------- ----------Programmer
1234.56
1234.56
Tester
4322.78
Manager
7897.78
12220.56
Tester
16774.12
Manager
2344.78
19118.9
32574.02
use:
COUNT (1)
emp
salary < 1000;
salary
1 ELSE
salary
1 ELSE
< 1000
null END) count_1,
BETWEEN 1001 AND 2000
null END) count_2
---------------------------------------------------------------------------------------------------------------------------------------------------