SQL Oracle
SQL Oracle
SQL Oracle
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’);
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’;
/=================================================================================
SQL\Ch02
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’
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')));
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’);
/
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
/
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
SQL\ch03
ENAME SALARY
---------- ---------
Anil 1500
Shankar 2000
Jaya 1800
Sunil 1700
Vijay 5000
Prakash 3000
Ajay 8000
ENAME
----------
Ajay
ENAME
----------
Shankar
Jaya
Sunil
Vijay
Prakash
Ajay
6 rows selected.
7 rows selected.
/
DISTINCT OPERATOR
CNAME
----------
Acc
CMC
TATA
SQL> select cname from emp_company;
CNAME
----------
Acc
TATA
CMC
CMC
TATA
TATA
ACC
7 rows selected.
COUNT(CNAME)
------------
7
COUNT(DISTINCTCNAME)
--------------------
4
SQL> commit;
Commit complete.
COUNT(CNAME)
------------
7
COUNT(DISTINCTCNAME)
--------------------
4
SQL> edit
Wrote file afiedt.buf
ENAME
----------
Ajay
Anil
Jaya
Prakash
Shankar
Sunil
Vijay
prakash
8 rows selected.
SQL> edit
Wrote file afiedt.buf
ENAME
----------
Anil
Shankar
Jaya
Sunil
Vijay
Prakash
Ajay
prakash
Anil
Sunil
Vijay
Prakash
12 rows selected.
1 row created.
ENAME
----------
Ajay
Jaya
Shankar
prakash
ENAME
----------
Ajay
Anil
Jaya
Prakash
Shankar
Sunil
Vijay
prakash
Anil
Shankar
Jaya
Sunil
Vijay
Prakash
Ajay
prakash
prakash
prakash
18 rows selected.
SQL> select * from employee where ename like 'A%'; --% means zero or more characters
ENAME CITY
---------- ----------
Anil Nagpur
Ajay Madras
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
8 rows selected.
no rows selected
no rows selected
no rows selected
5*4
---------
20
5*4+NULL
---------
ENAME NVL(CNAME,
---------- ----------
Anil Acc
Shankar TATA
Jaya CMC
Sunil CMC
Vijay TATA
Prakash TATA
Ajay ACC
prakash No Company
8 rows selected.
MAX(CNAME)
----------
TATA
SQL> select count(cname) from emp_company;
ASCII('ABC')
------------
65
C
-
A
INITC
-----
Abcde
UPPER
-----
ABCFG
LOWER(
------
sfdsfg
LENGTH('ABCDF')
---------------
5
LENGTHB('ABCDF')
----------------
5
LENGTHB(34)
-----------
2
SQL> select lengthb(1000) from dual;
LENGTHB(1000)
-------------
4
LENGTHB(516)
------------
3
LENGTHB(SYSDATE)
----------------
9
SU
--
cd
SQL> edit
Wrote file afiedt.buf
SUBS
----
cdef
SQL> edit
Wrote file afiedt.buf
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
LPAD('AB
--------
abc
SQL> edit
Wrote file afiedt.buf
LENGTH(LPAD('ABC',8))
---------------------
8
SQL> edit
Wrote file afiedt.buf
LENGTH(LPAD('ABC',2))
---------------------
2
SQL> edit
Wrote file afiedt.buf
LP
--
ab
SQL> edit
----left padding using 'as' till the length 8 ie reached
SQL> edit
Wrote file afiedt.buf
----right padding using 'as' till the length 8 ie reached
RPAD('AB
--------
abcasasa
SQL> edit
Wrote file afiedt.buf
--right trimming of blanks
RTR
---
abc
SQL> edit
Wrote file afiedt.buf
LENGTH(RTRIM('ABC'))
--------------------
3
SQL> edit
SQL> edit
Wrote file afiedt.buf
--right trimming of string i.e removing char 'c' from right end
LENGTH(RTRIM('ABC','C'))
------------------------
5
SQL> edit
Wrote file afiedt.buf
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
(
-
a
no rows selected
SQL> edit
Wrote file afiedt.buf
'
-
X
SYSDATE
---------
03-JAN-02
TO
--
01
SQL> edit
Wrote file afiedt.buf
TO_CHA
------
one
SQL> edit
Wrote file afiedt.buf
1* select to_char(sysdate,'mmspth') from dual
SQL> /
TO_CHAR(
--------
first
TO_CHAR(SYSDATE,'YYYYSPTH')
--------------------------------------------
two thousand second
SQL> edit
Wrote file afiedt.buf
TO_CHAR(SYSDATE,'YEAR')
------------------------------------------
two thousand two
TO_CHAR(S
---------
03-jan-02
NEW_TIME(
---------
03-JAN-02
SQL> EDIT
Wrote file afiedt.buf
TO_CHAR(NEW_TIME(SYS
--------------------
03-JAN-2002 12:21:08
TO_C
----
08PM
T
-
5
TO
--
03
TO_
---
003
TO_C
----
0035
T
-
1
TO
--
01
SQL> EDIT
Wrote file afiedt.buf
TO
--
01
TO_CHAR
-------
2452278
SQL>
SQL>
SQL>
SQL> EDIT
Wrote file afiedt.buf
TO_CH
-----
2002
TO_CHAR(SYSD
------------
03-I -2002
TRUNC(SYS
---------
01-JAN-02
SQL> EDIT
Wrote file afiedt.buf
TRUNC(SYS
---------
01-DEC-01
SQL> EDIT
Wrote file afiedt.buf
TRUNC(SYS
---------
01-JAN-01
SQL> EDIT
Wrote file afiedt.buf
TRUNC(SYS
---------
03-JAN-02
SQL> EDIT
Wrote file afiedt.buf
TO_CHAR(
--------
08:00:00
SQL> EDIT
Wrote file afiedt.buf
ROUND(SYS
---------
01-JAN-02
SQL> EDIT
Wrote file afiedt.buf
ROUND(SYS
---------
01-FEB-02
SQL> EDIT
Wrote file afiedt.buf
ROUND(SYS
---------
01-JAN-02
'
-
5
ROUND(123.556)
--------------
124
ROUND(123.456,1)
----------------
123.5
SQL> edit
Wrote file afiedt.buf
ROUND(123.456,2)
----------------
123.46
SQL> edit
Wrote file afiedt.buf
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
ROUND(123.456,-2)
-----------------
100
SQL> edit
Wrote file afiedt.buf
ROUND(123.456,-3)
-----------------
0
SQL> edit
Wrote file afiedt.buf
ROUND(173.456,-2)
-----------------
200
SQL> edit
Wrote file afiedt.buf
TRUNC(173.456,-2)
-----------------
100
SQL> edit
Wrote file afiedt.buf
TRUNC(173.456,-1)
-----------------
170
SQL> edit
Wrote file afiedt.buf
TRUNC(173.456,1)
----------------
173.4
SQL> edit
Wrote file afiedt.buf
TRUNC(173.456,2)
----------------
173.45
6 rows selected.
ABS(-9)
---------
9
SQL> select ceil(-9.3) from dual;
CEIL(-9.3)
----------
-9
CEIL(9.3)
---------
10
FLOOR(-9.3)
-----------
-10
FLOOR(9.3)
----------
9
SIGN(-5)
---------
-1
SIGN(5)
---------
1
SIGN(0)
---------
0
POWER(3,2)
----------
9
MOD(7,3)
---------
1
SQRT(9)
---------
3
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
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
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
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.
no rows selected
SQL>
SQL>
SQL>
SQL>
SQL>
SQL> SELECT TRANSLATE('ABBCD','AB','*+') FROM DUAL;
TRANS
-----
*++CD
SQL> EDIT
Wrote file afiedt.buf
TRA
---
*CD
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;
12 rows selected.
Rowid specifies position of row on disk.Rowid is unique for each row.It is constant throughout lifetime of that
row.
DECODE(SIGN
-----------
low salary
low salary
low salary
low salary
high salary
low salary
high salary
high salary
high salary
low salary
low salary
12 rows selected.
SQL> edit
Wrote file afiedt.buf
12 rows selected.
LEAST(45,23)
------------
23
GREATEST(23,45)
---------------
45
ROWID ENAME
------------------ ----------
AAAUuAAAIAAALPwAAC Ashok
AAAUuAAAIAAALPwAAD Vinod
AAAUuAAAIAAALPwAAE GORGE
12 rows selected.
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
USER
------------------------------
PSD
TO_CHAR(56
----------
5,678,98
SQL> EDIT
Wrote file afiedt.buf
TO_CHAR(56789
-------------
5,678,98.00
SQL> EDIT
Wrote file afiedt.buf
TO_CHAR(56789
-------------
5,678,98.00
SQL> EDIT
Wrote file afiedt.buf
TO_CHAR(56789
-------------
5#678#98*00
TO_CHAR
-------
67895-
SQL> 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
TO_CHAR(
--------
<67895>
SQL> EDIT
Wrote file afiedt.buf
TO_CHAR(678
-----------
USD67895
Session altered.
TO_CHAR(678
-----------
USD67895
TO_CHAR(678
-----------
USD67895
SQL> EDIT
Wrote file afiedt.buf
TO_CHAR(67895,'L99
------------------
RS.67895
SQL> EDIT
Wrote file afiedt.buf
TO_CHAR(6
---------
$67895
Session altered.
SQL> EDIT
Wrote file afiedt.buf
TO_CHAR(65
----------
6.58E+05
SQL\Ch04
Aggregate Functions
max,min,sum,avg,count,stdev
MAX(SALARY)
-----------
8000
ENAME
----------
Ajay
MAX(SALARY)
-----------
8000
COUNT(ENAME)
------------
1
AVG(SALARY) SUM(SALARY)
----------- -----------
3285.7143 23000
MAX(SALARY)
-----------
SQL> edit
Wrote file afiedt.buf
COUNT(SALARY)
-------------
0
SQL> edit
Wrote file afiedt.buf
NVL(MAX(SALARY),0)
------------------
0
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
CNAME MAX(SALARY)
---------- -----------
ACC 8000
Acc 1500
CMC 1800
TATA 5000
CNAME COUNT(ENAME)
---------- ------------
CMC 2
TATA 3
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);
/=================================================================================
SQL\Ch05
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';
delete emp_company
where ename in (select e.ename
from employee e
where e.city = 'Delhi');
5. Delete rows of emp_company having salary greater than 8000 and employee having living city
‘MADRAS’ and company located in city ‘BOMBAY’.
Table created.
2 rows created.
SQL> commit;
Commit complete.
Table created.
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.
Table altered.
Table altered.
Table altered.
Table altered.
SQL> edit
Wrote file afiedt.buf
Table altered.
Table renamed.
Synonym created.
11 rows selected.
11 rows deleted.
SQL> rollback;
Rollback complete.
no ddl operations
Table dropped.
Synonym dropped.
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
update emp_company
set salary=salary+100,cname='TATA'
where ename='Ajay';
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 Vijay
1000 1000 initial
900 1000 partial
900 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;
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’);
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.
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’.
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 =
’VIJAY’and
e1.cname = ’ACC’);
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’;
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\CH06
select max(d.avg_salary)
from
( select cname,avg(salary) avg_salary
from emp_company
group by cname ) d
/
/* create view comp_avg_salary as
select cname,avg(salary) avg_salary
from emp_company
group by cname
main query :
select max(avg_salary)
from comp_avg_salary */
ENAME MNAME
---------- ----------
Vijay
Shankar Vijay
Prakash Shankar
ENAME SALARY
---------- ---------
Prakash 3000
Shankar 2000
Vijay 5000
/
create view s as
(select m.ename ename, m.mname mname,e.salary sal
from manager m,emp_company e
where e.ename=m.ename )
/
select s.level,sum(s.sal)
from s
connect by prior s.ename=s.mname
start with s.ename='Vijay'
group by level
/
create view parent_child
as
select p.a pa,p.b pb,c.a ca,c.c cc
from parent p,child c
where p.a=c.a
/
Views
- To give simplified representation of data
7 rows selected.
SAL
---------
2000
SQL>
1 row created.
1 row created.
1 row created.
Force View
Table created.
no rows selected
main query :
select max(avg_salary)
from comp_avg_salary
main query :
select e.ename
from emp_city_details
where e.living_city=e.company_city
3.
4. create view comp_employees as
select cname,count(ename) no_of_employees
from emp_company
group by cname
main query :
select c.cname
from comp_employees c
where c.no_of_employees in
(select max(no_of_employees)
from comp_employees)
main query :
select e.name
from emp_mgr_city e
where e.living_city=e.manager_city
/
list salary of employees of Vijay
ENAME MNAME
---------- ----------
Vijay
Shankar Vijay
Prakash Shankar
ENAME SALARY
---------- ---------
Prakash 3000
Shankar 2000
Vijay 5000
/=================================================================================
SQL\Ch07
SQL\Ch07\ASSIGN1
CREATE TABLE DEPOSIT (ACTNO VARCHAR2(5) ,CNAME VARCHAR2(18) , BNAME VARCHAR2(18) ,
AMOUNT NUMBER(8,2) ,ADATE DATE);
DESC BRANCH;
DESC CUSTOMERS;
DESC BORROW;
/
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');
SQL\Ch07\Assign3
--1)GIVE NAME OF CUSTOMERS HAVING LIVING CITY BOMBAY AND BRANCH CITY NAGPUR.
SELECT D1.CNAME,D1.BNAME,C1.CNAME,C1.CITY,B1.CITY,B1.BNAME FROM DEPOSIT
D1,CUSTOMERS C1,BRANCH B1 WHERE C1.CITY = 'BOMBAY' AND B1.CITY = 'NAGPUR' AND
D1.CNAME = C1.CNAME AND D1.BNAME = B1.BNAME;
/
--2)GIVE NAME OF CUSTOMERS HAVING SAME LIVING CITY AS THEIR BRANCH CITY.
SELECT D1.CNAME,D1.BNAME,C1.CNAME,C1.CITY,
B1.BNAME,B1.CITY FROM DEPOSIT D1,CUSTOMERS C1,BRANCH B1 WHERE C1.CITY =B1.CITY
AND D1.CNAME = C1.CNAME AND D1.BNAME = B1.BNAME;
/
--3)GIVE NAME OF CUSTOMER WHO ARE BORROWERS AND DEPOSITORS AND HAVING LIVING
CITY NAGPUR.
SELECT D1.CNAME C1.CNAME, C1.CITY, BR1.CNAME FROM CUSTOMERS C1, DEPOSIT
D1,BORROW BR1 WHERE C1.CITY ='NAGPUR' AND C1.CNAME = D1.CNAME AND D1.CNAME =
BR1.CNAME ;
/
--4) GIVE NAME OF CUSTOMERS WHO ARE DEPOSITORS HAVING SAME BRANCH CITY OF MR.
SUNIL.
SELECT D1.BNAME, B1.BNAME FROM DEPOSIT D1,BRANCH B1 WHERE D1.BNAME AND
B2.CITY IN (SELECT D2.CNAME, D2.BNAME FROM DEPOSIT D2,BRANCH B2 WHERE D2.CNAME =
'SUNIL' AND D2.BNAME = B1.BNAME);
/
--5) GIVE NAME OF DEPOSITORS HAVING SAME LIVING CITY AS MR. ANIL AND HAVING DEPOSIT
AMOUNT GREATER THAN 2000.
SELECT D1.CNAME,D1.AMOUNT,C1.CNAME, C1.CITY FROM DEPOSIT D1,CUSTOMERS C1
WHERE D1.AMOUNT > 2000 AND D1.CNAME = C1.CNAME AND C1.CITY IN (SELECT C2.CITY FROM
CUSTOMERS C2 WHERE C2.CNAME = 'ANIL');
/
--6) GIVE NAME OF BORROWERS HAVING DEPOSIT AMOUNT GREATER THAN 1000 AND LOAN
AMOUNT GREATER THAN 2000.
SELECT BR1.CNAME, BR1.AMOUNT, D1.CNAME, D1.AMOUNT FROM BORROW BR1,DEPOSIT
D1 WHERE D1.CNAME = BR1.CNAME AND D1.AMOUNT > 1000 AND BR1.AMOUNT > 2000;
/
--7)GIVE NAME OF DEPOSITORS HAVING SAME BRANCH AS BRANCH OF SUNIL.
SELECT D1.BNAME FROM DEPOSIT D1 WHERE D1.BNAME IN (SELECT D2.CNAME FROM
DEPOSIT D2 WHERE D2.CNAME = 'SUNIL');
/
--8)GIVE NAME OF BORROWERS HAVING LOAN AMOUNT GREATER THAN AMOUNT OF PRAMOD.
SELECT BR1.CNAME,BR1.AMOUNT FROM BORROW BR1 WHERE BR1.AMOUNT > ALL
(SELECT BR2.AMOUNT FROM BORROW BR2 WHERE BR2.CNAME = 'PRAMOD');
/
--9)GIVE NAME OF CUSTOMERS LIVING IN SAME CITY WHERE BRANCH OF DEPOSITOR SUNIL IS
LOCATED.
SELECT C1.CITY FROM CUSTOMERS C1 WHERE C1.CITY IN (SELECT
D1.BNAME,D1.CNAME,B1.BNAME FROM BRANCH B1 ,DEPOSIT D1 WHERE D1.CNAME = 'SUNIL' AND
D1.BNAME = B1.BNAME);
/
--10)GIVE LOANNO,LOAN AMOUNT OF BORROWERS HAVING SAME BRANCH WHERE SUNIL IS
HAVING BRANCH.
SELECT BR1.LOANNO,BR1.AMOUNT FROM BORROW BR1 WHERE BR1.BNAME (SELECT
D1.BNAME FROM DEPOSIT D1 WHERE D1.CNAME = 'SUNIL');
/
--11)GIVE LOANNO,LOAN AMOUNT,ACCOUNT NO,DEPOSIT AMOUNT OF CUSTOMERS LIVING IN
CITY NAGPUR.
SELECT BR1.LOANNO , BR1.AMOUNT , D1.ACTNO , D1.AMOUNT FROM DEPOSIT
D1,BORROW BR1,
CUSTOMERS C1 WHERE C1.CNAME = D1.CNAME AND D1.CNAME = BR1.CNAME AND C1.CITY =
'NAGPUR'
/
--12)GIVE LOANNO,LOAN AMOUNT, ACCOUNT NO, DEPOSIT AMOUNT OF CUSTOMERS HAVING
BRANCH LOCATED AT BOMBAY.
SELECT BR1.LOANNO , BR1.AMOUNT , D1.ACTNO , D1.AMOUNT FROM DEPOSIT
D1,BORROW BR1,BRANCH B1 WHERE D1.CNAME = BR1.CNAME AND B1.CITY = 'BOMBAY' AND
D1.BNAME = B1.BNAME;
/
--13)GIVE LOANNO,LOAN AMOUNT,ACCOUNT NO,DEPOSIT AMOUNT,BRANCH NAME, BRANCH CITY
AND LIVING CITY OF PRAMOD.
SELECT BR1.LOANNO , BR1.AMOUNT , D1.ACTNO , D1.AMOUNT , D1.BNAME
,B1.CTTY,C1.CITY FROM BORROW BR1,DEPOSIT D1 , BRANCH B1, CUSTOMERS C1 WHERE
C1.CNAME = 'PRAMOD' AND D1.CNAME = C1.CNAME AND D1.CNAME=BR1.CNAME AND D1.BNAME
= B1.BNAME;
/
--14)GIVE DEPOSIT DETAILS AND LOAN DETAILS OF CUSTOMER IN SAME CITY WHERE PRAMOD IS
LIVING.
SELECT D1.ACTNO , D1.CNAME,D1.BNAME ,D1.AMOUNT ,D1.ADATE , BR1.LOANNO ,
BR1.BNAME , BR1.AMOUNT FROM DEPOSIT D1 , BORROW BR1 , CUSTOMERS C1 WHERE
C1.CNAME = D1.CNAME AND D1.CNAME = BR1.CNAME AND C1.CITY IN (SELECT C2.CITY FROM
CUSTOMERS C2 WHERE C2.CNAME = 'PRAMOD');
/
--15)GIVE NAME OF DEPOSITORS HAVING SAME BRANCH CITY AS MR. SUNIL AND HAVING SAME
LIVING CITY AS MR. ANIL.
SELECT D1.CNAME FROM DEPOSIT D1,CUSTOMERS C1,BRANCH B1 WHERE D1.CNAME =
C1.CNAME AND D1.BNAME = B1.BNAME AND B1.CITY IN (SELECT B2.CITY FROM BRANCH
B2,DEPOSIT D2 WHERE D2.CNAME = 'SUNIL'AND D2.BNAME = B2.BNAME) AND C1.CITY IN (SELECT
C2.CITY FROM CUSTOMERS C2 WHERE C2.CNAME = 'ANIL');
/
--16) GIVE NAME OF DEPOSITORS HAVING AMOUNT GREATER THAN 5000 AND HAVING SAME
LIVING CITY AS MR. PRAMOD.
SELECT D1.CNAME FROM DEPOSIT D1,CUSTOMERS C1 , CUSTOMERS C2 WHERE C1.CITY
= C2.CITY AND C2.CNAME = 'PRAMOD' AND C1.CNAME = D1.CNAME AND D1.AMOUNT > 5000;
/
--17)GIVE CITY OF CUSTOMER HAVING BRANCH CITY SAME AS MR. PRAMOD.
SELECT C1.CITY FROM CUSTOMERS C1,DEPOSIT D1,DEPOSIT D2,BRANCH B1,BRANCH B2
WHERE D1.BNAME=B1.BNAME AND D2.BNAME = B2.BNAME AND D2.CNAME = 'PRAMOD' AND
B1.CITY = B2.CITY AND C1.CNAME = D1.CNAME;
/
--18)GIVE BRANCH CITY AND LIVING CITY OF PRAMOD.
SELECT B1.CITY , C1.CITY FROM BRANCH B1,CUSTOMERS C1, DEPOSIT D1 WHERE
C1.CNAME = 'PRAMOD' AND C1.CNAME = D1.CNAME AND D1.BNAME = B1.BNAME;
/
--19)GIVE BRANCH CITY OF SUNIL AND BRANCH CITY OF ANIL.
SELECT B1.CITY FROM DEPOSIT D1,BRANCH B1 WHERE D1.BNAME = B1.BNAME AND
D1.CNAME = 'SUNIL' OR D1.CNAME = 'ANIL';
/
--20) GIVE LIVING CITY OF ASHOK AND LIVING CITY OF AJAY.
SELECT CNAME,CITY FROM CUSTOMERS WHERE CUSTOMERS.CNAME = 'ASHOK' OR
CUSTOMERS.CNAME = 'AJAY';
/=================================================================================
SQL\Ch07\Assign4
SQL\Ch07\ASSIGN1
DESC BRANCH;
DESC CUSTOMERS;
DESC BORROW;
/
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');
SQL\Ch07\ASSIGN2
SQL\Ch07\Assign3
--1)GIVE NAME OF CUSTOMERS HAVING LIVING CITY BOMBAY AND BRANCH CITY NAGPUR.
SELECT D1.CNAME,D1.BNAME,C1.CNAME,C1.CITY,B1.CITY,B1.BNAME FROM DEPOSIT
D1,CUSTOMERS C1,BRANCH B1 WHERE C1.CITY = 'BOMBAY' AND B1.CITY = 'NAGPUR' AND
D1.CNAME = C1.CNAME AND D1.BNAME = B1.BNAME;
/
--2)GIVE NAME OF CUSTOMERS HAVING SAME LIVING CITY AS THEIR BRANCH CITY.
SELECT D1.CNAME,D1.BNAME,C1.CNAME,C1.CITY,
B1.BNAME,B1.CITY FROM DEPOSIT D1,CUSTOMERS C1,BRANCH B1 WHERE C1.CITY =B1.CITY
AND D1.CNAME = C1.CNAME AND D1.BNAME = B1.BNAME;
/
--3)GIVE NAME OF CUSTOMER WHO ARE BORROWERS AND DEPOSITORS AND HAVING LIVING
CITY NAGPUR.
SELECT D1.CNAME C1.CNAME, C1.CITY, BR1.CNAME FROM CUSTOMERS C1, DEPOSIT
D1,BORROW BR1 WHERE C1.CITY ='NAGPUR' AND C1.CNAME = D1.CNAME AND D1.CNAME =
BR1.CNAME ;
/
--4) GIVE NAME OF CUSTOMERS WHO ARE DEPOSITORS HAVING SAME BRANCH CITY OF MR.
SUNIL.
SELECT D1.BNAME, B1.BNAME FROM DEPOSIT D1,BRANCH B1 WHERE D1.BNAME AND
B2.CITY IN (SELECT D2.CNAME, D2.BNAME FROM DEPOSIT D2,BRANCH B2 WHERE D2.CNAME =
'SUNIL' AND D2.BNAME = B1.BNAME);
/
--5) GIVE NAME OF DEPOSITORS HAVING SAME LIVING CITY AS MR. ANIL AND HAVING DEPOSIT
AMOUNT GREATER THAN 2000.
SELECT D1.CNAME,D1.AMOUNT,C1.CNAME, C1.CITY FROM DEPOSIT D1,CUSTOMERS C1
WHERE D1.AMOUNT > 2000 AND D1.CNAME = C1.CNAME AND C1.CITY IN (SELECT C2.CITY FROM
CUSTOMERS C2 WHERE C2.CNAME = 'ANIL');
/
--6) GIVE NAME OF BORROWERS HAVING DEPOSIT AMOUNT GREATER THAN 1000 AND LOAN
AMOUNT GREATER THAN 2000.
SELECT BR1.CNAME, BR1.AMOUNT, D1.CNAME, D1.AMOUNT FROM BORROW BR1,DEPOSIT
D1 WHERE D1.CNAME = BR1.CNAME AND D1.AMOUNT > 1000 AND BR1.AMOUNT > 2000;
/
--7)GIVE NAME OF DEPOSITORS HAVING SAME BRANCH AS BRANCH OF SUNIL.
SELECT D1.BNAME FROM DEPOSIT D1 WHERE D1.BNAME IN (SELECT D2.CNAME FROM
DEPOSIT D2 WHERE D2.CNAME = 'SUNIL');
/
--8)GIVE NAME OF BORROWERS HAVING LOAN AMOUNT GREATER THAN AMOUNT OF PRAMOD.
SELECT BR1.CNAME,BR1.AMOUNT FROM BORROW BR1 WHERE BR1.AMOUNT > ALL
(SELECT BR2.AMOUNT FROM BORROW BR2 WHERE BR2.CNAME = 'PRAMOD');
/
--9)GIVE NAME OF CUSTOMERS LIVING IN SAME CITY WHERE BRANCH OF DEPOSITOR SUNIL IS
LOCATED.
SELECT C1.CITY FROM CUSTOMERS C1 WHERE C1.CITY IN (SELECT
D1.BNAME,D1.CNAME,B1.BNAME FROM BRANCH B1 ,DEPOSIT D1 WHERE D1.CNAME = 'SUNIL' AND
D1.BNAME = B1.BNAME);
/
--10)GIVE LOANNO,LOAN AMOUNT OF BORROWERS HAVING SAME BRANCH WHERE SUNIL IS
HAVING BRANCH.
SELECT BR1.LOANNO,BR1.AMOUNT FROM BORROW BR1 WHERE BR1.BNAME (SELECT
D1.BNAME FROM DEPOSIT D1 WHERE D1.CNAME = 'SUNIL');
/
--11)GIVE LOANNO,LOAN AMOUNT,ACCOUNT NO,DEPOSIT AMOUNT OF CUSTOMERS LIVING IN
CITY NAGPUR.
SELECT BR1.LOANNO , BR1.AMOUNT , D1.ACTNO , D1.AMOUNT FROM DEPOSIT
D1,BORROW BR1,
CUSTOMERS C1 WHERE C1.CNAME = D1.CNAME AND D1.CNAME = BR1.CNAME AND C1.CITY =
'NAGPUR'
/
--12)GIVE LOANNO,LOAN AMOUNT, ACCOUNT NO, DEPOSIT AMOUNT OF CUSTOMERS HAVING
BRANCH LOCATED AT BOMBAY.
SELECT BR1.LOANNO , BR1.AMOUNT , D1.ACTNO , D1.AMOUNT FROM DEPOSIT
D1,BORROW BR1,BRANCH B1 WHERE D1.CNAME = BR1.CNAME AND B1.CITY = 'BOMBAY' AND
D1.BNAME = B1.BNAME;
/
--13)GIVE LOANNO,LOAN AMOUNT,ACCOUNT NO,DEPOSIT AMOUNT,BRANCH NAME, BRANCH CITY
AND LIVING CITY OF PRAMOD.
SELECT BR1.LOANNO , BR1.AMOUNT , D1.ACTNO , D1.AMOUNT , D1.BNAME
,B1.CTTY,C1.CITY FROM BORROW BR1,DEPOSIT D1 , BRANCH B1, CUSTOMERS C1 WHERE
C1.CNAME = 'PRAMOD' AND D1.CNAME = C1.CNAME AND D1.CNAME=BR1.CNAME AND D1.BNAME
= B1.BNAME;
/
--14)GIVE DEPOSIT DETAILS AND LOAN DETAILS OF CUSTOMER IN SAME CITY WHERE PRAMOD IS
LIVING.
SELECT D1.ACTNO , D1.CNAME,D1.BNAME ,D1.AMOUNT ,D1.ADATE , BR1.LOANNO ,
BR1.BNAME , BR1.AMOUNT FROM DEPOSIT D1 , BORROW BR1 , CUSTOMERS C1 WHERE
C1.CNAME = D1.CNAME AND D1.CNAME = BR1.CNAME AND C1.CITY IN (SELECT C2.CITY FROM
CUSTOMERS C2 WHERE C2.CNAME = 'PRAMOD');
/
--15)GIVE NAME OF DEPOSITORS HAVING SAME BRANCH CITY AS MR. SUNIL AND HAVING SAME
LIVING CITY AS MR. ANIL.
SELECT D1.CNAME FROM DEPOSIT D1,CUSTOMERS C1,BRANCH B1 WHERE D1.CNAME =
C1.CNAME AND D1.BNAME = B1.BNAME AND B1.CITY IN (SELECT B2.CITY FROM BRANCH
B2,DEPOSIT D2 WHERE D2.CNAME = 'SUNIL'AND D2.BNAME = B2.BNAME) AND C1.CITY IN (SELECT
C2.CITY FROM CUSTOMERS C2 WHERE C2.CNAME = 'ANIL');
/
--16) GIVE NAME OF DEPOSITORS HAVING AMOUNT GREATER THAN 5000 AND HAVING SAME
LIVING CITY AS MR. PRAMOD.
SELECT D1.CNAME FROM DEPOSIT D1,CUSTOMERS C1 , CUSTOMERS C2 WHERE C1.CITY
= C2.CITY AND C2.CNAME = 'PRAMOD' AND C1.CNAME = D1.CNAME AND D1.AMOUNT > 5000;
/
--17)GIVE CITY OF CUSTOMER HAVING BRANCH CITY SAME AS MR. PRAMOD.
SELECT C1.CITY FROM CUSTOMERS C1,DEPOSIT D1,DEPOSIT D2,BRANCH B1,BRANCH B2
WHERE D1.BNAME=B1.BNAME AND D2.BNAME = B2.BNAME AND D2.CNAME = 'PRAMOD' AND
B1.CITY = B2.CITY AND C1.CNAME = D1.CNAME;
/
--18)GIVE BRANCH CITY AND LIVING CITY OF PRAMOD.
SELECT B1.CITY , C1.CITY FROM BRANCH B1,CUSTOMERS C1, DEPOSIT D1 WHERE
C1.CNAME = 'PRAMOD' AND C1.CNAME = D1.CNAME AND D1.BNAME = B1.BNAME;
/
--19)GIVE BRANCH CITY OF SUNIL AND BRANCH CITY OF ANIL.
SELECT B1.CITY FROM DEPOSIT D1,BRANCH B1 WHERE D1.BNAME = B1.BNAME AND
D1.CNAME = 'SUNIL' OR D1.CNAME = 'ANIL';
/
--20) GIVE LIVING CITY OF ASHOK AND LIVING CITY OF AJAY.
SELECT CNAME,CITY FROM CUSTOMERS WHERE CUSTOMERS.CNAME = 'ASHOK' OR
CUSTOMERS.CNAME = 'AJAY';
/=================================================================================
SQL\Ch07\Assign4
--1)LIST ALL THE CUSTOMERS WHO ARE DEPOSITORS BUT NOT BORROWERS.
/=================================================================================
SQL\Ch07\Assign5
SELECT SUM(AMOUNT) FROM DEPOSIT WHERE ADATE > '1-JAN-96' GROUP BY BNAME;
/
--15)GIVE BRANCHWISE LOAN OF CUSTOMER LIVING IN NAGPUR.
SELECT SUM(BR1.AMOUNT) FROM BORROW BR1, CUSTOMERS C1 WHERE
C1.CNAME=BR1.CNAME AND C1.CITY = 'NAGPUR' GROUP BY BR1.BNAME;
/
--16)COUNT TOTAL NUMER OF CUSTOMERS.
SELECT COUNT(DISTINCT (CNAME)) FROM CUSTOMERS;
/
--17)COUNT TOTAL NUMBER OF DEPOSITORS BRANCHWISE.
SELECT COUNT(DISTINCT (CNAME)) FROM DEPOSIT GROUP BY BNAME;
/
--18)GIVE MAXIMUM LOAN FROM BRANCH VRCE.
SELECT MAX(AMOUNT) FROM BORROW WHERE BNAME ='VRCE';
/
--19)GIVE LIVING CITYWISE LOAN OF BORROWERS.
SELECT SUM(BR1.AMOUNT) FROM BORROW BR1, CUSTOMERS C1 WHERE C1.CNAME
=BR1.CNAME GROUP BY C1.CITY;
/
--20)GIVE NUMBER OF CUSTOMERS WHO ARE DEPOSITORS AND BORROWERS.
SELECT COUNT(DISTINCT (CNAME)) FROM CUSTOMERS WHERE CNAME IN
((SELECT CNAME FROM DEPOSIT)
INTERSECT
(SELECT CNAME FROM BORROW));
/=================================================================================
SQL\Ch07\Assign6
/
--4)LIST THE NAME OF CUSTOMERS HAVING MAXIMUM DEPOSIT.
SQL\Ch07\Assign7
UPDATE DEPOSIT
SET AMOUNT = AMOUNT + 10
WHERE CNAME IN
(SELECT D1.CNAME FROM DEPOSIT D1
WHERE CNAME IN
(SELECT C1.CNAME FROM CUSTOMERS C1
WHERE C1.CNAME = D1.CNAME GROUP BY C1.CITY HAVING AVG(D1.AMOUNT)
>= ALL (SELECT D2.AMOUNT FROM DEPOSIT D2,
CUSTOMERS C2 WHERE D2.CNAME = C2.CNAME AND C2.CITY = C1.CITY)))
/
/
--7)ASSIGN TO DEPOSIT OF ANIL MAXIMUM DEPOSIT FROM VRCE BRANCH.
UPDATE DEPOSIT
SET AMOUNT = (SELECT MAX(AMOUNT) FROM DEPOSIT WHERE BNAME = 'VRCE') WHERE
CNAME = 'ANIL';
/
--8)CHANGE THE LIVING CITY OF VRCE BRANCH BORROWERS TO NAGPUR.
UPDATE CUSTOMERS
SET CITY = 'NAGPUR' WHERE CNAME IN
(SELECT CNAME FROM BORROW WHERE BNAME = 'VRCE');
/
--9)UPDATE DEPOSIT OF ANIL , GIVE HIM MAXIMUM DEPOSIT FROM DEPOSITORS IN LIVING
CITY NAGPUR.
UPDATE DEPOSIT
SET AMOUNT = (SELECT MAX(AMOUNT) FROM DEPOSIT, CUSTOMERS WHERE
DEPOSIT.CNAME = CUSTOMERS.CNAME AND CUSTOMERS.CITY
= 'NAGPUR')
WHERE CNAME = 'ANIL';
/
--10)GIVE DEPOSIT TO ANIL, SUM OF DEPOSIT OF SUNIL AND VIJAY.
UPDATE DEPOSIT
SET AMOUNT = (SELECT SUM(AMOUNT) FROM DEPOSIT WHERE CNAME IN
('SUNIL','VIJAY')) WHERE CNAME = 'ANIL';
/
--11) TRANSFER 10 RS FROM ACCOUNT OF ANIL TO SUNIL.
UPDATE DEPOSIT
SET AMOUNT = AMOUNT - 10 WHERE CNAME = 'ANIL';
UPDATE DEPOSIT
SET AMOUNT = AMOUNT + 10 WHERE CNAME = 'SUNIL';
/
--12) TRANSFER 10 RS FROM ACCOUNT OF ANIL TO SUNIL IF BOTH ARE HAVING SAME BRANCH.
UPDATE DEPOSIT
SET AMOUNT = AMOUNT - 10 WHERE CNAME = 'ANIL' AND BNAME IN
(SELECT D1.BNAME FROM DEPOSIT D1 WHERE D1.CNAME = 'SUNIL');
UPDATE DEPOSIT
SET
AMOUNT = AMOUNT + 10 WHERE CNAME = 'SUNIL' AND BNAME IN
(SELECT D2.BNAME FROM DEPOSIT D2 WHERE D2.CNAME = 'ANIL');
/
--13) TRANSFER 10 RS FROM ACCOUNT OF ANIL TO SUNIL IF BOTH ARE LIVING IN NAGPUR.
UPDATE DEPOSIT
SET AMOUNT = AMOUNT - 10 WHERE CNAME = 'ANIL' AND CNAME IN
(SELECT C1.CNAME FROM CUSTOMERS C1, CUSTOMERS C2 WHERE
C2.CNAME = 'SUNIL' AND C1.CITY = 'NAGPUR' AND C1.CITY=C2.CITY);
UPDATE DEPOSIT
SET
AMOUNT = AMOUNT + 10 WHERE CNAME = 'SUNIL' AND CNAME IN
(SELECT C2.CNAME FROM CUSTOMERS C2, CUSTOMERS C4 WHERE
C4.CNAME = 'ANIL' AND C2.CITY=C4.CITY);
/
--14)TRANSFER 10 RS FROM ACCOUNT OF ANIL TO SUNIL IF BOTH ARE LIVING IN SAME CITY.
UPDATE DEPOSIT
SET AMOUNT = AMOUNT + 10 WHERE CNAME = 'SUNIL' AND CNAME IN
(SELECT C3.CNAME FROM CUSTOMERS C3, CUSTOMERS C4 WHERE
C3.CNAME='ANIL' AND C3.CITY = C4.CITY);
/
--16)GIVE 100 RS MORE TO ALL DEPOSITORS IF THEY ARE MAXIMUM DEPOSITORS IN THEIR
RESPECTIVE BRANCH.
UPDATE DEPOSIT
SET AMOUNT = AMOUNT + 100
WHERE CNAME IN
(SELECT D1.CNAME FROM DEPOSIT D1
WHERE BNAME IN
( SELECT D1.BNAME FROM DEPOSIT D1
GROUP BY D1.BNAME HAVING AVG(D1.AMOUNT) >= ALL
(SELECT MAX(D2.AMOUNT) FROM DEPOSIT D2 WHERE
D1.BNAME = D2.BNAME GROUP BY D2.BNAME)));
/
--17)GIVE 10 RS MORE TO ALL DEPOSITORS HAVING MORE DEPOSIT THAN THE AVERAGE
DEPOSIT.
UPDATE DEPOSIT
SET AMOUNT = AMOUNT + 10
WHERE CNAME IN
(SELECT D1.CNAME FROM DEPOSIT D1 GROUP BY D1.CNAME
HAVING SUM(D1.AMOUNT) > ALL (SELECT AVG(D2.AMOUNT) FROM DEPOSIT D2));
/
--18)GIVE 10 RS MORE FOR ALL DEPOSITORS HAVING MORE DEPOSIT THAN THE AVERAGE
DEPOSIT OF THEIR BRANCH.
UPDATE DEPOSIT
SET AMOUNT = AMOUNT + 10
WHERE CNAME IN
(SELECT D1.CNAME FROM DEPOSIT D1
GROUP BY D1.BNAME
HAVING SUM(D1.AMOUNT) > ALL (SELECT AVG(D2.AMOUNT)FROM DEPOSIT D2
WHERE D2.BNAME = D1.BNAME GROUP BY D2.BNAME));
/
--19)GIVE 10 RS MORE FOR ALL CUSTOMERS LIVING IN SAME CITY WHERE THEIR BRANCH IS
LOCATED.
UPDATE DEPOSIT
SET AMOUNT = AMOUNT + 10
WHERE CNAME IN
(SELECT D1.CNAME FROM DEPOSIT D1, BRANCH B1, CUSTOMERS C1
WHERE B1.BNAME = D1.BNAME AND B1.CITY = C1.CITY AND
C1.CNAME = D1.CNAME);
/
--20)GIVE 10 RS MORE FOR ALL DEPOSITORS HAVING MORE DEPOSIT THAN AVERAGE DEPOSIT
OF THEIR BRANCH.
UPDATE DEPOSIT
SET AMOUNT = AMOUNT + 10
WHERE CNAME IN
(SELECT D1.CNAME FROM DEPOSIT D1,CUSTOMERS C1
WHERE C1.CNAME = D1.CNAME GROUP BY C1.CITY,D1.CNAME HAVING
SUM(D1.AMOUNT)
>= ALL (SELECT D2.AMOUNT FROM DEPOSIT D2,
CUSTOMERS C2 WHERE D2.CNAME = C2.CNAME AND C2.CITY = C1.CITY))
/=================================================================================
SQL\Ch07\Assign8
SQL\Ch08
SQL\Ch08\ASSIGN1
SELECT BATCHCODE
FROM BATCH
WHERE EXPECTEDINCOME-NETINCOME>2000;
/
-- 7) LIST ENQUIRIES FOR COURSE CODE 11.
SELECT * FROM ENQUIRY
WHERE COURSECODE=11;
/
--8) LIST NAME OF PERSONS HAVING CITY NAGPUR.
SELECT FNAME
FROM ENQUIRY
WHERE CITY='NAGPUR';
/
--9) LIST ADVERTISEMENT DATE.
SELECT ADVDATE
FROM ADVERTISEMENT;
/
--10) LIST ROLLNO FOR PARTICULAR BATCH.
SELECT ROLLNO
FROM ENROLLMENT
WHERE BATCHCODE=10001;
/
--11)LIST ALL EXPENDITURE TYPE FROM EXPMASTER.
SELECT EXPTYPE
FROM EXPMASTER;
/
--12) LIST DETAILS OF PERSON ENQUIRED FOR A PARTICULAR COURSE.
SELECT * FROM ENQUIRY
WHERE COURSECODE=11;
/
--13) LIST DETAIL ABOUT ENROLLMENT.
SELECT * FROM ENROLLMENT;
/
--14) LIST CONTENTS OF EXPENDITURE.
SELECT * FROM EXPENDITURE;
/
--15)LIST INSTALLMENTS HAVING STATUS 'N'.
SELECT * FROM INSTALLMENT
WHERE INSTALLSTATUS='N';
/
-- 16) LIST SYLLABUS OF PARTICULAR COURSE.
SELECT SYLLABUS
FROM COURSE
WHERE COURSECODE=11;
/
--17)LIST CONTENTS OF EXPENDITURE MASTER.
SELECT * FROM EXPMASTER;
/
--18) LIST CONTENTS OF FEESPAID.
SELECT * FROM FEESPAID;
/
--19) LIST ALL DETAILS FOR EXPAUDIT.
SELECT * FROM EXPAUDIT;
/=================================================================================
SQL\Ch08\ASSIGN2
SELECT COURSENAME,BATCHCODE,STARTINGDATE
FROM BATCH,COURSE
/
--3)LIST MEDIA NAME,DATE OF ADVERTISEMENT FOR A SPECIFIED COURSE.
SELECT MEDIANAME,ADVDATE
FROM ENQUIRY,ADVERTISEMENT
WHERE ENQUIRY.REFCODE=ADVERTISEMENT.REFCODE
AND ENQUIRY.COURSECODE=11;
/
--4)LIST ROLL NO AND NAME OF STUDENTS FOR A PARTICULAR BATCH.
SELECT ROLLNO,FNAME,SNAME
FROM ENQUIRY,ENROLLMENT
WHERE ENQUIRY.ENQUIRYNO=ENROLLMENT.ENQUIRYNO
AND ENROLLMENT.BATCHCODE=11001;
/
5)LIST ROLLNO,ADDRESS OF STUDENTS IN SPECIFIED BATCH.
SELECT ROLLNO,PLOTNO,STREET,CITY,PINCODE,PHONE
FROM ENQUIRY,ENROLLMENT
WHERE ENQUIRY.ENQUIRYNO=ENROLLMENT.ENQUIRYNO
AND ENROLLMENT.BATCHCODE=11001;
OR
SELECT ROLLNO,(PLOTNO||'
'||STREET||'
'||CITY||'
'||PIN||'
'||PHONE) ADDRESS
FROM ENQUIRY,ENROLLMENT
WHERE ENQUIRY.ENQUIRYNO=ENROLLMENT.ENQUIRYNO
AND ENROLLMENT.BATCHCODE=11001;
/
6) LIST ROLLNO, NAME,AND FEESPAID FOR A PARTICULAR BATCH.
SELECT
ENROLLMENT.ROLLNO,SNAME,ENROLLMENT.BATCHCODE,FEESPAID.AMOUNT
FROM ENQUIRY,ENROLLMENT,FEESPAID
WHERE ENQUIRY.ENQUIRYNO=ENROLLMENT.ENQUIRYNO
AND ENROLLMENT.ROLLNO=FEESPAID.ROLLNO
AND ENROLLMENT.BATCHCODE=11001;
/
7)LIST ROLLNO,NAME,INSTALLMENT DATE,AMOUNT FOR A PARTICULAR BATCH.
SELECT ENROLLMENT.ROLLNO,ENQUIRY.FNAME,
INSTALLEMENT.INDATE,INSTALLEMENT.AMOUNT
FROM ENQUIRY,ENROLLMENT,INSTALLMENT
WHERE ENQUIRY.ENQUIRYNO=ENROLLMENT.ENQUIRYNO
AND ENROLLMENT.ROLLNO=INSTALLMENT.ROLLNO
AND ENROLLMENT.BATCHCODE=11001;
/
8)LIST COURSE NAME, AND DETAILS OF ALL BATCHES.
SELECT
COURSE.COURSENAME,BATCH.BATCHCODE,BATCH.STARTINGDATE,BATCH.DURATION,
BATCH.COURSEFEES,BATCH.NETINCOME,BATCH.EXPECTEDINCOME,BATCH.LASTNO
FROM COURSE,BATCH
WHERE COURSE.COURSECODE=BATCH.COURSECODE;
/
--9.LIST ROLLNO, NAME OF STUDENTS WHO PAID FEES MORE THAN 5000 AT A TIME.
SELECT ENROLLMENT.ROLLNO,ENQUIRY.FNAME
FROM ENQUIRY,ENROLLMENT,FEESPAID
WHERE ENQUIRY.ENQUIRYNO=ENROLLMENT.ENQUIRYNO
AND ENROLLMENT.ROLLNO=FEESPAID.ROLLNO
AND FEESPAID.AMOUNT>5000;
/=================================================================================
SQL\Ch08\Assign3
SELECT B1.BATCHCODE
FROM BATCH B1,BATCH B2
WHERE B1.EXPECTEDINCOME>B2.EXPECTEDINCOME
AND B1.COURSECODE=B2.COURSECODE
AND B2.BATCHCODE=10001;
/
--4)LIST NAME OF STUDENTS WHICH ARE IN SAME BATCH IN WHICH PERSON JAYANT IS
--ENROLLED.
SELECT E1.FNAME NAME
FROM ENQUIRY E1,ENQUIRY E2,ENROLLMENT EN1,ENROLLMENT EN2
WHERE E2.FNAME='JAYANT'
AND EN1.ENQUIRYNO=E2.ENQUIRYNO
AND EN1.BATCHCODE=EN2.BATCHCODE
AND EN2.ENQUIRYNO=E1.ENQUIRYNO;
/
--5.LIST NAME OF STUDENTS WHICH ARE ENROLLED IN SAME COURSE WHERE JAYANT --IS
ENROLLED.
SELECT E1.FNAME NAME FROM ENQUIRY E1,ENQUIRY E2,ENROLLMENT EN1,ENROLLMENT
EN2,BATCH B1,BATCH B2
WHERE E2.FNAME='JAYANT'
AND E2.ENQUIRYNO=EN1.ENQUIRYNO
AND EN1.BATCHCODE=B1.BATCHCODE
AND B1.COURSECODE=B2.COURSECODE
AND B2.BATCHCODE=EN2.BATCHCODE
AND EN2.ENQUIRYNO=E1.ENQUIRYNO;
/=================================================================================
SQL\Ch08\Assign4
SELECT FNAME
FROM ENQUIRY E1,ENROLLMENT EN1
WHERE E1.ENQUIRYNO=EN1.ENQUIRYNO
AND EN1.BATCHCODE
IN(SELECT BATCHCODE
FROM BATCH
WHERE COURSECODE=10);
/
--3)ASSUMING ONE PERSON IS ENROLLED IN MULTIPLE BATCHES,LIST ALL PERSONS
--ENROLLED IN ALL THOSE BATCHES WHERE SOMESH HAS ENROLLED;
SELECT FNAME
FROM ENQUIRY E1
WHERE
NOT EXISTS((SELECT EN2.BATCHCODE
FROM ENQUIRY E2,ENROLLMENT EN2
WHERE E2.FNAME='SOMESH'
AND E2.ENQUIRYNO=EN2.ENQUIRYNO)
MINUS
SELECT EN3.BATCHCODE
FROM ENQUIRY E3,ENROLLMENT EN3
WHERE E3.ENQUIRYNO=EN3.ENQUIRYNO
AND E3.FNAME=E1.FNAME));
/
--4)ASSUMING ONE PERSON IS ENROLLED IN MULTIPLE COURSES,LIST NAME OF
PERSONS ENROLLED IN ALL THOSE BATCHES.
SELECT FNAME
FROM ENQUIRY E1
WHERE
NOT EXISTS((SELECT EN2.BATCHCODE
FROM ENQUIRY E2,ENROLLMENT EN2
WHERE E2.FNAME='ANIL'
AND E2.ENQUIRYNO=EN2.ENQUIRYNO)
MINUS
(SELECT EN3.BATCHCODE
FROM ENQUIRY E3,ENROLLMENT EN3
WHERE E3.ENQUIRYNO=EN3.ENQUIRYNO
AND E3.FNAME=E1.FNAME))
/
Assuming one person is enrolled in multiple batches, list persons enrolled in all those batches where ANIL
has enrolled.
SQL>SELECT E1.FNAME
2 FROM ENQUIRY E1
3 WHERE NOT EXISTS
4 ((SELECT B1.COURSECODE
5 FROM BATCH B1,ENROLLMENT EN1,ENQUIRY E2
6 WHERE B1.BATCHCODE = EN1.BATCHCODE
7 AND EN1.ENQUIRYNO = E2.ENQUIRYNO
8 AND E2.FNAME = ’ANIL’)
9 MINUS
10 (SELECT B2.COURSECODE
11 FROM BATCH B2,ENROLLMENT EN2
12 WHERE B2.BATCHCODE = EN2.BATCHCODE
13 AND EN2.ENQUIRYNO = E1.ENQUIRYNO));
/
List names of students who have not paid any fees.
SQL>SELECT FNAME
2 FROM ENQUIRY E1
3 WHERE ENROLLSTATUS = ’Y’
4 AND
5 NOT EXISTS
6 (SELECT * FROM ENROLLMENT EN1,FEESPAID F1
7 WHERE EN1.ENQUIRYNO = E1.ENQUIRYNO
8 AND EN1.ROLLNO = F1.ROLLNO);
/
--8)LIST NAME OF PERSONS ENROLLED IN BATCH HAVING STARTING DATE MORE
--THAN SPECIFIED DATE.
SELECT FNAME
FROM ENQUIRY E1,ENROLLMENT EN1,BATCH B1
WHERE E1.ENQUIRYNO=EN1.ENQUIRYNO
AND EN1.BATCHCODE=B1.BATCHCOD
AND EXISTS(SELECT * FROM BATCH B2
WHERE STARTINGDATE>'23-JAN-99'
AND B2.BATCHCODE=B1.BATCHCODE );
/
--9. LIST NAME OF STUDENTS LIVING IN NAGPUR AND ENROLLED IN COURSE 10.
SELECT FNAME
FROM ENQUIRY E1
WHERE E1.CITY='NAGPUR'
AND EXISTS(SELECT * FROM ENROLLMENT EN1,BATCH B1
WHERE EN1.BATCHCODE=B1.BATCHCODE
AND EN1.ENQUIRYNO=E1.ENQUIRYNO
AND B1.COURSECODE=10);
/=================================================================================
SQL\Ch08\Assign5
SELECT BATCHCODE
FROM BATCH
GROUP BY BATCH.BATCHCODE
HAVING AVG(BATCH.NETINCOME)>1000;
/
3)FIND OUT NAME OF COURSE HAVING MAXIMUM NETINCOMECOME
SELECT C.COURSENAME
FROM COURSE C
WHERE C.COURSECODE
IN(SELECT B.COURSECODE
FROM BATCH B
GROUP BY B.COURSECODE
HAVING SUM(B.NETINCOME)>=ALL
( SELECT SUM(B1.NETINCOME)FROM BATCH B1
GROUP BY (B1.COURSECODE))) ;
/
--4)FIND OUT NAME OF COURSE HAVING MAXIMUM DIFFERENCE OF NETINCOMECOME
AND EXPECTED INCOME.
SELECT C.COURSENAME
FROM COURSE C
WHERE C.COURSECODE
IN(SELECT B.COURSECODE
FROM BATCH B
GROUP BY B.COURSECODE
HAVING (SUM(B.EXPECTEDINCOME)-SUM(B.NETINCOME))>=ALL
( SELECT (SUM(B1.EXPECTEDINCOME)-SUM(B1.NETINCOME))FROM BATCH B1
GROUP BY (B1.COURSECODE))) ;
/
--5). FIND OUT TOTAL FEES PAID BY A PARTICULAR STUDENT.
SELECT SUM(AMOUNT) FROM FEESPAID
WHERE ROLLNO = 11001100;
/
--6)FIND OUT DIFFERENCE BETWEEN EXPECTED FEES AND TOTAL FEES FOR A
--PARTICULAR ROLL NUMBER.
SELECT (BATCH.COURSEFEES-F1.TOTAL)
FROM (SELECT ROLLNO ROLLNO1, SUM(AMOUNT) TOTAL
FROM FEESPAID
GROUP BY (ROLLNO)) F1 ,BATCH,ENROLLMENT
WHERE F1.ROLLNO1=ENROLLMENT.ROLLNO
AND ENROLLMENT.BATCHCODE=BATCH.BATCHCODE
AND F1.ROLLNO1=11001100;
/
--7)FIND OUT COURSE WISE INCOME.
SELECT A.COURSECODE,SUM(B.NETINCOME)
FROM COURSE A,BATCH B
WHERE A.COURSECODE=B.COURSECODE
GROUP BY A.COURSECODE;
/
--8)FIND OUT SUM OF FEES AND ROLLNO FOR A SEPCIFIED COURSE;
SELECT D.ROLLNO,SUM(A.AMOUNT)
FROM FEESPAID A,COURSE B,BATCH C,ENROLLMENT D
WHERE B.COURSENAME='JAVA'
AND A.ROLLNO=D.ROLLNO
AND D.BATCHCODE=C.BATCHCODE
AND C.COURSECODE=B.COURSECODE
GROUP BY D.ROLLNO
/
--9) FIND OUT TOTAL AMOUNT COLLECTED
SQL\Ch08\Assign6
SELECT TO_CHAR(A.PAYDATE,'MM'),TO_CHAR(A.PAYDATE,'MON'),
SUM(A.AMOUNT) FROM FEESPAID A
GROUP BY TO_CHAR(A.PAYDATE,'MM'),TO_CHAR(A.PAYDATE,'MON')
ORDER BY 1;
/
--2)FIND OUT SUM OF AMOUNT COLLECTED WEEKLY (YEARLY WEEK).
SELECT TO_CHAR(PAYDATE,'IW'),SUM(AMOUNT)
FROM FEESPAID
GROUP BY TO_CHAR(PAYDATE,'IW');
/
--3. FIND OUT MONTHLY ,WEEKLY INCOME.
SELECT TO_CHAR(PAYDATE,'MON'),TO_CHAR(PAYDATE,'W'),SUM(AMOUNT)
FROM FEESPAID
GROUP BY TO_CHAR(PAYDATE,'MON'),TO_CHAR(PAYDATE,'W')
/
--4)FIND OUT ROLL NO AND NAME OF STUDENT WHO HAS NOT PAID
AMOUNT ACCORDING TO THEIR INSTALLMENT DATE
SELECT EN1.ROLLNO,E1.FNAME
FROM ENROLLMENT EN1,ENQUIRY E1,INSTALLMENT I1
WHERE EN1.ENQUIRYNO=E1.ENQUIRYNO
AND EN1.ROLLNO=I1.ROLLNO
AND NOT EXISTS (SELECT * FROM FEESPAID F1
WHERE I1.ROLLNO=F1.ROLLNO
AND I1.AMOUNT=F1.AMOUNT
AND I1.INDATE=F1.PAYDATE)
/
--5.FIND OUT EXPENDITURE MONTHWISE.
SELECT TO_CHAR(STARTINGDATE,'Q'),SUM(NETINCOME)
FROM BATCH
WHERE BATCHCODE=11001
GROUP BY TO_CHAR(STARTINGDATE,'Q')
/
--8) FIND OUT MONTHLY AVG INCOME AND AVG EXPENDITURE.
SQL\Ch08\Assign7
SELECT INITCAP(FNAME),INITCAP(LNAME)
FROM ENQUIRY
UPDATE ENQUIRY
SET FNAME=INITCAP(FNAME);
/
-- 4) DISPLAY AMOUNT IN FORMAT RS 99,999,99.
SELECT TO_CHAR(NETINCOME,'09,999,99')
FROM BATCH
/
5) PRODUCE STATICSTICS OF FEES COLLECTED AS
LOW HIGH NO OF STUDENTS
1 1000 3
1001 2000 5
SELECT P1.PAYDATE,SUM(P2.AMOUNT)
FROM FEESPAID P1,FEESPAID P2
WHERE P1.PAYDATE>=P2.PAYDATE
GROUP BY P1.PAYDATE
ORDER BY 1;
/=================================================================================
SQL\Ch08\Assign8
delete course;
/
--1) delete all rows from course
delete course;
/
--2) delete all rows from BATCH having specified BATCHCODE
delete BATCH
where BATCHCODE=11001;
/
--3) delete all rows from BATCH which is not having any specified expected income
delete BATCH
where EXPECTEDINCOME=0;
/
--4) delete all rows from enq for a specified coursename
delete enquiry
where coursecode in
select coursecode
from course
where coursename='oracle');
/
--5)delete all rows from enq which are six months before
--systime and are not having enrollment.
delete enquiry
where months_between(SYSDATE,ENQDATE)>6
and ENROLLSTATUS='n';
/
--6) update BATCH set NETINCOME according to feespaid;
UPDATE BATCH B
SET B.NETINCOME=(SELECT SUM(AMOUNT)
FROM FEESPAID F1,ENROLLMENT EN1,BATCH B1
WHERE F1.ROLLNO=EN1.ROLLNO
AND EN1.BATCHCODE=B1.BATCHCODE
AND B1.BATCHCODE=B.BATCHCODE)
WHERE EXISTS (SELECT * FROM FEESPAID F2,ENROLLMENT EN2,BATCH B2
WHERE F2.ROLLNO=EN2.ROLLNO
AND EN2.BATCHCODE=B2.BATCHCODE
AND B2.BATCHCODE=B.BATCHCODE);
/
--7) UPDATE SET EXPECTED INCOME FROM INSTALLMENT
update BATCH d
set EXPECTEDINCOME=(select sum(a.AMOUNT)
from INSTALLEMENT a,ENROLLMENT b,BATCH c
where a.ROLLNO=b.ROLLNO
and b.BATCHCODE=c.BATCHCODE
and b.BATCHCODE=d.BATCHCODE)
where d.BATCHCODE in
(SELECT BATCHCODE
from ENROLLMENT,INSTALLEMENT
WHERE ENROLLMENT.ROLLNO=INSTALLEMENT.ROLLNO
AND ENROLLMENT.BATCHCODE=d.BATCHCODE);
/
--8)UPDATE LASTNO OF COURSE HAVING DATA FROM BATCH.
UPDATE COURSE C1
SET C1.LASTNO=(SELECT COUNT(*)
FROM BATCH B1
WHERE B1.COURSECODE=C1.COURSECODE)
WHERE C1.COURSECODE IN
(SELECT B2.COURSECODE
FROM BATCH B2
WHERE B2.COURSECODE=C1.COURSECODE);
/
--9) DELETE DUPLICATE ROWS FROM ENQUIRY.
DELETE ENQUIRY A
WHERE A.ROWID NOT IN
(SELECT MAX(A1.ROWID) FROM ENQUIRY A1
WHERE A1.ENQUIRYNO=A.ENQUIRYNO);
/=================================================================================
SQL\Ch09
1. Suppose data is
T
Sn Amt
n(2) n(4)
1 100
4 300
7 200
produce the output
Sn ClAmt
1 100
4 400
7 600
2. For the above query find our Sn for which clamt is maximum
3. Suppose data is
T
Sn Amt
1 100
4 300
7 200
11 400
15 150
4 Suppose data is
T
Sn Amt
1 100
4 300
4 300
11 400
15 150
5 Suppose data is
company
cname city
ACC NGP
ACC CAL
HLL NGP
HLL MUM
TATA NGP
TATA CAL
TATA DLH
Find out name of companies located in all those cities
where ACC is located(TATA).
Solution: 1
Table created.
1 row created.
SQL> /
Enter value for sn: 4
Enter value for amt: 300
old 1: insert into T values(&sn,&amt)
new 1: insert into T values(4,300)
1 row created.
SQL> /
Enter value for sn: 7
Enter value for amt: 200
old 1: insert into T values(&sn,&amt)
new 1: insert into T values(7,200)
1 row created.
SQL> commit;
SN AMT SN AMT
--------- --------- --------- ---------
1 100 1 100
4 300 1 100
7 200 1 100
1 100 4 300
4 300 4 300
7 200 4 300
1 100 7 200
4 300 7 200
7 200 7 200
9 rows selected.
SN AMT SN AMT
--------- --------- --------- ---------
1 100 1 100
4 300 1 100
7 200 1 100
4 300 4 300
7 200 4 300
7 200 7 200
1 select t1.sn,sum(t2.amt)
2 from t t1,t t2
3 where t1.sn >=t2.sn
4* group by t1.sn
SQL> /
SN SUM(T2.AMT)
--------- -----------
1 100
4 400
7 600
2. For the above query find our Sn for which clamt is maximum
3. Suppose data is
T
Sn Amt
1 100
4 300
7 200
SN AMT SN AMT
--------- --------- --------- ---------
1 100 1 100
4 300 1 100
7 200 1 100
1 100 4 300
4 300 4 300
7 200 4 300
1 100 7 200
4 300 7 200
7 200 7 200
9 rows selected.
SN AMT SN AMT
--------- --------- --------- ---------
1 100 4 300
7 200 4 300
1 100 7 200
SQL> select t1.sn,count(t1.amt) from t t1,t t2 where t1.amt < t2.amt group by t1.sn;
SN COUNT(T1.AMT)
--------- -------------
1 2
7 1
SN COUNT(T1.AMT)
--------- -------------
1 2
4 Suppose data is
T
Sn Amt
1 100
4 300
4 300
11 400
15 150
delete t t1
where t1.rowid > any (select rowid
from t t2
where t1.sn=t2.sn
and t1.amt = t2.amt)
or
delete t t1
where 1 <= (select count(*)
from t t2
where t2.sn=t1.sn and
t2.amt = t1.amt and
t2.rowid > t1.rowid)
5 Suppose data is
company
cname city
ACC NGP
ACC CAL
HLL NGP
HLL MUM
TATA NGP
TATA CAL
TATA DLH
Find out name of companies located in all those cities
where ACC is located(TATA). (Hint use not exists and minus)
ENAME MNAME
---------- ----------
Anil Ajay
Shankar Vijay
Jaya
Sunil Jaya
Vijay
Prakash Shankar
Ajay
7 rows selected.
SQL> select ename,mname from manager connect by prior ename=mname start with ename='Vijay';
ENAME MNAME
---------- ----------
Vijay
Shankar Vijay
Prakash Shankar
SQL> edit
Wrote file afiedt.buf
ENAME MNAME
---------- ----------
Vijay
SQL> edit
Wrote file afiedt.buf
1* select level,ename,mname from manager connect by prior mname=ename start with ename='Prakash'
SQL> /
SQL> edit
Wrote file afiedt.buf
count number of employees at each level
1 select level,count(ename) from manager connect by prior ename=mname start with ename='Vijay'
2* group by level
SQL> /
LEVEL COUNT(ENAME)
--------- ------------
1 1
2 2
3 1
/=================================================================================
SQL\Ch10
CHECK CONSTRAINT
Table created.
Table altered.
1 row created.
## CHECK CONSTRAINT CAN BE ENABLE USING NOVALIDATE OPTION EVEN IF DATA IS NOT
ACCORDING
## TO CONSTRAINT
SQL> edit
Wrote file afiedt.buf
Table altered.
deferred constraints
## Deferred constraints are imposed at the end of transaction if data is not according to constraint then the
whole transaction is rolled back
## immediate constraint is imposed at the end of dml statement.
Table created.
1 row created.
SQL> commit;
Commit complete.
A B
--------- ---------
1 7
2 8
1 row created.
1 row created.
SQL> commit;
commit
*
ERROR at line 1:
ORA-02091: transaction rolled back
ORA-00001: unique constraint (PSD.PK_DEFER_CON) violated
A B
--------- ---------
1 7
2 8
Constraint set.
Constraint set.
1 row created.
SQL> COMMIT;
COMMIT
*
ERROR at line 1:
ORA-02091: transaction rolled back
ORA-00001: unique constraint (PSD.PK_DEFER_CON) violated
Table created.
1 row created.
Table altered.
1 row created.
1 row created.
SQL> EDIT
Wrote file afiedt.buf
SQL> SELECT * FROM CON_EXCEPT WHERE ROWID IN (SELECT ROW_ID FROM EXCEPTIONS);
A B
--------- ---------
1 7
1 8
2 2
SQL> EDIT
Wrote file afiedt.buf
1 SELECT T.A,T.B,R.CONSTRAINT
2 FROM CON_EXCEPT T , EXCEPTIONS R
3* WHERE T.ROWID=R.ROW_ID
SQL> /
A B CONSTRAINT
--------- --------- ------------------------------
1 7 PK_CON_EXCEPT
1 8 PK_CON_EXCEPT
2 2 CK_CON_EXCEPT
student(rollno,name)
student_marks(rollno,subject,marks) rollno is foreign key
Table created.
1 row created.
1 row created.
### foreign key columns (a,b) in child table references unique key or primary key
## in parent table . default is primary key i.e if onle parent table is mentioned
Table created.
1 row created.
## it will not accept values which are not in parent table
1 row created.
1 row created.
## if there is null value in any of the foreign key column it is accepted
## match none implementation
## if parent row is 1 1 1 then following rows are accepted in child rows according to
## type of implementation
1 row created.
A B C
--------- --------- ---------
1 1 1
2 2 2
A B D
--------- --------- ---------
1 1 5
2 2 4
2 2 7
5 6
## on delete cascade means if parent row is deleted corrosponding child rows are also deleted
1 row deleted.
A B D
--------- --------- ---------
1 1 5
5 6
## set to null
SQL> alter table child drop constraint fk_child;
Table altered.
1 alter table child add constraint fk_child foreign key (a,b) references parent(a,b)
2* on delete set null
SQL> /
Table altered.
A B C
--------- --------- ---------
1 1 1
SQL> select * from child;
A B D
--------- --------- ---------
1 1 5
5 6
1 row deleted.
A B D
--------- --------- ---------
5
5 6
SQL>
## set to default is not implemented
SQL> create table child_2(a number(2) default 1,b number(2) default 1,c number(2),
2 constraint fk_child_2 foreign key (a,b) references parent(a,b) on delete set default);
constraint fk_child_2 foreign key (a,b) references parent(a,b) on delete set default)
*
ERROR at line 2:
ORA-03001: unimplemented feature
1 create table child_2(a number(2) default 1,b number(2) default 1,c number(2),
2* constraint fk_child_2 foreign key (a,b) references parent(a,b))
SQL> /
Table created.
# #default means if no value is mentioned then default value is assumed instead of null value
1 row created.
A B C
--------- --------- ---------
1 1 4
1 row created.
A B C
--------- --------- ---------
5
Table dropped.
#on update cascad is not implemented
SQL> create table child_2(a number(2) default 1,b number(2) default 1,c number(2),
2 constraint fk_child_2 foreign key (a,b) references parent(a,b) on update cascade);
constraint fk_child_2 foreign key (a,b) references parent(a,b) on update cascade)
*
ERROR at line 2:
ORA-00905: missing keyword
SQL> edit
SQL> alter table child disable constraint fk_child;
Table altered.
1 row created.
## foreign key constraint is enabled with novalidate option even if data is not
## according to constraint
## the constraints are enforced on next dml operations
##later on existing data can be validated using enable validate
Table altered.
1 row deleted.
Table altered.
Table dropped.
Table dropped.
/
Integrity Constraints
Table created.
1 row created.
Table altered.
1 row created.
1 row deleted.
Table altered.
Table created.
Table created.
SQL> edit
Wrote file afiedt.buf
SQL> edit
Wrote file afiedt.buf
Table created.
1 4 6
1 3 8
1 7 6
null 3 2
###### null values are not accepted in primary key columns
1 row created.
1 row created.
1 row created.
A B C
--------- --------- ---------
3 4 7
3 5 8
1 row created.
###### primary constraint is not enabled if existing data is against the constraint
###### even if novalidate clause is used
A B C
--------- --------- ---------
3 4 7
3 5 8
3 9 7
1 row deleted.
Table altered.
SQL> create table pk_con4(a number(2) primary key,b number(2) primary key,c number(2));
create table pk_con4(a number(2) primary key,b number(2) primary key,c number(2))
*
ERROR at line 1:
ORA-02260: table can have only one primary key
10 root node
5 15
11 17
/
unique key
Table created.
1 row created.
1 row created.
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
A B
--------- ---------
2 6
6
6
1 row created.
SQL> edit
Wrote file afiedt.buf
2 rows deleted.
Table altered.
SQL> alter table uk_con1 add constraint pk_uk_con1 primary key (a);
alter table uk_con1 add constraint pk_uk_con1 primary key (a)
*
ERROR at line 1:
ORA-02261: such unique or primary key already exists in the table
SQL> alter table uk_con1 add constraint pk_uk_con1 primary key (a,b);
Table altered.
index (B tree index)
89
10 100
1 17
52
5 502 ( N+ 1) ** L * N 100
2 35 60
3*2 101 * 1 * 100 10000
3 ** 2 *2 101 ** 2 * 10
/=================================================================================
SQL\Ch11
/
SELECT timeid,quantity,width_bucket(quantity,3,29,4) bucket_up
FROM SALES_data
WHERE CUSTID = 37290 AND timeid<'11-jan-1998'
order by 3
/
SELECT timeid,quantity,
(case when quantity < 5 then 5 else quantity end) calc_quantity_sold
FROM SALES_data
WHERE CUSTID = 37290 AND timeid<'11-jan-1998'
/
SELECT timeid,quantity,
(case when quantity between 0 and 17 then 1 else 0 end) "17",
(case when quantity between 18 and 37 then 1 else 0 end) "37"
FROM SALES_data
WHERE CUSTID = 37290 AND timeid<'11-jan-1998'
/
SELECT
(case when quantity between 0 and 17 then '0-17'
when quantity between 18 and 37 then '18-37'
else 'HIGH' end) range,
count(*) as cnt
FROM SALES_data
WHERE CUSTID = 37290 AND timeid<'11-jan-1998'
group by
(case when quantity between 0 and 17 then '0-17'
when quantity between 18 and 37 then '18-37'
else 'HIGH' end)
/
SELECT quantity,
(case when quantity between 0 and 17 then '0-17'
when quantity between 18 and 37 then '18-37'
else 'HIGH' end) range
FROM SALES_data
WHERE CUSTID = 37290 AND timeid<'11-jan-1998'
/
select to_char(timeid,'mon'), min(amount) KEEP (DENSE_RANK last ORDER BY (timeid)) lastvalue
from sales_data
where to_char(timeid,'yyyy')='1998'
and CUSTID = 37290
group by to_char(timeid,'mon'), to_char(timeid,'mm')
order by to_char(timeid,'mm')
/
select to_char(timeid,'mon'), amount
from sales_data s
where to_char(timeid,'yyyy')='1998'
and CUSTID = 37290
and
timeid in
(select max(s1.timeid)
from sales_data s1
where to_char(s1.timeid,'yyyy')='1998'
and s1.CUSTID = 37290
and to_char(s.timeid,'mon')=to_char(s1.timeid,'mon'))
order by to_char(timeid,'mm')
/
SELECT rank(10) WITHIN GROUP
(ORDER BY quantity) AS hrank,
percent_rank(10) WITHIN GROUP
(ORDER BY quantity) AS perc_rank,
cume_dist(10) WITHIN GROUP
(ORDER BY quantity) AS cume_rank
FROM SALES_data WHERE CUSTID = 37290 AND timeid<'11-jan-1998'
/
SELECT TIMEID,CHANNELID,AMOUNT
FROM SALES_data WHERE CUSTID = 37290
AND TIMEID < '11-JAN-1998'
/
SELECT TIMEID,CHANNELID,AMOUNT ,
LAG(AMOUNT,1)OVER( ORDER BY TIMEID)LG1, AMOUNT/LAG(AMOUNT,1)OVER( ORDER BY
TIMEID) RATIO_LAG , LEAD(AMOUNT,1)OVER(ORDER BY TIMEID) LD1,
AMOUNT/LEAD(AMOUNT,1)OVER(ORDER BY TIMEID) RATIO_LEAD
FROM SALES_data WHERE CUSTID = 37290
AND TIMEID < '11-JAN-1998'
ORDER BY 1
/
CREATE OR REPLACE FUNCTION CALC_DAYS(FDATE DATE) RETURN NUMBER AS
BEGIN
IF (TO_CHAR(FDATE,'DY') = 'MON') OR (TO_CHAR(FDATE,'DY')='TUE') THEN
RETURN(4);
ELSE
RETURN(2);
END IF;
END;
/
SELECT timeid,quantity
FROM SALES_data
WHERE CUSTID = 37290 AND timeid<'11-jan-1998'
/
SELECT timeid,quantity ,cume_dist()over(order by quantity)
FROM SALES_data
WHERE CUSTID = 37290 AND timeid<'11-jan-1998'
/
SELECT PERCENTILE_DISC(0.5) WITHIN GROUP
(ORDER BY quantity) AS perc_disc,
PERCENTILE_CONT(0.5) WITHIN GROUP
(ORDER BY quantity) AS perc_cont
FROM SALES_data
WHERE CUSTID = 37290 AND timeid<'11-jan-1998'
/
SELECT TIMEID,AMOUNT,CHANNELID ,
SUM(AMOUNT) OVER(PARTITION BY CHANNELID ) CALC_AMT ,
AMOUNT/SUM(AMOUNT) OVER(PARTITION BY CHANNELID ) perc
FROM SALES_data
WHERE CUSTID = 37290 AND ROWNUM<11
ORDER BY 3,1
/
SELECT TIMEID,AMOUNT,
CHANNELID ,
SUM(AMOUNT) OVER(PARTITION BY CHANNEL_ID ) CALC_AMT,
RATIO_TO_REPORT(AMOUNT) OVER(PARTITION BY CHANNELID ) RAT
FROM SALES_data
WHERE CUST_ID = 37290 AND ROWNUM<11
ORDER BY 3,1
/
SQL\Ch12
ROLLUP
Transaction Processing
Financial Systems
Manufacturing Systems
Production Activities
Marketing Activities
Sales Activities
ROLLUP IS HELPFUL IN
--Creating Reports
--Improved efficiency when creating the reports for the above mentioned areas will reduce system load.
--It makes calculations easier and more efficient, thereby enhancing database performance, scalability, and
simplicity.
All the requests above involve multiple dimensions. Many multidimensional questions require aggregated
data and comparisons of data sets, often across time, geography or budgets.
--The examples in this chapter show ROLLUP and CUBE used with the SUM() function. While this is the
most common type of aggregation, these extensions can also be used with all other functions available to
the GROUP BY clause, for example, COUNT, AVG, MIN, MAX, STDDEV, and VARIANCE. COUNT, which
is often needed in cross-tabular analyses, is likely to be the second most helpful function.
ROLLUP EXAMPLES
OUTPUT
12 rows selected
-----------------------------------------------------------------------------------------------
OUTPUT
21 rows selected
-------------------------------------------------------------------------------------------------
COMPARISION
--Regular aggregation rows that would be produced by GROUP BY without using ROLLUP
--First-level subtotals aggregating across Department for each combination of Time and Region
--Second-level subtotals aggregating across Region and Department for each Time value
--A grand total row
----------------------------------------------------------------------------------------------
OUTPUT
20 rows selected
--------------------------------------------------------------------------------------------------
COMPARISION
--Regular aggregation rows that would be produced by GROUP BY without using ROLLUP
--First-level subtotals aggregating across Department for each combination of Time and Region
--Second-level subtotals aggregating across Region and Department for each Time value
--It does not produce a grand total row
---------------------------------------------------------------------------------------------------
CUBE EXAMPLES
OUTPUT
36 rows selected
OUTPUT
OUTPUT
21 rows selected
/
create table sales(time number(4),region varchar2(15),
dept varchar2(20),profit number(10,2))
/
insert into sales values(1996,'central','pen_sales',75000.00)
/
insert into sales values(1996,'central','book_sales',74000.00)
/
insert into sales values(1996,'east','pen_sales',89000.00)
/
insert into sales values(1996,'east','book_sales',115000.00)
/
insert into sales values(1996,'west','pen_sales',87000.00)
/
insert into sales values(1996,'west','book_sales',86000.00)
/
insert into sales values(1997,'central','pen_sales',82000.00)
/
insert into sales values(1997,'central','book_sales',85000.00)
/
insert into sales values(1997,'east','pen_sales',101000.00)
/
insert into sales values(1997,'east','book_sales',137000.00)
/
insert into sales values(1997,'west','pen_sales',96000.00)
/
insert into sales values(1997,'west','book_sales',97000.00)
/
==================================================================================
=========
SQL\Ch13
select e.ename,e.city,c.salary
from employee e , emp_company c
where e.ename=c.ename
/
select e.ename,city,salary
from employee e natural join emp_company c
/
select e.ename,e.city,c.salary
from employee e , emp_company c
/
select e.ename,city,salary
from employee e cross join emp_company c
/
select c.ename, m.mname,c.salary
from manager m , emp_company c
where c.ename=m.ename
/
select ename,mname,salary
from manager join emp_company using(ename)
where salary < 3000
/
select c.ename, m.mname,c.salary
from manager m , emp_company c
where c.ename=m.ename and salary > 3000
/
select c.ename, m.mname,c.salary
from manager m join emp_company c
on ( c.ename=m.ename and salary > 3000)
/
SELECT ENAME,MNAME,SHIFT
FROM MANAGER NATURAL JOIN EMP_SHIFT
/
SELECT M.ENAME,MNAME,SHIFT
FROM MANAGER M LEFT OUTER JOIN EMP_SHIFT S
ON (M.ENAME = S.ENAME)
/
SELECT M.ENAME,MNAME,SHIFT
FROM MANAGER M RIGHT OUTER JOIN EMP_SHIFT S
ON (M.ENAME = S.ENAME)
/
SELECT M.ENAME,MNAME,SHIFT
FROM MANAGER M FULL OUTER JOIN EMP_SHIFT S
ON (M.ENAME = S.ENAME)
/=================================================================================
SQL\Ch14
/
SELECT ENAME,SALARY,CNAME,(SELECT MAX(SALARY) FROM EMP_COMPANY R WHERE
R.CNAME=S.CNAME) MAX_SALARY
FROM EMP_COMPANY S
ORDER BY (SELECT MAX(SALARY) FROM EMP_COMPANY R WHERE R.CNAME=S.CNAME)
/
SQL> @SCALE1
7 rows selected.
SQL> @SCALE2
SQL> @SCALE3
7 rows selected.
SQL\Ch15
ENAME SHI
------------------------------ ---
ANIL A
SHANKAR B
C
D
SQL> @CASE1
ENAME SHIFT_NAM
------------------------------ ---------
ANIL A_SHIFT
SHANKAR B_SHIFT
C_SHIFT
ANY_SHIFT
SQL> @CASE2
ENAME SHIFT_NAME
------------------------------ -------------
ANIL MORNING SHIFT
SHANKAR MORNING SHIFT
EVENING_SHIFT
NIGHT_SHIFT
SQL> @CASE2
ENAME SHI
------------------------------ ---
ANIL A
SHANKAR B
C
D
SQL> @CASE3
ENAME NUL
------------------------------ ---
ANIL A
SHANKAR B
C
SQL> EDIT
Wrote file afiedt.buf
Table created.
1 row created.
SQL> /
Enter value for a: NULL
Enter value for b: 21
Enter value for c: 31
old 1: INSERT INTO TEST1 VALUES(&A,&B,&C)
new 1: INSERT INTO TEST1 VALUES(NULL,21,31)
1 row created.
SQL> /
Enter value for a: 12
Enter value for b: 22
Enter value for c: 32
old 1: INSERT INTO TEST1 VALUES(&A,&B,&C)
new 1: INSERT INTO TEST1 VALUES(12,22,32)
1 row created.
SQL> COMMIT;
Commit complete.
SQL> @CASE4
C RESULT
---------- ----------
30 10
31 21
32 12
1 row created.
SQL> COMMIT;
Commit complete.
SQL> @CASE4
C RESULT
---------- ----------
30 10
31 21
32 12
33 0
SQL\Ch16
insert all
when cname='ACC' then
into emp_acc values(ename,salary,jdate)
when cname='TATA' then
into emp_tata values (ename,salary,jdate)
when cname='CMC' and ename in (select ename from emp_shift where shift='A') then
into emp_cmc values (ename,salary,jdate)
select ename,cname,salary,jdate
from emp_company
/
insert all
when cname='ACC' then
into emp_acc values(ename,salary,jdate)
when cname='TATA' then
into emp_tata values (ename,salary,jdate)
when cname='CMC' and ename in (select ename from emp_shift where shift='A') then
into emp_cmc values (ename,salary,jdate)
select ename,cname,salary,jdate
from emp_company
/
create table emp_cmp_shift(ename varchar2(30),
cname varchar2(30),
shift char(3))
/
insert first
when cname='ACC' then
into emp_cmc values(ename,salary,jdate)
when ename='TATA' then
into emp_cmc values(ename,salary,jdate)
else
into emp_all values(ename,salary,jdate)
select ename,cname,salary,jdate
from emp_company
/
insert all
into emp_cmp_shift values(ename,cname,shift)
into emp_cmp_mgr values(ename,cname,mname)
select e.ename,cname,mname,shift
from emp_company e ,manager,emp_shift
where e.ename=manager.ename
and e.ename = emp_shift.ename
/
insert all
into emp_cmp_mgr values (ename,cname,'ASHOK')
into emp_cmp_mgr values (ename,cname,'ASHWIN')
select ename,cname
from emp_cmp_shift
/
merge into emp_all a
using emp_company b
on(a.ename=b.ename)
when matched then
update set a.salary = b.salary+100
when not matched then
insert (ename,salary,jdate)
values(ename,salary,jdate)
/
create table emp_acc as select ename,salary,jdate from emp_company
2 where rownum < 1;
Table created.
SQL> edit
Wrote file afiedt.buf
Table created.
SQL> edit
Wrote file afiedt.buf
Table created.
Table created.
SQL> create table emp_cmp_mgr(ename varchar2(10),cname varchar2(10),mname varchar2(10));
Table created.
SQL> @insert_all
select ename,cname,mname,shift
*
ERROR at line 4:
ORA-00904: invalid column name
SQL> @insert_all
select ename,cname,mname,shift
*
ERROR at line 4:
ORA-00918: column ambiguously defined
SQL> @insert_all
and emp_company.ename = emp_shift.ename
*
ERROR at line 7:
ORA-00904: invalid column name
SQL> @insert_all
into emp_cmp_mgr values(e.ename,cname,shift)
*
ERROR at line 3:
ORA-00904: invalid column name
SQL> @insert_all
into emp_cmp_shift values(ename,cname,shift)
*
ERROR at line 2:
ORA-01401: inserted value too large for column
SQL> desc emp_shift
Name Null? Type
----------------------------------------- -------- ----------------------------
ENAME VARCHAR2(30)
SHIFT CHAR(3)
ENAME SHI
------------------------------ ---
ANIL A
SHANKAR B
C
D
Table dropped.
SQL> @crtab_emp_sh
Table created.
SQL> @insert_all
4 rows created.
2 rows deleted.
2 rows deleted.
SQL> commit;
Commit complete.
SQL> @insert_all
4 rows created.
SQL> select * from emp_cmp_mgr;
SQL> commit;
Commit complete.
2 rows deleted.
SQL> commit;
Commit complete.
SQL> @insert_pivot
4 rows created.
SQL> commit;
Commit complete.
SQL> @cond_insert
into emp_cmp_acc values(ename,salary,jdate)
*
ERROR at line 3:
ORA-00905: missing keyword
SQL> @cond_insert
into emp_cmp_acc values(ename,salary,jdate)
*
ERROR at line 3:
ORA-00905: missing keyword
SQL> edit cond_insert
SQL> @cond_insert
when cname='CMC' and ename in (select ename from emp_shift where shift='A') then
*
ERROR at line 6:
ORA-00904: invalid column name
SQL> @cond_insert
into emp_cmp_cmc values (ename,salary,jdate)
*
ERROR at line 7:
ORA-00942: table or view does not exist
40 rows selected.
SQL>
SQL> edit cond_insert
SQL> @cond_insert
5 rows created.
Table created.
7 rows deleted.
SQL> commit;
Commit complete.
SQL> @insert_first
7 rows created.
SQL> commit;
Commit complete.
7 rows selected.
SQL> @merge
7 rows merged.
7 rows selected.
SQL> commit;
Commit complete.