Sqlnotes
Sqlnotes
i integration
g grid
c cloud
java frontend
other db techs
sql server
sybase
mongo db
db2 .. etc
Relational Operators
******************
= <> !=
<
>
<=
>=
in not in
like not like
between not between
all
any
is null is not null
select
column(s)
from
table name
where
condition
order by asc/desc -- last
case manipulation
upper
lower
initcap
character manipulation
********************
instr
substr
length
replace
translate
reverse
trim
ltrim
rtrim
lpad
rpad
length 1 - 1 - input
purpose : to count the no of letters
eg : select first_name , length(first_name) from employees;
select length('Hermann') from dual;
reverse 1 - 1 - input
purpose : to get the mirror of the given string
eg : select first_name , reverse(first_name) from employees;
Number functions
**************
Round
Trunc
Mod
Ceil
Floor
Abs
Sign
round - 1 checks the decimal whether 5 or > 5 => will rounding off
eg : select round(42.56) , round(85.86) , round(78.49) from dual;
trunc - 1 removes the decimals - 2 nd argument decides hw mny digits to display
eg : select trunc(42.56) , trunc(85.86) , trunc(78.4452589,2) from dual;
as - column alias
date functions
************
add_months
months_between
next_day
last_day
general functions
***************
distinct / unique
greates
least
concat ||
case
decode
Null functions => null is not equal to anything. you can not compare
anything with null. null is null
Conversion Functions
********************
=================================================================
create
alter
truncate
rename
drop
datatypes
number
char 2000
varchar / varchar2 4000
date
timestamp
long
clob char large object
blob binary large object
bfile path - outside
char(3) varchar(3)
sai sai
a hi
6 operations
1. adding a column
alter table wwe add bb date;
1.adding a column
alter table wwe add cc varchar(10);
2.droping a column
alter table wwe drop column bb;
3.adding a constraint
alter table wwe add constraint ww1 primary key(aa);
4.droping a constraint
alter table wwe drop constraint ww1;
5.renaming a column
alter table wwe rename column cc to bb;
6.modifying a datatype
alter table wwe modify bb date;
insert
update
delete
merge
merge
*******
omr
id name fee
101 sai 5000
102 ram 5000
tam
101 sai 4500 u
i
grant
revoke
=================================================================
Virtual column
=================================================================
Set Operators
************
union => removes duplicates , asc
union all => it displays result as it is from the table
intersect => common values
minus => value in first query not in second query
Rules
1.no of columns should be matched
2.data type should be matched
3.execution order can be changed by using ( and )
union
*****
select a from t1
union
select b from t2;
union all
********
select b from t2
union all
select a from t1;
intersect
********
select a from t1
intersect
select b from t2;
minus
******
select a from t1
minus
select b from t2;
select b from t2
minus
select a from t1;
task
****
select b from t2
minus
select a from t1
union all
select b from t2
union
select a from t1;
select b from t2
minus
(select a from t1
union all
select b from t2)
union
select a from t1;
=================================================================
=================================================================
Analytical Functions
******************
Rank()
Dense_rank()
row_number()
lead
lag
listagg -- 11g
select * from(
select first_name , salary ,department_id,dense_rank() over(order by salary desc) as
rnk from employees)
where rnk = 3;
with abcde as
(select first_name , salary ,department_id,dense_rank() over(order by salary desc)
as rnk from employees)
select * from abcde where rnk=3;
with e as(
select first_name , salary , dense_rank() over(order by salary desc) as rnk from
employees)
select * from e where rnk = 1;
with s as(
select first_name , department_id , salary ,
dense_rank() over(partition by department_id order by salary desc) as rnk from
employees)
select * from s where rnk=1;
=================================================================
Pseudo columns
*************
sysdate / current_date
systimestamp
user
uid
rownum
rowid
level
nextval
currval
Dual : Oracle predefined dummy table (Or) One row one column table
Sequence : it generates unique numbers automatically. mainly used for primary key
column
Sequence attributes
nextval
currval
===================================================================================
=====
Group functions => This funcion will group the rows and fetch one row for
each group.
max
min
sum
avg
count
select
column name
from
table name
where
conditions
group by
add columns + grp fun
having
grp fun with condition
order by
asc/desc
=================================================================
sql plus commands
===============
clear screen/clear scr/cl scr
set pagesize => how many records to be displayed in each page set linesize
=> how many char to be printed in each line @ =>
@desktop\testomr.txt
set heading on => columns to be displyed ?
set heading off => columns not to be displayed ?
set feedback on => whether acknowledgement required ?
set feedback off => or not ?
set timing on => time taken to execute the query
set timing off => to disable
set verify on => to display old and new
set verify off => to remove old and new
define => to change value of constant variable
undefine => to reset the value of constant variable
ed => edit the query .. save .. close then give /
ttitle ' hi '
btitle ' bye '
& => substitution variable
&& => constant
/ => last executed statement spool => spool
desktop\test.txt ....... spool off
show user => to display the current user
exit/quit => to quit the session
***********************************************************************************
***********
sql loader => bulk loader utility
open a notepad
cid,cname,fees
10,sql,4500
20,plsql,3500
30,java,5000
save as (select all files) => 1.csv (comma separated value file)
open a notpad
**********************************************************************
views => virtual table / only query will be stored / does not occupy memory in db
types of view
retriction methods
****************
with read only
with check option
Complex view
************
functions
expression
group function
analytical function
dictionary table
sub query .... etc
*********************************************************************
synonym - alternative name for an object
we can create multiple synonym for an object
we can create multiple synonym for a synonym
if table is dropped then what abt synonym ?
invaild - synonym translation is no longer valid
is it possible to perform dml on synonym ? yes
is it possible to create synonym with the same name of an object name ? yes
**********************************************************************
joins -- to retrieve data from more than one table
types of joins
************
inner join => matching records
left outer join => matching of both , unmatching of left table
right outer join=> matching of both , unmatching of right table
full outer join => matching and unmatching of both tables
self join => joining table within itself
cross join => cartesian poduct , no of rows in first table *
no of rows in second table
drop table students purge;
drop table course purge;
create table students(sid number, sname varchar(50), cid number) ;
create table course(cid number, cname varchar(50)) ;
sname cname
neena oracle
alex java
rupa c++
kiran unix
(or)
(or)
sname cname
neena oracle
alex java
rupa c++
kiran unix
raju
select s.sname , c.cname
from students s, course c
where s.cid = c.cid(+);
(or)
(or)
(or)
(or)
sname cname
neena oracle
alex java
rupa c++
kiran unix
linux
rpa
python
self join
**********************************************************************
refresh methods
**************
complete
fast
force
***********************************************************************************
***********
External Tables => data import and export process
***********************************************************************************
***********
Index
why ? to retrieve the data faster but not all the times
decision maker - optimizer
when ? whenever a column frequently called in where clause
**********************************************************************
course
sql 10
plsql 10
java 10
unix 10
course
sql 100000 p1
plsql 100000 p2
java 100000 p3
unix 100000 p4
list range
hash
List Partitioning
**************
create table course
(
cid number,
cname varchar(15)
)
partition by list(cname)
(
partition a1 values('sql'),
partition a2 values('plsql'),
partition a3 values('java'),
partition a4 values('unix'),
partition a5 values(default) -- optional
);
Range Partitioning
****************
create table emp
(
eid number,
ename varchar(10),
phno number,
salary number
)
partition by range(salary)
interval(5000) --11g
(
partition b1 values less than (5000),
partition b2 values less than (10000),
partition b3 values less than (15000),
partition b4 values less than (20000)
);
=================================================================
single row sub query: if sub query returns only one row
******************
select first_name from employees where salary = (select min(salary) from employees);
select * from
(select first_name , salary , dense_rank() over(order by salary desc) as rnk from
employees)
where rnk = 2;
scalar sub query: if your query is in select clause
**************
select first_name ,
(select department_name from departments where departments.department_id =
employees.department_id) as depname ,
salary from employees;
nested sub query: sub query that embeded within another sub query
***************
*****************************************************
1.syntactic check
if any mistake there in clauses
2.symantic check
if any mistake there in column name or table name
3.shared pool check ( library cache check )
for every select statement oracle will generate unique sqlid
it will check the same sqlid is there in the library cache
if found then it will go for softparsing else optimizer will generate new
explainplan( hard parsing)
Trainer
Karthick Ravichandran
9444999041