Oracle SQL Note Book
Oracle SQL Note Book
1.case sensitive
DATA TYPES:
1.NUMERIC
2.CHAR
char
varchar()
varchar2()
3.DATE
date
=========================================================================
==
CREATING TABLE:
----->commit
----->rolback
----->savepoint
---->grant
----->revoke
=============================================================
insert:
note: while inserting data,if you know the column order then you dont
need to mention columns.
Result Set 1:
sql> commit:
commit complete.
ADDING COLUMN:
sql>
update customer
set country ='INDIA';
=========================================================================
==
drop column:
===========
=========================================================================
==
1.if you want to modify existing datatype ,first you should empty that
column.for that
you should take backup .
1.backup table
2.truncate base table
3.modify datatype
4.restore the data
5.drop bkp table
creating backup table:
create table customer_bkp as select * from customer;
sql>
truncate table customer:
truncate comepleted.
=========================================================================
=======
=========================================================================
========
rename column :
alter table customer
rename column dob to date_of_birth;
=========================================================================
==========
rename table:
=========================================================================
====
CONSTRAINTS ON ORACLE:
2.NOT NULL
3.unique
4. check
data validation.
check (age>18)
5 foreign key
i).relation ship between two tables.
ii)it will accept duplicates.
iii)it should be primary key another table.
iv).any no of foreign key in table.
=========================================================================
======
op; here wil getconstrain name(SYS_C09883) but we cant see which column
it is belong to, so
note: we dont need to check this manually so we are making join using two
tables.then we can easily know which key s violated.we can easil
identify.
select
a.owner,a.constraint_name,a.constraint_type,b.table_name,b.column_name
from all_constraints a,all_cons_columns b
where a.constraint_name=b.constraint_name and a.owner='HR' and
a.table_name='cust';
=========================================================================
=======
you can delete a record from child table.
CITYID CITYNAME
10 hyd
20 chennai
30 banglore
40 pun
because child records found in other table so we cant delete.if you want
to delete ,
first you should delete child records from other table
and then delete record from parent table.
=========================================================================
====
ON DELETE CASCADE:
sys_C00098(constrant_name)
now if you delete record from parent table ,parallely child records also
deleted from another table.
=========================================================================
=================================
composite key:
=========================================================================
=============================
column concatination:
sql>
select f_name as first_name,concat(f_name,l_name) as FULLNAME from emp;
FIRST_NAME FULLNAME
chandra chandrababu
sai saibabu
sree sreekanth
=========================================================================
==============================
create table emp(id number(10),fname varchar2(20),lname varchar2(20));
ID FNAME LNAME
3 sree kanth
2 sai babu
1 chandra babu
3 sree kanth
COUNT(DISTINCTID)
3
op:MONDAY
SQL>select to_char('05-05-2020','mm') from emp;
op:05
SQL>select to_char('05-05-2020','yyyy') from emp;
op:2020
SQL>select to_char('05-05-2020','dd') from emp;
op:05
concatination:
-----------------------------------------
or
distinct:
========================================================================
note:we cant select rownum ">= or ">" ,it works with "<" or "<=" only.
and rownum will be changed in future(because if we delete any
record,automatically rownum wil changed.
=========================================================================
=========================================================================
===
=========================================================================
====
dummy table
=========================================================================
====
Like %
output:Sandeep, SandeepKumar
output: sriraghava
ends with raghava
op:chandrababunaidu
in between babu should exist.
op:: cbabu (only 5 letter should present and ends with babu.
=========================================================================
===
order by:
note:: first it wil sort the data according to first column, if first
column has duplicates then it sort based on second coulmn..
=======================================================================
substr:
op::
Accenture
=========================================================================
==
instr::
op:3
----------------------------------------------------------
select instr('aa-ba-aa-ba-aa-ba','ba',1,1) from dual;
op::4
----------------------------------------------------------
select instr('aa-ba-aa-ba-aa-ba','ba',1,2) from dual;
Note: 1>> it represents 1stposition, 2 >> reprsents second occuranceof
"ba"
op::10
--------------------------------------------------------------
HR~123 AND INSURANCE~256(COMBINATION OF DEPARTMENT AND DEPT ID)
FROM THESE TWO WORDS EXTRACT ONLY WORDS NOT DIGITS WITH SINGLE
LOGIC.NORMALLY WE
CAN EXTRACT WORDS BY USING SUBSTR. BUT WHEN IT COMES TO LOGIC IT APPLY
FOR ONLY SINGLE TERM.
BY USING INSTR WE CAN MAKE A LOGIC, WHICH APPLICABLE FOR ALL.
note : here the logic is same but applied for both words(HR~123 AND
insurance~256)
op: 256
OP::256
======================================================================
LPAD:
OP:: ********WELCOME
RPAD:
op: WELCOME********
========================================================================
LTRIM:
select ltrim(' SANDEEP') from dual;
OP:: SANDEEEP
TRIME:
op::SUN DEEP
NOTE: LEFT SPACES AND RIGHT SIDE SPACES DELETED, BUT INBETWEEN WONT BE
DELETED.
=====================================================================
REPLACE:
op::sundeep
hadooop developer
========================================================================
select ltrim('012021021102547021011022101022','021') from dual;
op::547021011022101022
012021021102547
=========================================================================
==
Translate:
op::WXYZ-EFGH
=========================================================================
==
NVL:
op:: 10
note:
nvl(arg1,arg2)
if arg1 =null
then it will return arg2
if arg1!= null
then it returns arg1.
========================================================================
NVL2:
it takes 3 arguements.
nvl(arg1,arg2,arg3)
op: 30
select
emp_id,emp_name,allocation_id,nvl2(allocation_id,'ALLOCATED','WAITING FOR
PROJECT') FROM DUAL;
COALESCE:
=========================================================================
==
round:
select round(456.56) from dual;
op:: 457
select round(456.56,1) from dual;
op: 457
op: 457
========================================================================
MONTHSS_BETWEEN
select months_between(to_date('2025-01-01','yyyy-mm-dd'),to_date('2023-
01-01','yyyy-mm-dd')) as months from dual;
MONTHS
25
=======================================================================
days between:
select to_date('2023-30-01','yyyy-dd-mm')-to_date('2022-20-02','yyyy-dd-
mm') days from dual;
344
=========================================================================
joins:
1.implicit method
2.ansi method
1).implicit method:
Inner join:
-----------
select a.cust_id,a.cust_name,a.id,a.mail,b.counry,b.country_id from
customer a, country b
where a.id=b.country_id
right join:
-----------
select a.cust_id,a.cust_name,a.id,a.mail,b.counry,b.country_id from
customer a, country b
where a.id(+)=b.country_id
2).Ansi method:
---------------
-
=========================================================================
=
cross join:
Ansi method:
select a.cust_id,a.cust_name,a.id,a.mail,b.counry,b.country_id
from customer a, country b
=========================================================================
==
Subquery:
inner query is executed without outer query but outer query is not
executed
without innner query.
1.singlerow subquery:
select * from emp where dept_id < any (30,60,90) order bydept_id;
select * from emp where dept_id > all (30,60,90) order bydept_id;
mote: outer query returns records whenver dept id is greater than all of
mentioned deptids(30,60,90).
=========================================================================
==
=========================================================================
==
duplicate records:
=========================================================================
===
=================================================================
union all:
intersect:
------------
1).RANK()
Rank()
dense_rank()
row_number()
=========================================================================
==
top 5 salaries:
select * from
(select emp_id,f_name,salary,dept_id,rank() over(order by salary desc)
rnk from emp) where rnk<=5 ;
=========================================================================
===
second highest :
select * from
(select emp_id,f_name,salary,dept_id,rank() over(order by salary desc)
rnk from emp) where rnk=2 ;
=========================================================================
===
second highest salary deptwise and department name also should present.
LEAD():
select emp_id,emp_name,salary,hire_date,email,phone_number,dept_id,
lead(hire_date) over(order by hire_date) after_hire,
lead(first_name) over(order by hire_date) after_hire
from emp;
=========================================================================
========
LAG()
IF you wanted to know who is joined before this person.
select emp_id,emp_name,salary,hire_date,email,phone_number,dept_id,
lag(hire_date) over(order by hire_date) before_hire,
lag(first_name) over(order by hire_date) before_hire
from emp;
=========================================================================
=======
=========================================================================
==
=========================================================================
==
=========================================================================
===
=========================================================================
====