You are on page 1of 7

SQL:HL,NP,CMD

ddl(data defination language)--Create,alter,truncate,drop


dml(data manipulation language)---insert,update,delete,
(select)--drl(data retrival language
tcl(transaction control language)----commit, rollback,savepoint
dcl(data control language)----grant,revoke
_________________________________________
Datatypes in SQL:
Character: char within single quotes ,alpha numeric data
Char-0-2000 bytes:fixed length data type
character
char---fixed length data type
cname char(5)--- 'Anu' != 'Anu '
varchar-0 to 2000
varchar2-0 to 4000
cname varchar2(10) 'Anu'
long----------2gb
only 1 column of long type in a table
connot be indexed
cannot use as criteria (condition)
(Large objects)lob-clob(charcter large objects),(binary large objects)blob---- 4
gb
It points to the address of the objects
numeric--+ve,-ve,0,decimal
number
Maximum of 28 digits can be passed
age number(2)---90,100 ,9
sal number(9,2)--1.234, 123.4, 12345678
9 is the total number excluding decimal
2 is number to right of the decimal place
date---date and time sysdate
'dd-mon-yy'
hh:Mi:ss
binary --raw (2000 bytesog data),long raw (2gb), blob(4gb)
disadvantage of raw:Only retrive data but we cannot manipulate data
__________________________________________________________
drop table emp_dets
__________________________________________________________
CREATE TABLE EMP_DETS
(
EMP_ID NUMBER(4) constraint emp_eid_pk primary key,
EMP_NAME VARCHAR2(20),
EMP_DOB DATE constraint emp_edob_nn not null,
EMP_ADD VARCHAR2(40),
EMP_QUA VARCHAR2(10),
EMP_SAL NUMBER(9,2) constraint emp_esal_ch check (EMP_SAL > 20000),
EMP_MNGR NUMBER(4) constraint emp_emgr_fk references emp_dets(emp_id) on delete
set null

ALTER
ALTER
ALTER
ALTER
ALTER
ALTER
ALTER
ALTER
ALTER

TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE

EMP_DETS RENAME TO E;
E ADD EMP_PHNO NUMBER(10);
E MODIFY EMP_PHNO NUMBER(20);
E ADD CONSTRAINT E_EMPPHNO_U UNIQUE(EMP_PHNO);
E DROP CONSTRAINT E_EMPPHNO_U;
E DISABLE CONSTRAINT EMP_ESAL_CH;
E ENABLE CONSTRAINT EMP_ESAL_CH;
E DROP COLUMN EMP_PHNO;
E DROP COLUMN EMP_QUA;

__________________________________________________________
drop table customer;
__________________________________________________________
create table customer
(cid number(4) constraint c_pk primary key,
cname varchar2(10) constraint c_nn not null,
cdob date,
cadd varchar2(30),
cphno number(10) constraint c_u unique,
csal number(11,2) constraint c_ch check(csal>10000),
cmgr number(4) constraint c_fk references customer(cid) on delete set null)
____________________________________________________________
alter table emp_dets rename to e;
alter table e add empphono number(10);
alter table e modify empphono number(20);
alter table e add constraint e_empphno_U unique(empphono);......................
alter table e drop constraint e_empphono_U;............................
alter table e disable constraint emp_esal_ch;
alter table e enable constraint emp_esal_ch;
alter table e drop column empphono;
________________
truncate table e;
drop table e;
__________________________
dml
___________
insert,update,delete
insert into e values(101,'Anu','13-May-90','Bangalore',22000,null)
insert into e (empid,empdob,empname,empsal) values(102,'10-Oct-92','Anil',26000)
insert into e (empid,empdob,empname,empsal) values(&empid,&empdob,&empname,&emps
al)..................
commit;
insert into cust(cid,cname,cadd,csal,cmgr) values(103,'Chennai',14000,102);
insert into cust(cid,cname,cadd,csal,cmgr) values

insert into cust(cid,cname,cadd,csal,cmgr) values


commit;
select * from cust;
----------------------------------------------------------------------------------------------update e set empsal=25000
rollback
update e set empsal=25000 where empsal>30000
rollback
update e set empsal=25000,empmgr=101 where empsal=21000
rollback
update e set empsal=2500 empmgr=106 where empsal=21000 and empmgr is null.......
..........
-----------------------------------------------------------------------------------------------------update cust set cdob='10-Oct-91'
rollback;
update cust set cdob='10-Oct-91' where cdob is null;
update cust set cdob='10-Oct-91',csal=25000 where cdob is null and cmgr=102;
commit;
delete from e where empsal<15000;
commit;
_____________________________________________
empdet
________
eid---pk
ename---nn
edoj-esal-->20000 and <40000
emgr--fk
erole
____________________
ddl-create,alter,truncate,drop
________________________________
dml--insert,update,delete,select
insert into empdet values(10,'Ravi','12-may-90',25000,null,'SE')
insert into empdet (eid,ename,esal,edoj) values (11,'Lalit',34000,sysdate)
insert into empdet (eid,ename,esal,edoj) values
(&eid,'&ename',&esal,'&edoj')
commit
select * from empdet
________________________________________
update empdet set emgr=11
update empdet set emgr=11 where esal>25000
update empdet set emgr=11,erole='PA' where esal>25000
update empdet set emgr=11,erole='PA' where esal>25000 and eid>12

select * from empdet


___________
delete from empdet
rollback
delete from empdet where edoj=sysdate
rollback
__________________
Select * from emp
select empno,ename,hiredate,sal from emp
select empno,ename,hiredate,sal from emp where sal>1000
select empno,ename,hiredate,
from emp
select empno,ename,hiredate,sal*0.1 as esal from emp where deptno=10 AND SAL>200
0
select ename || ' Works as ' || job as "emp_job" from emp
_____________________________________________________
Operators:
Arithmatic operators:+,-,*,/,()
Select hiredate,hiredate+2 from emp
Relational-----=,<>,!=,>,<,>=,<=,BETWEEN,IN,LIKE,IS NULL
Select * from emp where lower(job) = 'manager'
Logical----- AND,OR,NOT
Set-------- UNION ALL,UNION,INTERSECT,MINUS
____________________________________
JOB ='CLERK'
SELECT * FROM EMP WHERE SAL BETWEEN 1000 AND 3000
______________________________________________________
SELECT * FROM EMP WHERE JOB IN ('CLERK','MANAGER','ANALYST','SALESMAN')
________________________________
LIKE----------% CAN REPLACE ANY NUMBER OF ANY CHARACTER,
_ CAN REPLACE ANY SINGLE CHARACTER
SELECT * FROM EMP WHERE JOB LIKE '%MAN%'
SELECT * FROM EMP WHERE JOB LIKE 'S%'
SELECT * FROM EMP WHERE hiredate LIKE '___FEB___'
select * from emp where job like 'MANAGER' and sal>2000
___
SELECT * FROM EMP WHERE JOB NOT LIKE 'CLERK'
NOT IN
NOT BETWEEN
IS NOT NULL
___________________
select deptno from emp union all select deptno from dept
which are the dept in which no employee works

select deptno from dept minus select deptno from emp


_________________________________________________________
select * from emp where job like 'MANAGER'
intersect select * from emp where sal>2000
--------------------------------------------------------------------------single row/scalar functions
--- upper(ename)---18----18-- max(sal)---1 value
character
__________
initcap,upper,lower,ltrim,rtrim,lpad,rpad,length,substr,replace,instr,soundex
select ename,initcap(ename),ltrim(ename,'S'),rpad(ename,10,'#'),length(ename),re
place(ename,'A','@'),substr(ename,2,3) xyz from emp
select
select
select
select
select
select
) from

12+13-6*7/2+100 from dual


sysdate,sysdate+10,sysdate-2 from dual
instr('hello','l') from dual
instr('hello','l',3) from dual
instr('hello','l',3,2) from dual
instr('hello','l',3,4
dual

select * from emp where soundex(ename) LIKE soundex('jons')


________________________________
numeric----abs,sign,round,trunc,floor,ceil,mod,power,sqrt
select abs(-90),sign(-22),round(123.456,1),trunc(123.456,2),floor(8.989898),cei
l(7.11111),mod(8,3),power(4,3),sqrt(64) from dual
_______________________________________
Date functions
_______________
Add_months
months_between
next_day
last_day
select add_months(sysdate,3),add_months(sysdate,-3) from dual
select hiredate from emp
select trunc(months_between(sysdate,hiredate)/12) eyofexp from emp
select next_day(sysdate,'Sunday') from dual
select next_day('10-Mar-11',2) from dual
select last_day(sysdate) from dual
_____________________________________________
----- general functions---------nvl
select empno,ename,nvl(sal,0),nvl(to_char(mgr),'NA') from emp
select empno,ename,sal,job,decode(deptno,10,'a',20,'b',null,'x','y') from emp
_____________________________________
conversion function

select to_char(sysdate,'ddspth Month Year') from dual


_____________________________________________________________
----------Sorting Data--------------------select * from emp order by empno;
select * from emp order by empno desc
select * from emp order by sal
select * from emp order by sal,ename
select * from emp order by sal,ename desc
select * from emp order by sal desc,ename
select empno,ename,job,hiredate from emp order by 4
select empno,sal*0.1 esal,job,mgr from emp order by esal
_____________________________
---------- Group Functions and Grouping Date------sum,min,max,avg,count
select sum(sal),min(hiredate),max(hiredate) ,avg(sal) from emp
select count(*),count(sal),count(distict job ) from emp
select deptno,min(sal) from emp where job not like 'CLERK' group by deptno
find out dept wise min sal excluding clerks
select deptno, job,min(sal),count(*) hc from emp where job not like 'CLERK' grou
p by deptno,job having count(*)>1 order by hc
select * from emp where ename like 'S%' and job like 'M_N%';
____________________________________________________________
-------------- subqueries-----------------DETAILS OF ALL EMP REPORTING TO BLAKE
SELECT EMPNO FROM EMP WHERE ENAME LIKE 'BLAKE'
SELECT * FROM EMP WHERE MGR IN (SELECT EMPNO FROM EMP WHERE ENAME LIKE 'BLAKE')
GET THE DETAILS OF ALL EMP WORKING WITH JONES
SELECT * FROM EMP WHERE DEPTNO IN(SELECT DEPTNO FROM EMP WHERE ENAME LIKE 'JONES
')
SELECT * FROM EMP WHERE SAL =
(SELECT MAX(SAL) FROM EMP)
------------------- Table Joins----------------------select e.empno,e.ename,e.job,e.sal,e.hiredate,e.deptno,d.dname,d.loc from emp e,
dept d where e.deptno=d.deptno(+)
select e.empno,e.ename,e.job,e.sal,e.hiredate,e.deptno,d.dname,d.loc
from emp e left outer join dept d on e.deptno=d.deptno
___________________________________________________________
select e.empno,e.ename,e.job,e.sal,e.hiredate,e.deptno,d.dname,d.loc from emp e,
dept d where e.deptno(+)=d.deptno
select e.empno,e.ename,e.job,e.sal,e.hiredate,e.deptno,d.dname,d.loc
from emp e right outer join dept d on e.deptno=d.deptno
________________________________________________________
select e.empno,e.ename,e.job,e.sal,e.hiredate,e.deptno,d.dname,d.loc
from emp e full outer join dept d on e.deptno=d.deptno
_______________________________________________________

salgrade
select e.*,s.grade from emp e,salgrade s where e.sal between s.losal and s.hisal
___________________________________________
select e.ename emp_name,m.ename mgr_name from emp e,emp m where e.mgr=m.empno
__________________________________________________
select e.ename emp_name ||' Reports to '||m.ename mgr_name from emp e,emp m w
here e.mgr=m.empno

You might also like