Вы находитесь на странице: 1из 61

Assignment -1 CREATE SCRIPT CREATE TABLE DEPOSIT (ACTNO VARCHAR2(5) ,CNAME VARCHAR2(18) , VARCHAR2(18) , AMOUNT NUMBER(8,2) ,ADATE DATE);

CREATE TABLE BRANCH (BNAME VARCHAR2(18),CITY VARCHAR2(18)); CREATE TABLE CUSTOMERS (CNAME VARCHAR2(19) ,CITY VARCHAR2(18)); CREATE TABLE BORROW (LOANNO VARCHAR2(5), CNAME VARCHAR2(18), VARCHAR2(18),AMOUNT NUMBER (8,2)); DESC SCRIPT DESC DEPOSIT; DESC BRANCH;s DESC CUSTOMERS; DESC BORROW;

BNAME

BNAME

INSERT SCRIPT INSERT INTO DEPOSIT VALUES ('100',ANIL',VRCE',1000.00,'1-MAR-95'); / INSERT INTO DEPOSIT VALUES ('101','SUNIL','AJNI',5000.00 , '4-JAN-96' ); / INSERT INTO DEPOSIT VALUES ('102','MEHUL','KAROLBAGH',3500.00 ,'17-NOV-95' ); / INSERT INTO DEPOSIT VALUES ('104','MADHURI','CHANDNI',1200.00 , 17-DEC-95' ); / INSERT INTO DEPOSIT VALUES ('105','PRAMOD','M.G.ROAD',3000.00 '27-MAR-96); / INSERT INTO DEPOSIT VALUES ('106','SANDIP','ANDHERI',2000.00,'31-MAR-96');

/ INSERT INTO DEPOSIT VALUES ('107','SHIVANI','VIRAR',1000.00,'5-SEP-95' ); / INSERT INTO DEPOSIT VALUES ('108','KRANTI','NEHRU PLACE',5000.00,2-JUL-95'); / INSERT INTO DEPOSIT VALUES ('109','NAREN','POWAI',7000.00,'10-AUG-95'); / INSERT INTO BRANCH VALUES ('VRCE','NAGPUR'); INSERT INTO BRANCH VALUES ('AJNI','NAGPUR'); INSERT INTO BRANCH VALUES ('KAROLBAGH','DELHI'); INSERT INTO BRANCH VALUES ('CHANDNI','DELHI'); INSERT INTO BRANCH VALUES ('DHARAMPETH','NAGPUR'); INSERT INTO BRANCH VALUES ('M.G.ROAD','BANGLORE'); INSERT INTO BRANCH VALUES ('ANDHERI','BOMBAY'); INSERT INTO BRANCH VALUES ('VIRAR','BOMBAY'); INSERT INTO BRANCH VALUES ('NEHRU PLACE','DELHI'); INSERT INTO BRANCH VALUES ('POWAI','BOMBAY'); INSERT INTO CUSTOMERS VALUES ('ANIL','CALCUTTA'); INSERT INTO CUSTOMERS VALUES ('SUNIL','DELHI'); INSERT INTO CUSTOMERS VALUES ('MEHUL','BARODA'); INSERT INTO CUSTOMERS VALUES ('MANDAR','PATNA'); INSERT INTO CUSTOMERS VALUES ('MADHURI','NAGPUR'); INSERT INTO CUSOTMERS VALUES ('PRAMOD','NAGPUR'); INSERT INTO CUSTOMERS VALUES ('SANDIP','SURAT'); INSERT INTO CUSTOMERS VALUES ('SHIVANI','BOMBAY'); INSERT INTO CUSTOMERS VALUES ('KRANTI','BOMBAY'); INSERT INTO CUSTOMERS VALUES ('NAREN','BOMBAY'); INSERT INTO BORROW VALUES ('201','ANIL','VRCE',1000.00); INSERT INTO BORROW VALUES ('206','MEHUL','AJNI',5000.00); INSERT INTO BORROW VALUES ('311','SUNIL','DHARAMPETH',3000.00);

INSERT INTO BORROW VALUES ('321','MADHURI','ANDHERI',2000.00); INSERT INTO BORROW VALUES ('375','PRAMOD','VIRAR',8000.00); INSERT INTO BORROW VALUES ('481','KRANTI','NEHRU PLACE',3000.00); 1. LIST ALL DATA FROM TABLE DEPOSIT. SELECT * FROM DEPOSIT; 2. 3. LIST ALL DATA FROM TABLE BORROW. SELECT * FROM BORROW; LIST ALL DATA FROM TABLE CUSTOMERS. SELECT * FROM CUSTOMERS; Ex.1 List name of companies located in BOMBAY. SQL> select cname from company where city =`BOMBAY'; Ex.2 list name of employees living in city `NAGPUR' SQL> select ename from employees where city = `Nagpur'; EX.3 list name of employees having company `ACC' AND RECEIVING SALARY GREATER THAN 1000.00 SQL> select ename from emp_company where cname=`ACC' and salary>1000; Example 4. List the names of employee who are not employee with the company ACC. SQL>select ename from emp_company where not (cname=ACC); SQL>select ename from emp_company where cname<>ACC; Example 5. Suppose you want the names of employee from company ACC or TATA. SQL>select ename from emp_company where cname =ACC or cname = TATA; CARTESIAN_PRODUCT

Example 1. Give name of employee whose company is located in city DELHI. SQL>select emp_company.ename from emp_company, company where emp_company.cname = company.cname and company.city = DELHI; Example 2. Give name of employee living in the same city where their company is located. SQL>select employee.ename from employee,emp_company,company where employee.ename = emp_company.ename and emp_company.cname = company.cname and employee.city = company.city; Example 3. Give name of employee living in city BOMBAY and having company located in city DELHI. SQL>select employee.ename from employee,emp_company,company where employee.ename = emp_company.ename and emp_company.cname = company.cname and employee.city = BOMBAY and company.city = DELHI Example. 4. Give name of manager and salary of employee SUNIL. SQL>select emp_company.salary,manager.mname from emp_company,manager where emp_company.ename = SUNIL and emp_company.ename = manager.ename; EXIT_clause Example 1. Give the salary of employee living in DELHI. SQL>select salary from emp_company where exists (select X from employee where employee.city = DELHI and employee.ename = emp_company.ename);

Example 2. Give name of companies located in those cities where TATA is located. SQL>select cname from company where not exists((select city from company cmp1 where cname = TATA) minus (select cmp2.city from company cmp2 where cmp2.cname = company.cname)); try the following example using only in 1. List name of employees having living city same as 'Ajay' 2. List name of Employees with their living city having shift B and company 'ACC' 3. List name of employees with their living city having company city 'Bombay' 4. List name of employees having living city same as company city 5. List name of employee having same company city as company city of 'Ajay' 1. select e.ename,e.city from employee e where exists (select 'X' from employee e1 where e1.ename='Ajay' and e.city = e1.city); 2. select e.ename,e.city from employee e where exists (select 'X' from emp_company c where c.cname='ACC'and e.ename=c.ename) // anil sunil and exists (select 'X' from emp_shift s where s.shift='B'and e.ename=s.ename); //ajay sunil 3. select e.ename,e.city from employee e where e.ename in

(select c.ename --ajay sunil vijay from emp_company c where c.cname in (select m.cname -- ACC TATA from company m where m.city='Bombay') ); select e.ename,e.city from employee e where exists (select'X' --ajay sunil vijay from emp_company c where e.ename=c.ename and exists (select 'X' from company m where c.cname = m.cname and m.city='Bombay') ); 4. select e1.ename from employee e1 where e1.city in (select c1.city from company c1 where c1.cname in (select m1.cname from emp_compay m1 where m1.ename=e1.ename)) ; using exists 4. select e1.ename ajay from employee e1 where exists (select 'X' from company c1 where c1.city = e1.city and exists (select 'X' from emp_compay m1 where m1.ename=e1.ename and c1.cname=m1.cname)); 5.

select e1.ename --all employees working in ACC,TATA from emp_company e1 where e1.cname in (select c1.cname -- ACC,TATA from company c1 where c1.city in (select c2.city --Bombay from company c2 where c2.cname in -- ACC (select e2.cname from emp_company e2 where e2.ename='Ajay'))); IN_clause Example 1. List the employee living in city BOMBAY and those having company located in city DELHI. SQL>select ename from emp_company where ename In (select ename from employee where city = BOMBAY) and cname In (select cname from company where city = DELHI); Example 2. List the names of employee living in the same city as where SUNIL is living. SQL>select ename from employee where city in (select city from employee where ename = SUNIL); SELF_JOIN Example 1. Give name of employee having same company city as SUNIL. SQL>select employee_cmp2.ename from emp_company employee_cmp1,company company1, emp_company employee_cmp2,company company2 Where employee_cmp1.cname = company1.cname and employee_cmp1.ename = SUNIL and employee_cmp2.cname = company2.cname and company2.city = company1.city / SET( intersect, minus and union) list name of employees who don't have shift

select ename from employee minus select ename from emp_shift / Examples 1.list name of employees living in nagpur and working with company ACC 2.list name of employee living in Nagpur but not working in company ACC 3.list name of employee living in nagpur ,working in ACC and having shift A 4.list name of employee living in Nagpur and having company ACC ,don't have shift A 5.list name of employee who are living in nagpur or bombay 1.select ename from employee where city = 'Nagpur' intersect select ename from emp_company where cname = 'ACC' / 2.select ename from employee where city = 'Nagpur' minus select ename from emp_company where cname = 'ACC' / 3.select ename from employee where city = 'Nagpur' intersect select ename from emp_company where cname = 'ACC' intersect select ename from emp_shift where shift = 'A' /

4. (select ename from employee where city='Nagpur' intersect select ename from emp_company where cname='ACC') minus select ename from emp_shift where shift='A' / 5. select ename from employee where city='Nagpur' union select ename from employee where city='Bombay' / All_any SQL> select ename,salary from emp_company; ENAME SALARY ---------- --------Anil 1500 Shankar 2000 Jaya 1800 Sunil 1700 Vijay 5000 Prakash 3000 Ajay 8000 list name of employee having highest salary SQL> select ename 2 from emp_company 3 where salary >= all(select salary from emp_company); ENAME ----------

Ajay SQL> select ename 2 from emp_company 3 where salary > any(select salary from emp_company); ENAME ---------Shankar Jaya Sunil Vijay Prakash Ajay 6 rows selected. list name of company having highest avg. deposit select e.cname,avg(e.salary) from emp_company e group by cname having avg(e.salary) > = all(select avg(c.salary) from emp_company c group by c.cname)

7 rows selected. SQL> spool off try these examples 1. List name of company having highest number of employees 2. List name of employee from ACC having highest salary in company ACC. 1. List name of company having highest number of employees 1. select e.cname,count(e.ename) from emp_company e

group by cname having count(e.ename) > = all(select count(c.ename) from emp_company c group by c.cname); 2. List name of employee from ACC having highest salary in company ACC. 2. select e1.ename from emp_company e1 where e1.cname='ACC' and e1.salary >=All(select e2.salary from emp_company e2 where e2.cname='ACC') DISTINCT OPERATOR DISTINCT OPERATOR SQL> select distinct cname from emp_company; CNAME ---------Acc CMC TATA SQL> select cname from emp_company; CNAME ---------Acc TATA CMC CMC TATA TATA ACC 7 rows selected. SQL> select count(cname) from emp_company;

COUNT(CNAME) -----------7 SQL> select count(distinct cname) from emp_company; COUNT(DISTINCTCNAME) -------------------4 SQL> insert into emp_company(ename,salary) values('prakash',5000); 1 row created. SQL> commit; Commit complete. SQL> select count(cname) from emp_company; COUNT(CNAME) -----------7 SQL> select count(distinct cname) from emp_company; COUNT(DISTINCTCNAME) -------------------4 SQL> edit Wrote file afiedt.buf 1 select ename from emp_company 2 union 3* select ename from emp_shift SQL> / ENAME ---------Ajay

Anil Jaya Prakash Shankar Sunil Vijay prakash 8 rows selected. SQL> edit Wrote file afiedt.buf 1 select ename from emp_company 2 union all 3* select ename from emp_shift SQL> / ENAME ---------Anil Shankar Jaya Sunil Vijay Prakash Ajay prakash Anil Sunil Vijay Prakash 12 rows selected. SQL> insert into emp_company(ename,salary) values('prakash',4000); 1 row created.

SQL>select ename from emp_company 2 minus 3* select ename from emp_shift SQL> / ENAME ---------Ajay Jaya Shankar prakash SQL> spool off SQL> select ename from emp_company 2 union 3 select ename from emp_company 4 union all 5* select ename from emp_company 6 / ENAME ---------Ajay Anil Jaya Prakash Shankar Sunil Vijay prakash Anil Shankar Jaya Sunil Vijay Prakash Ajay prakash

prakash prakash 18 rows selected. Like operator SQL> select * from employee where ename like 'A%'; --% means zero or more characters ENAME CITY ---------- ---------Anil Nagpur Ajay Madras ename = 'A%' -- wrong

1* select * from employee where ename like 'A_ay%' -- '_' means any one character SQL> / ENAME CITY ---------- ---------Ajay Madras If your data is SQL> insert into employee values('A%B','bm'); 1 row created. You want to retrieve rows which starts with A%. here you want to shut down special meaning of character %. You can use essacpe character / for doing this. You can write query as select ename from employee where ename like 'A/%%' escape '/' The null operator insert into emp_company(ename,salary) values ('prakash',5000)

or insert into emp_company values ('prakash',null,5000,null) SQL> select * from emp_company; ENAME CNAME SALARY JDATE ---------- ---------- --------- --------Anil Acc 1500 01-MAY-89 Shankar TATA 2000 10-JUL-90 Jaya CMC 1800 07-JUN-91 Sunil CMC 1700 01-JAN-88 Vijay TATA 5000 03-JAN-88 Prakash TATA 3000 27-MAY-89 Ajay ACC 8000 30-APR-95 prakash 5000 8 rows selected. SQL> select * from emp_company where cname=''; no rows selected SQL> select * from emp_company where cname=null; no rows selected SQL> select * from emp_company where not(cname=null); no rows selected SQL> select * from emp_company where cname is null; ENAME CNAME SALARY JDATE ---------- ---------- --------- --------prakash 5000 compliment is not null SQL> select 5*4 from dual;

5*4 --------20 SQL> select 5*4+null from dual; 5*4+NULL ---------

1* select * from emp_company where cname=null or salary>=8000 SQL> / ENAME CNAME SALARY JDATE ---------- ---------- --------- --------Ajay ACC 8000 30-APR-95 SQL> select ename,nvl(cname,'No Company') from emp_company; ENAME NVL(CNAME, ---------- ---------Anil Acc Shankar TATA Jaya CMC Sunil CMC Vijay TATA Prakash TATA Ajay ACC prakash No Company 8 rows selected. SQL> select max(cname) from emp_company; MAX(CNAME) ---------TATA SQL> select count(cname) from emp_company;

SQL char function SQL> select ascii('Abc') from dual; ASCII('ABC') -----------65 SQL> select chr(65) from dual; C A SQL> select initcap('abcDe') from dual; INITC ----Abcde SQL> select upper('abCfg') from dual; UPPER ----ABCFG SQL> select lower('sfdsFG') from dual; LOWER( -----sfdsfg SQL> select length('abcdf') from dual; LENGTH('ABCDF') --------------5 SQL> select lengthb('abcdf') from dual;

LENGTHB('ABCDF') ---------------5 SQL> select lengthb(34) from dual; LENGTHB(34) ----------2 SQL> select lengthb(1000) from dual; LENGTHB(1000) ------------4 SQL> select lengthb(516) from dual; LENGTHB(516) -----------3 SQL> select lengthb(sysdate) from dual; LENGTHB(SYSDATE) ---------------9 SQL> select substr('abcdef',3,2) from dual; SU -cd SQL> edit Wrote file afiedt.buf 1* select substr('abcdef',3,5) from dual SQL> /

SUBS ---cdef SQL> edit Wrote file afiedt.buf 1* select substr('abcdef',13,5) from dual SQL> / S -- Finds first occurence of 'bb' in 'abbcdgbb' from position 1 and returns it's position SQL> edit Wrote file afiedt.buf 1 select instr('abbcdgbb','bb',1,1) 2* from dual SQL> / INSTR('ABBCDGBB','BB',1,1) -------------------------2 SQL> edit Wrote file afiedt.buf --Finds second occurence of 'bb' in 'abbcdgbb' from position 1 and returns it's position 1 select instr('abbcdgbb','bb',1,2) 2* from dual SQL> / INSTR('ABBCDGBB','BB',1,2) -------------------------7

Finds second occurence of 'bb' in 'abbcdgbb' from position 4 and returns it's position SQL> edit Wrote file afiedt.buf 1 select instr('abbcdgbb','bb',4,2) 2* from dual SQL> / INSTR('ABBCDGBB','BB',4,2) -------------------------0 --left padding using blank till the length 8 ie reached SQL> select lpad('abc',8) from dual; LPAD('AB -------abc SQL> edit Wrote file afiedt.buf 1* select length(lpad('abc',8)) from dual SQL> / LENGTH(LPAD('ABC',8)) --------------------8 SQL> edit Wrote file afiedt.buf 1* select length(lpad('abc',2)) from dual SQL> / LENGTH(LPAD('ABC',2)) --------------------2

SQL> edit Wrote file afiedt.buf 1* select lpad('abc',2) from dual SQL> / LP -ab SQL> edit ----left padding using 'as' till the length 8 ie reached 1* select lpad('abc',8,'as') from dual SQL> / LPAD('AB -------asasaabc SQL> edit Wrote file afiedt.buf ----right padding using 'as' till the length 8 ie reached 1* select rpad('abc',8,'as') from dual SQL> / RPAD('AB -------abcasasa SQL> edit Wrote file afiedt.buf --right trimming of blanks 1* select rtrim('abc ') from dual SQL> / RTR

--abc SQL> edit Wrote file afiedt.buf 1* select length(rtrim('abc ')) from dual SQL> / LENGTH(RTRIM('ABC')) -------------------3 SQL> edit SQL> edit Wrote file afiedt.buf --right trimming of string i.e removing char 'c' from right end 1* select length(rtrim('abc ','c')) from dual SQL> / LENGTH(RTRIM('ABC','C')) -----------------------5 SQL> edit Wrote file afiedt.buf 1* select length(rtrim('abcc','c')) from dual SQL> / LENGTH(RTRIM('ABCC','C')) ------------------------2 SQL> edit Wrote file afiedt.buf --right trimming of string i.e removing char 'c' from right end

1* select (rtrim('abcc','c')) from dual SQL> / (R -ab SQL> edit Wrote file afiedt.buf --right trimming of string i.e removing char 'bc' from right end 1* select (rtrim('abcc','bc')) from dual SQL> / ( a SQL> select 'X' from dual where 'son'='sun'; no rows selected SQL> edit Wrote file afiedt.buf -- comparison true if both are producing same sound 1* select 'X' from dual where soundex('son')=soundex('sun') SQL> / ' X Date Function SQL> select sysdate from dual; SYSDATE --------03-JAN-02

SQL> select to_char(sysdate,'mm') from dual; TO -01 SQL> edit Wrote file afiedt.buf 1* select to_char(sysdate,'mmsp') from dual SQL> / TO_CHA -----one SQL> edit Wrote file afiedt.buf 1* select to_char(sysdate,'mmspth') from dual SQL> / TO_CHAR( -------first SQL> select to_char(sysdate,'yyyyspth') from dual SQL> / TO_CHAR(SYSDATE,'YYYYSPTH') -------------------------------------------two thousand second SQL> edit Wrote file afiedt.buf 1* select to_char(sysdate,'year') from dual SQL> /

TO_CHAR(SYSDATE,'YEAR') -----------------------------------------two thousand two SQL> select to_char(sysdate,'dd-mon-rr') from dual; TO_CHAR(S --------03-jan-02 SQL> select new_time(sysdate,'GMT','PST') FROM DUAL; NEW_TIME( --------03-JAN-02 SQL> EDIT Wrote file afiedt.buf 1* select TO_CHAR(new_time(sysdate,'GMT','PST'),'DD-MON-YYYY HH:MI:SS') FROM DUAL SQL> / TO_CHAR(NEW_TIME(SYS -------------------03-JAN-2002 12:21:08 SQL> SELECT TO_CHAR(SYSDATE,'HH12AM') FROM DUAL; TO_C ---08PM SQL> SELECT TO_CHAR(SYSDATE,'D') FROM DUAL; T 5 SQL> SELECT TO_CHAR(SYSDATE,'DD') FROM DUAL;

TO -03 SQL> SELECT TO_CHAR(SYSDATE,'DDD') FROM DUAL; TO_ --003 SQL> SELECT TO_CHAR(SYSDATE,'DDDD') FROM DUAL; TO_C ---0035 SQL> SELECT TO_CHAR(SYSDATE,'W') FROM DUAL; T 1 SQL> SELECT TO_CHAR(SYSDATE,'WW') FROM DUAL; TO -01 SQL> EDIT Wrote file afiedt.buf 1* SELECT TO_CHAR(SYSDATE,'IW') FROM DUAL SQL> / TO -01 SQL> SELECT TO_CHAR(SYSDATE,'J') FROM DUAL;

TO_CHAR ------2452278 SQL> SQL> SQL> SQL> EDIT Wrote file afiedt.buf 1* SELECT TO_CHAR(SYSDATE,'SYYYY') FROM DUAL SQL> / TO_CH ----2002 SQL> SELECT TO_CHAR(SYSDATE,'DD-RM-YYYY') FROM DUAL; TO_CHAR(SYSD -----------03-I -2002 SQL> SELECT TO_CHAR(SYSDATE,'RD') FROM DUAL; SELECT TO_CHAR(SYSDATE,'RD') FROM DUAL * ERROR at line 1: ORA-01821: date format not recognized SQL> SELECT TRUNC(SYSDATE,'MM') FROM DUAL; TRUNC(SYS --------01-JAN-02 SQL> EDIT Wrote file afiedt.buf

1* SELECT TRUNC(SYSDATE-10,'MM') FROM DUAL SQL> / TRUNC(SYS --------01-DEC-01 SQL> EDIT Wrote file afiedt.buf 1* SELECT TRUNC(SYSDATE-10,'YY') FROM DUAL SQL> / TRUNC(SYS --------01-JAN-01 SQL> EDIT Wrote file afiedt.buf 1* SELECT TRUNC(SYSDATE,'HH') FROM DUAL SQL> / TRUNC(SYS --------03-JAN-02 SQL> EDIT Wrote file afiedt.buf 1* SELECT TO_CHAR(TRUNC(SYSDATE,'HH'),'HH:MI:SS') FROM DUAL SQL> / TO_CHAR( -------08:00:00 SQL> EDIT Wrote file afiedt.buf

1* SELECT ROUND(SYSDATE+12,'MM') FROM DUAL SQL> / ROUND(SYS --------01-JAN-02 SQL> EDIT Wrote file afiedt.buf 1* SELECT ROUND(SYSDATE+13,'MM') FROM DUAL SQL> / ROUND(SYS --------01-FEB-02 SQL> EDIT Wrote file afiedt.buf 1* SELECT ROUND(SYSDATE+13,'Q') FROM DUAL SQL> / ROUND(SYS --------01-JAN-02 SQL Function SQL> select '5' from dual; ' 5 SQL> select round(123.556) from dual; ROUND(123.556) -------------124

SQL> select round(123.456,1) from dual; ROUND(123.456,1) ---------------123.5 SQL> edit Wrote file afiedt.buf 1* select round(123.456,2) from dual SQL> / ROUND(123.456,2) ---------------123.46 SQL> edit Wrote file afiedt.buf 1* select round(123.456,3) from dual SQL> / ROUND(123.456,3) ---------------123.456 SQL> edit Wrote file afiedt.buf 1* select round(123.456,-1) from dual SQL> / ROUND(123.456,-1) ----------------120 SQL> edit Wrote file afiedt.buf

1* select round(123.456,-2) from dual SQL> / ROUND(123.456,-2) ----------------100 SQL> edit Wrote file afiedt.buf 1* select round(123.456,-3) from dual SQL> / ROUND(123.456,-3) ----------------0 SQL> edit Wrote file afiedt.buf 1* select round(173.456,-2) from dual SQL> / ROUND(173.456,-2) ----------------200 SQL> edit Wrote file afiedt.buf 1* select trunc(173.456,-2) from dual SQL> / TRUNC(173.456,-2) ----------------100 SQL> edit Wrote file afiedt.buf

1* select trunc(173.456,-1) from dual SQL> / TRUNC(173.456,-1) ----------------170 SQL> edit Wrote file afiedt.buf 1* select trunc(173.456,1) from dual SQL> / TRUNC(173.456,1) ---------------173.4 SQL> edit Wrote file afiedt.buf 1* select trunc(173.456,2) from dual SQL> / TRUNC(173.456,2) ---------------173.45 SQL> spool off list statistics of employees in following way salary_low salary_high No_of_employees 0 999 1 1000 1999 3 2000 2999 2

select trunc(salary,-3) salary_low,trunc(salary,-3)+999 count(ename) from emp_company group by trunc(salary,-3),trunc(salary,-3)+999 SALARY_LOW SALARY_HIGH COUNT(ENAME) ---------- ----------- -----------1000 1999 3 2000 2999 1 3000 3999 3 5000 5999 2 6000 6999 1 7000 7999 1 6 rows selected.

salary_high,

1 select trunc(salary/5000)*5000 salary_low,trunc(salary/5000)*5000+4999 salary_high, count(ename) 2 from emp_company 3* group by trunc(salary/5000)*5000,trunc(salary/5000)*5000+4999 SQL> / SALARY_LOW SALARY_HIGH COUNT(ENAME) ---------- ----------- -----------0 4999 7 5000 9999 4

SQL> select abs(-9) from dual; ABS(-9) --------9 SQL> select ceil(-9.3) from dual; CEIL(-9.3) ----------9

SQL> select ceil(9.3) from dual; CEIL(9.3) --------10 SQL> select floor(-9.3) from dual; FLOOR(-9.3) -----------10 SQL> select floor(9.3) from dual; FLOOR(9.3) ---------9 SQL> select sign(-5) from dual; SIGN(-5) ---------1 SQL> select sign(5) from dual; SIGN(5) --------1 SQL> select sign(0) from dual; SIGN(0) --------0 SQL> select power(3,2) from dual; POWER(3,2)

---------9 SQL> select mod(7,3) from dual; MOD(7,3) --------1 SQL> select sqrt(9) from dual; SQRT(9) --------3 SQL_MISC_FUNCTIONS SQL> SELECT * FROM EMP_COMPANY WHERE ROWNUM < 4; ENAME CNAME SALARY JDATE ---------- ---------- --------- --------Anil Acc 1500 01-MAY-89 Shankar TATA 2000 10-JUL-90 Jaya CMC 1800 07-JUN-91 SQL> SELECT ENAME,CNAME FROM EMP_COMPANY ORDER BY SALARY; ENAME CNAME ---------- ---------Anil Acc Sunil CMC Jaya CMC Shankar TATA Prakash TATA Ashok TATA Vinod ACC prakash Vijay TATA Nitin ACC Ajay Tata

11 rows selected. SQL> EDIT Wrote file afiedt.buf 1* SELECT ENAME,CNAME FROM EMP_COMPANY ORDER BY 1 SQL> / ENAME CNAME ---------- ---------Ajay Tata Anil Acc Ashok TATA Jaya CMC Nitin ACC Prakash TATA Shankar TATA Sunil CMC Vijay TATA Vinod ACC prakash 11 rows selected. SQL> EDIT Wrote file afiedt.buf 1* SELECT ENAME,CNAME FROM EMP_COMPANY ORDER BY 2,1 SQL> / ENAME CNAME ---------- ---------Nitin ACC Vinod ACC Anil Acc Jaya CMC Sunil CMC Ashok TATA Prakash TATA

Shankar TATA Vijay TATA Ajay Tata prakash 11 rows selected. SQL> EDIT Wrote file afiedt.buf 1* SELECT ENAME,CNAME FROM EMP_COMPANY ORDER BY 2,1 DESC SQL> / ENAME CNAME ---------- ---------Vinod ACC Nitin ACC Anil Acc Sunil CMC Jaya CMC Vijay TATA Shankar TATA Prakash TATA Ashok TATA Ajay Tata prakash 11 rows selected. SQL> SELECT ENAME FROM EMP_COMPANY WHERE ROWNUM = 2; no rows selected SQL> SQL> SQL> SQL> SQL>

SQL> SELECT TRANSLATE('ABBCD','AB','*+') FROM DUAL; TRANS ----*++CD SQL> EDIT Wrote file afiedt.buf 1* SELECT TRANSLATE('ABBCD','AB','*') FROM DUAL SQL> / TRA --*CD SQL> INSERT INTO EMP_COMPANY(ENAME,JDATE) VALUES('GORGE',TO_DATE('12-DEC-2001 12:45:10','DD-MON-YYYY HH:MI:SS')); 1 row created. SQL> EDIT Wrote file afiedt.buf 1* INSERT INTO EMP_COMPANY(ENAME,JDATE) VALUES('GORGE',TO_DATE('12-DEC-2001 12:45:10','DD-MON-YYYY HH:MI:SS')) SQL> SELECT TO_NUMBER('34') + 10 FROM DUAL; TO_NUMBER('34')+10 -----------------44 SQL> select rowid,ename,salary from emp_company; ROWID ENAME SALARY ------------------ ---------- --------AAAUuAAAIAAALPvAAA Anil 1500 AAAUuAAAIAAALPvAAB Shankar 2000

AAAUuAAAIAAALPvAAC Jaya AAAUuAAAIAAALPvAAD Sunil AAAUuAAAIAAALPvAAE Vijay AAAUuAAAIAAALPvAAF Prakash AAAUuAAAIAAALPvAAG Ajay AAAUuAAAIAAALPwAAA prakash AAAUuAAAIAAALPwAAB Nitin AAAUuAAAIAAALPwAAC Ashok AAAUuAAAIAAALPwAAD Vinod AAAUuAAAIAAALPwAAE GORGE 12 rows selected.

1800 1700 5100 3000 7700 5000 6000 3000 3200

Rowid specifies position of row on disk.Rowid is unique for each row.It is constant throughout lifetime of that row. SQL> select rowid,ename,salary from emp_company rowid=chartorowid('AAAUuAAAIAAALPvAAB'); ROWID ENAME SALARY ------------------ ---------- --------AAAUuAAAIAAALPvAAB Shankar 2000 SQL> select decode(sign(salary - 4000),-1,'low salary',1,'high salary') from emp_company; DECODE(SIGN ----------low salary low salary low salary low salary high salary low salary high salary high salary high salary low salary low salary where

12 rows selected. SQL> edit Wrote file afiedt.buf 1* select ename,salary,decode(sign(salary - 4000),-1,'low salary',1,'high salary') from emp_company SQL> / ENAME SALARY DECODE(SIGN ---------- --------- ----------Anil 1500 low salary Shankar 2000 low salary Jaya 1800 low salary Sunil 1700 low salary Vijay 5100 high salary Prakash 3000 low salary Ajay 7700 high salary prakash 5000 high salary Nitin 6000 high salary Ashok 3000 low salary Vinod 3200 low salary GORGE 12 rows selected. SQL> select least(45,23) from dual; LEAST(45,23) -----------23 SQL> select greatest(23,45) from dual; GREATEST(23,45) --------------45

SQL> select rowid,ename from emp_company chartorowid('AAAUuAAAIAAALPwAAB'); ROWID ENAME ------------------ ---------AAAUuAAAIAAALPwAAC Ashok AAAUuAAAIAAALPwAAD Vinod AAAUuAAAIAAALPwAAE GORGE

where

rowid

>

SQL> select vsize(ename),ename,vsize(salary),salary from emp_company; VSIZE(ENAME) ENAME VSIZE(SALARY) ------------ ---------- ------------- --------10 Anil 2 1500 10 Shankar 2 2000 10 Jaya 2 1800 10 Sunil 2 1700 10 Vijay 2 5100 10 Prakash 2 3000 10 Ajay 2 7700 10 prakash 2 5000 10 Nitin 2 6000 10 Ashok 2 3000 10 Vinod 2 3200 10 GORGE 12 rows selected. SQL> select ename,dump(ename) from emp_company; ENAME ---------DUMP(ENAME) --------------------------------------------------------------------------------------------------Anil Typ=96 Len=10: 65,110,105,108,32,32,32,32,32,32 Shankar Typ=96 Len=10: 83,104,97,110,107,97,114,32,32,32 SALARY

J SQL> select user from dual; USER -----------------------------PSD Numeric Function SQL> SELECT TO_CHAR(567898,'99,999,99') FROM DUAL; TO_CHAR(56 ---------5,678,98 SQL> EDIT Wrote file afiedt.buf 1* SELECT TO_CHAR(567898,'99,999,99.99') FROM DUAL SQL> / TO_CHAR(56789 ------------5,678,98.00 SQL> EDIT Wrote file afiedt.buf 1* SELECT TO_CHAR(567898,'99G999G99D99') FROM DUAL SQL> / TO_CHAR(56789 ------------5,678,98.00 SQL> EDIT Wrote file afiedt.buf

1* SELECT TO_CHAR(567898,'99G999G99D99','NLS_NUMERIC_CHARACTERS='' *#''') FROM DUAL SQL> / TO_CHAR(56789 ------------5#678#98*00 SQL> SELECT TO_CHAR(-67895,'999999MI') FROM DUAL; TO_CHAR ------67895SQL> EDIT Wrote file afiedt.buf 1* SELECT TO_CHAR(-67895,'PR999999') FROM DUAL SQL> / SELECT TO_CHAR(-67895,'PR999999') FROM DUAL * ERROR at line 1: ORA-01481: invalid number format model SQL> EDIT Wrote file afiedt.buf 1* SELECT TO_CHAR(-67895,'999999PR') FROM DUAL SQL> / TO_CHAR( -------<67895> SQL> EDIT Wrote file afiedt.buf 1* SELECT TO_CHAR(67895,'C999999PR') FROM DUAL

SQL> / TO_CHAR(678 ----------USD67895 SQL> ALTER SESSION SET NLS_CURRENCY='RS.'; Session altered. SQL> SELECT TO_CHAR(67895,'C999999PR') FROM DUAL; TO_CHAR(678 ----------USD67895 SQL> SELECT TO_CHAR(67895,'C999999PR') FROM DUAL; TO_CHAR(678 ----------USD67895 SQL> EDIT Wrote file afiedt.buf 1* SELECT TO_CHAR(67895,'L999999PR') FROM DUAL SQL> / TO_CHAR(67895,'L99 -----------------RS.67895 SQL> EDIT Wrote file afiedt.buf 1* SELECT TO_CHAR(67895,'$999999PR') FROM DUAL SQL> / TO_CHAR(6

--------$67895 SQL> ALTER SESSION SET NLS_CURRENCY='RS.'; Session altered. SQL> SELECT TO_CHAR(657656,'EEEE') FROM DUAL; SELECT TO_CHAR(657656,'EEEE') FROM DUAL * ERROR at line 1: ORA-01481: invalid number format model SQL> EDIT Wrote file afiedt.buf 1* SELECT TO_CHAR(657656,'9.99EEEE') FROM DUAL SQL> / TO_CHAR(65 ---------6.58E+05 Aggregate Functions max,min,sum,avg,count,stdev SQL> select * from emp_company; ENAME CNAME SALARY JDATE ---------- ---------- --------- --------Anil Acc 1500 01-MAY-89 Shankar TATA 2000 10-JUL-90 Jaya CMC 1800 07-JUN-91 Sunil CMC 1700 01-JAN-88 Vijay TATA 5000 03-JAN-88 Prakash TATA 3000 27-MAY-89 Ajay ACC 8000 30-APR-95 Abhay ACC 1800 1-APR-98 7 rows selected.

list maximum salary SQL> select max(salary) from emp_company; MAX(SALARY) ----------8000 SQL> select ename,max(salary) from emp_company; select ename,max(salary) from emp_company * ERROR at line 1: ORA-00937: not a single-group group function SQL> select ename from emp_company where salary in (select max(salary) from emp_company); ENAME ---------Ajay list maximum of employees of ACC SQL> select max(salary) from emp_company where cname='ACC'; MAX(SALARY) ----------8000 SQL> select count(ename) from emp_company where cname='ACC'; COUNT(ENAME) -----------1 SQL> select avg(salary),sum(salary) from emp_company; AVG(SALARY) SUM(SALARY) ----------- -----------

3285.7143

23000

SQL> select max(salary) from emp_company where cname='l'; MAX(SALARY) ----------Returns null value SQL> edit Wrote file afiedt.buf 1* select count(salary) from emp_company where cname='l' SQL> / COUNT(SALARY) ------------0 SQL> edit Wrote file afiedt.buf 1* select nvl(max(salary),0) from emp_company where cname='l' SQL> / NVL(MAX(SALARY),0) -----------------0 SQL> spool off try these examples 1. List the number of employees living in Bombay 2. list the number of employees having shift A

3. list name of employees with their living city having maximum salary in company TATA 4. list name of employee having maximum salary in company where Sunil is working 5. list name of employees having maximum salary in their company. 1. select count(ename) from employee where city='Bombay'; 2. select count(*) from emp_shift where shift='A'; 3. select e.ename,e.city from employee e where e.ename in (select c.ename from emp_company c where c.cname='TATA' and c.salary in // ensures the employee is having in TATA (select max(c1.salary) from emp_company c1 where c1.cname='TATA')); 4. select e.ename from emp_company e where e.salary in (select max(e1.salary) // this will find out maximum salary in Sunil company from emp_company e1 where e1.cname in (select e2.cname // this will find out company of sunil from emp_company e2 where e2.ename='Sunil') ) and e.cname in // this will ensure that employee should be from Sunil's company (select e3.cname from emp_company e3 where e3.ename='Sunil'); ENAME CNAME SALARY JDATE ---------- ---------- --------- --------Anil ACC 1500 01-MAY-89 Shankar TATA 2000 10-JUL-90 Jaya CMC 1800 07-JUN-91 Sunil CMC 1700 01-JAN-88 Vijay TATA 5000 03-JAN-88 Prakash TATA 3000 27-MAY-89

Ajay ACC Abhay ACC 7 rows selected.

8000 30-APR-95 1800 1-APR-98

5. select e1.ename from emp_company e1 where e1.salary in 1500(Anil) /8000(Ajay) (select max(e2.salary) 8000 from emp_company e2 where e1.cname=e2.cname); Group By / Having Clause SQL> select cname,max(salary) from emp_company group by cname; // group by expression CNAME MAX(SALARY) ---------- ----------ACC 8000 Acc 1500 CMC 1800 TATA 5000 SQL> select cname,count(ename) from emp_company group by cname; CNAME COUNT(ENAME) ---------- -----------ACC 1 Acc 1 CMC 2 TATA 3 SQL> select cname,count(ename) from emp_company 2 group by cname 3 having count(ename) > 1; CNAME COUNT(ENAME) ---------- ------------

CMC TATA SQL> spool off

2 3

Try these examples 1. Find out avg. salary of each company 2. Find out avg. salary of each company except 'ACC' 3. Find out name of companies having average salary more than 1500 4. find out avg. salary of company only for employees living in delhi 5. find out name of company having highest average salary 1. select cname,avg(salary) from emp_company group by cname; 2. select cname,avg(salary) from emp_company where cname != 'ACC' group by cname ; 3. select cname,avg(salary) from emp_company group by cname having avg(salary) > 1500; 4. select cname,avg(e.salary) from emp_company e where e.ename in (select l.ename from employee l where l.city='Delhi') group by e.cname; 5. select e.cname,avg(e.salary) from emp_company e group by cname having avg(e.salary) > = all(select avg(c.salary) from emp_company c group by c.cname); DELETE STATEMENT 1. Delete all rows

delete emp_company; or -- truncate table emp_company; -- triggers are fired in the case of delete (not for truncate)

2. delete every employee of company ACC delete emp_company where cname='ACC'; 3. Delete every employee from emp_company living in Delhi delete emp_company where ename in (select e.ename from employee e where e.city = 'Delhi'); 4. Delete rows of emp_company having salary greater than 8000. delete emp_company where salary > 8000; 5. Delete rows of emp_company having salary greater than 8000 and employee having living city MADRAS and company located in city BOMBAY. delete emp_company where salary > 8000 and ename in (select ename from employee where city = MADRAS) and cname in (select cname from company where city = BOMBAY); 6. Write the above query using exists clause. delete emp_company where salary > 8000 and exists (select employee ename from employee where employee ename and employee.city = MADRAS) and exists (select company.cname from company

where emp_company.cname = company.cname and company. city = BOMBAY); Insert Statement SQL> create table emp_comp(ename char(10), cname char(10)); Table created. SQL> insert into emp_comp 2 select ename,cname from emp_company where cname='ACC'; 2 rows created. SQL> commit; Commit complete. SQL> create table emp_comp1 as select ename,cname from emp_company; Table created. SQL> select * from emp_comp1; ENAME CNAME ---------- ---------Anil Acc Shankar TATA Jaya CMC Sunil CMC Vijay TATA Prakash TATA Ajay Tata prakash Nitin ACC Ashok TATA Vinod ACC 11 rows selected.

SQL> alter table emp_comp1 add (salary number(10,2)); Table altered. SQL> alter table emp_comp1 modify(cname varchar2(10)); Table altered. SQL> alter table emp_comp1 modify(cname char(12)); Table altered. 1* alter table emp_comp1 drop(salary) SQL> / Table altered. SQL> desc emp_comp1; Name Null? Type ------------------------------- -------- ---ENAME CHAR(10) CNAME CHAR(12) SQL> edit Wrote file afiedt.buf 1* alter table emp_comp1 drop(cname) SQL> / Table altered.

SQL> rename emp_comp1 to emp_comp2 SQL> / Table renamed.

SQL> desc emp_comp2; Name Null? Type ------------------------------- -------- ---ENAME CHAR(10) SQL> create synonym emp_comp1 for emp_comp2; Synonym created. SQL> select * from emp_comp1; ENAME ---------Anil Shankar Jaya Sunil Vijay Prakash Ajay prakash Nitin Ashok Vinod 11 rows selected. SQL> delete emp_comp1; 11 rows deleted. SQL> rollback; Rollback complete. no ddl operations SQL> drop table emp_comp2;

Table dropped. SQL> drop synonym emp_comp1; Synonym dropped. Update 1. update salary of every employee by giving 10%raise update emp_company set salary = 1.1 * salary; 2. update salary of every employee by giving 10%raise for company ACC update emp_company set salary = 1.1 * salary where cname='ACC'; 3. update salary of every employee by giving 10%raise for employees living in Delhi update emp_company set salary = 1.1 * salary where ename in (select e.ename from employee e where e.city = 'Delhi'); 4. update salary of Ajay by adding 10%salary of Vijay update emp_company e1 set e1.salary = (select (e1.salary + 0.1 * e2.salary) from emp_company e2 where e2.ename='Vijay' ) where e1.ename = 'Ajay'; // the select in set should produce single row // for no row null is returned 5. Add 100 Rs to salary of Ajay and give him company TATA

update emp_company set salary=salary+100,cname='TATA' where ename='Ajay'; 6. transfer 100 Rs from salary of Ajay to Vijay update emp_company set salary = salary - 100 where ename='Ajay'; update emp_company set salary = salary + 100 where ename='Vijay'; Atomicity of Tranasaction -- Transaction is defined as set of DML statements between two successive commit -- one transaction is equivalent to one real life unit of work -- one real life unit of work may be implemented as multiple DML statements -- The concept of Transaction is to group such DML statement -- The effect of transaction should be full or null.Such transactions are called as atomic -- transactions. Transfer 100 Rs. from account of Ajay to account of Vijay Ajay 1000 900 900 Vijay 1000 initial 1000 partial 1100 full

commit; --1 update bank set amt = amt - 100 where ename='Ajay'; --2 update bank set amt = amt + 100 where ename='Vijay';

--3 commit; --4 commit; update sales; update inv_balance; commit; 7. transfer 100 Rs from salary of Ajay to Vijay if both are having same company update emp_company e1 set e1.salary = e1.salary - 100 where e1.ename='Ajay' and exists(select 'X' from emp_company e2 where e2.ename='Vijay' and e1.cname=e2.cname); update emp_company e1 set e1.salary = e1.salary + 100 where e1.ename='Vijay' and exists(select 'X' from emp_company e2 where e2.ename='Ajay' and e1.cname=e2.cname); 8. Decrease the salary of VIJAY by 100 if VIJAY and SUNIL both are living in city MADRAS SQL>update emp_company set salary = salary 100 where ename = VIJAY and ename in(select e1.ename from employee e1 where e1.city = MADRAS) and exists (select emp1.ename from employee emp1 where emp1.ename = SUNIL and emp1.city = MADRAS); 9. All employee of ACC having salary greater than 8000 are shifted to TATA. The living city of employee is BOMBAY. SQL>update emp_company set cname = TATA

where cname = ACC and salary > 8000 and ename in (select ename from employee where city = BOMBAY); 10. All employee of ACC having salary greater than 8000 are shifted to TATA. The living city of employee is BOMBAY. The salary in new company is 20% more than original salary; SQL>update emp_company set cname = TATA,salary = 1.2 * salary where cname = ACC and salary > 8000 and ename in (select ename from employee where city = BOMBAY); 11. Decrease salary of employee VIJAY by 100 and increase salary of employee SUNIL by 100. The two update statements will be SQL>update emp_company set salary = salary 100 where cname = VIJAY; SQL>update emp_company set salary = salary + 100 where cname = SUNIL; 12. Decrease salary of VIJAY by 100 and increase salary of SUNIL if both are having company ACC. The two update statements can be written as SQL>update emp_company set salary = salary 100 where ename = VIJAY and cname = ACC and exists (select e1.ename from emp_company e1 where

e1.ename = SUNIL and e1.cname = ACC); SQL>update emp_company set salary = salary + 100 where ename = SUNIL and cname = ACC and exists (select e1.ename from emp_company e1 where e1.ename = VIJAYand e1.cname = ACC); 13. Update salary of VIJAY, give him salary that is sum of his salary and SUNILs salary. SQL>update emp_company Set salary = (select emp_company.salary + ec1.salary) From emp_company ec1 where ec1.ename = 'SUNIL'); where ename = VIJAY; 14. Update salary and company of VIJAY, give him the same salary as that of SUNIL and the same company as that of ANIL. SQL>update emp_company set salary = (select ec1 salary from emp_company ec1 where ec1.ename = SUNIL), cname = (select ec2.ename from emp_company ec2 where ec2.ename = ANIL) where ename = VIJAY; 15. Give the average salary of company ACC to employee VIJAY. SQL>update emp_company set salary =(select avg(salary) from emp_company where cname = ACC) where ename = VIJAY; 16. update salary of employee VIJAY, give him max salary of company of SUNIL. SQL>update emp_company ec1

set salary = (select max(salary) from emp_company ec1 where ec1.cname in (select ec2.cname from emp_company ec2 where ec2.ename = SUNIL)) where ename = VIJAY;