Sei sulla pagina 1di 50

INSURANCE DATABASE

The primary keys are underlined and the data types are specified.

PERSON (did: String, name: string, address: string)


CAR (regid: string, model: string, year: int)
OWNS (did :string, regid:string)
ACCIDENT (repno: int, adate: date, location: string)
PARTICIPATED (did: string, regid:string, repno:int, damt:int)

Tasks:
(1) Create the above tables by properly specifying the primary keys and the foreign
keys.
(2) Enter at least five tuples for each relation.
(3) Demonstrate how you
a. Update the damage amount for the car with a specific regid in the accident
with report number 12 to 25000.
b. Add a new accident to the database.
(4) Find the total number of people who owned cars that were involved in accidents in
2008.
(5) Find the number of accidents in which cars belonging to a specific model were
involved.
(6) Find the total number of cars along with their details that are owned by a specific
person.
(7) Display the name of the person and model of the car that are met with an accident
along with the report-number and damage amount.

1)
SQL>create table person
(did varchar(10) primary key,
name varchar(10),
address varchar(20));

Table created
SQL>create table car
( regid varchar(10) primary key,
model varchar(10),
year int);

Table created

SQL>create table owns


(did varchar(10) references person,
regid varchar(10) references car,
primary key(did,regid));

Table created

SQL>create table accident


(repno int primary key,
adate date,
location varchar(10));

Table created

SQL>create table participated


(did varchar(10),
regid varchar(10),
repno int references accident,
damt int,
primary key(did,regid,repno),
foreign key(did,regid) references owns);

Table created
2)
SQL>insert into person values('p1','kiran','belgum');
1 row created
SQL>insert into person values('p2','naveen','hubli');
1 row created
SQL> insert into person values('p3','santosh','jkd');
1 row created
SQL> insert into person values('p4','amar','jkd');
1 row created
SQL> insert into person values('p5','amit','bjp');
1 row created

SQL>insert into car values('c1','alto',2008);


1 row created
SQL>insert into car values('c2','hundayi',2009);
1 row created
SQL>insert into car values('c3','alto',2008);
1 row created
SQL>insert into car values('c4','ford',2010);
1 row created
SQL>insert into car values('c5','ford',2010);
1 row created

SQL>insert into owns values('p1','c1');


1 row created
SQL>insert into owns values('p1','c2');
1 row created
SQL>insert into owns values('p2','c3');
1 row created
SQL>insert into owns values('p3','c4');
1 row created
SQL>insert into owns values('p4','c5');
1 row created
SQL>insert into accident values(11,'12-jul-2008','tumkur');
1 row created
SQL>insert into accident values(12,'24-sep-2009','dharwad');
1 row created
SQL>insert into accident values(13,'18-mar-2010','mangalore');
1 row created
SQL>insert into accident values(14,'20-aug-2008','tumkur');
1 row created
SQL>insert into accident values(15,'25-jan-2010','hubli');
1 row created

SQL>insert into participated values('p1','c1',11,10000);


1 row created
SQL>insert into participated values('p1','c1',12,8000);
1 row created
SQL>insert into participated values('p1','c2',13,12000);
1 row created
SQL>insert into participated values('p2','c3',14,3000);
1 row created
SQL>insert into participated values('p3','c4',15,3000);
1 row created

SQL>commit;
Commit complete

SQL>select * from person;


DID NAME ADDRESS
---------- ---------- --------------------
p1 kiran belgum
p2 naveen hubli
p3 santosh jkd
p4 amar jkd
p5 amit bjp
SQL>select * from car;
REGID MODEL YEAR
---------- ---------- ----------
c1 alto 2008
c2 hundayi 2009
c3 alto 2008
c4 ford 2010
c5 ford 2010

SQL>select * from owns;


DID REGID
---------- ----------
p1 c1
p1 c2
p2 c3
p3 c4
p4 c5

SQL>select * from accident;

REPNO ADATE LOCATION


---------- --------- ----------
11 12-JUL-08 tumkur
12 24-SEP-09 dharwad
13 18-MAR-10 mangalore
14 20-AUG-08 tumkur
15 25-JAN-10 hubli

SQL>select * from participated;


DID REGID REPNO DAMT
---------- ---------- ---------- ----------
p1 c1 11 10000
p1 c1 12 8000
p1 c2 13 12000
p2 c3 14 3000
p3 c4 15 6000
Queries
3)
a) SQL>update participated set damt=25000 where repno=12 and regid=’c1’;
1 row updated
b) SQL>insert into accident values(16,’23-aug-2009’,’tumkur’);
1 row created
SQL>insert into participated values(‘p1’,’c2’,16,16000);
1 row created

4)
SQL> select count(*) from participated p,accident a where a.repno=p.repno and adate
like ’%08’;

COUNT(*)
----------
2

5)
SQL> select count(*) from car c,participated p where c.regid=p.regid and model=’alto’;

COUNT(*)
----------
3

6)
SQL> select did,model,year,count(*) from owns o,car c where c.regid=o.regid and
did='p1' group by di
d,model,c.year;

DID MODEL YEAR COUNT(*)


---------- ---------- ---------- ----------
p1 alto 2008 1
p1 hundayi 2009 1
7)
SQL> select name,model,repno,damt from person p,car c,participated pa where
p.did=pa.did and c.regid=pa.regid;

NAME MODEL REPNO DAMT


---------- ---------- ---------- ---------- - -----------
kiran alto 11 10000
kiran alto 12 25000
kiran hundayi 13 12000
naveen alto 14 3000
santosh ford 15 6000
kiran hundayi 16 16000
ORDER PROCESSING DATABASE

Consider the following relations for an order processing database application in a


company.

CUSTOMER (cno: string , cname: string, city: string)


ORDERS (ono: string, odate: date, cno: int, ordamt: int)
ITEM (ino : string, iname : string, uprice: int)
ORDERITEMS (ono: string, ino: string, qty: int)
WAREHOUSE (wno: string, city: string)
SHIPMENT (ono: string, wno: string, sdate: date)

(1) Create the above tables by properly specifying the primary keys and the foreign
keys.
(2) Enter at least five tuples for each relation.
(3) Produce a listing: CUSTNAME, No.of orders, AVG_ORDER_AMT, where the middle
column is the total numbers of orders by the customer and the last column is the
average order amount for that customer.
(4) List the orderno for orders that were shipped from all the warehouses that the
company has in a specific city.
(5) Demonstrate the deletion of an item from the ITEM table and demonstrate a
method of handling the rows in the ORDERITEMS table that contain this particular
item.
(6) Raise the price of all the items by 15%.
(7) Display customer name, Items ordered by him along with Item-number,
ordernumber,order-amount and Warehouse-city.

1)
SQL> create table customer
(cno varchar(10) primary key,
cname varchar(10),
city varchar(10));

Table created.
SQL> create table orders
(ono varchar(10) primary key,
odate date,
cno varchar(10) references customer,
ordamt int);

Table created.

SQL> create table item


(ino varchar(10) primary key,
iname varchar(10),
uprice int);

Table created.

SQL> create table orderitems


(ono varchar(10) references orders on delete cascade,
ino varchar(10) references item on delete cascade,
qty int);

Table created.

SQL> create table warehouse


(wno varchar(10) primary key,
city varchar(10));

Table created.

SQL> create table shipment


(ono varchar(10) references orders,
wno varchar(10) references warehouse,
sdate date,
primary key(ono,wno));

Table created.
SQL> insert into customer values('c1','praveen','jkd');
1 row created.

SQL> insert into customer values('c2','manoj','jkd');


1 row created.

SQL> insert into customer values('c3','vinayak','jkd');


1 row created.

SQL> insert into customer values('c4','santosh','jkd');


1 row created.

SQL> insert into customer values('c5','amit','jkd');


1 row created.

SQL> insert into orders values('o1','12-jul-2006','c1',10000);


1 row created.

SQL> insert into orders values('o2','14-jul-2006','c1',1000);


1 row created.

SQL> insert into orders values('o3','16-jul-2006','c1',500);


1 row created.

SQL> insert into orders values('o4','18-jul-2006','c2',1500);


1 row created.

SQL> insert into orders values('o5','20-jul-2006','c3',20000);


1 row created.
SQL> insert into item values('i1','mobile',10000);
1 row created.

SQL> insert into item values('i2','watch',500);


1 row created.

SQL> insert into item values('i3','bag',100);


1 row created.

SQL> insert into item values('i4','book',120);


1 row created.

SQL>insert into item values('i5','cap',120);


1 row created.

SQL> insert into orderitems values('o1','i1',1);


1 row created.

SQL> insert into orderitems values('o2','i2',2);


1 row created.

SQL> insert into orderitems values('o3','i3',5);


1 row created.

SQL> insert into orderitems values('o4','i2',3);


1 row created.

SQL> insert into orderitems values('o5','i1',2);


1 row created.

SQL> insert into warehouse values('w1','bangalore');


1 row created.

SQL> insert into warehouse values('w2','hubli');


1 row created.
SQL> insert into warehouse values('w3','delhi');
1 row created.

SQL> insert into warehouse values('w4','pune');


1 row created.

SQL> insert into warehouse values('w5','mumbai');


1 row created.

SQL> insert into shipment values('o1','w1','14-jul-2006');


1 row created.

SQL> insert into shipment values('o2','w1','16-jul-2006');


1 row created.

SQL> insert into shipment values('o3','w2','18-jul-2006');


1 row created.

SQL> insert into shipment values('o4','w1','20-jul-2006');


1 row created.

SQL> insert into shipment values('o5','w2','22-jul-2006');


1 row created.

SQL> commit;
Commit complete.

SQL> select * from customer;

CNO CNAME CITY


---------- ---------- ----------
c1 praveen jkd
c2 manoj jkd
c3 vinayak jkd
c4 santosh jkd
c5 amit jkd
SQL> select * from orders;

ONO ODATE CNO ORDAMT


---------- --------- ---------- --------------------
o1 12-JUL-06 c1 10000
o2 14-JUL-06 c1 1000
o3 16-JUL-06 c1 500
o4 18-JUL-06 c2 1500
o5 20-JUL-06 c3 20000

SQL> select * from item;

INO INAME UPRICE


---------- ---------- ----------
i1 mobile 10000
i2 watch 500
i3 bag 100
i4 book 120
i5 cap 120

SQL> select * from orderitems;

ONO INO QTY


---------- ---------- ----------
o1 i1 1
o2 i2 2
o3 i3 5
o4 i2 3
o5 i1 2
SQL> select * from warehouse;
WNO CITY
---------- ----------
w1 bangalore
w2 hubli
w3 delhi
w4 pune
w5 mumbai

SQL> select * from shipment;

ONO WNO SDATE


---------- ---------- ---------
o1 w1 14-JUL-06
o2 w1 16-JUL-06
o3 w2 18-JUL-06
o4 w1 20-JUL-06
o5 w2 22-JUL-06

Queries

3)
SQL> select cname,count(*),avg(ordamt) from customer c,orders o where c.cno=o.cno
group by cname;

CNAME COUNT(*) AVG(ORDAMT)


---------- ---------- -------------------------------
praveen 3 3833.33333
vinayak 1 20000
manoj 1 1500
4)
SQL> select ono,w.wno,city,sdate from shipment s,warehouse w where s.wno=w.wno
and city='bangalore';

ONO WNO CITY SDATE


---------- ---------- ---------- ---------
o1 w1 bangalore 14-JUL-06
o2 w1 bangalore 16-JUL-06
o4 w1 bangalore 20-JUL-06

5)
SQL> delete from item where ino='i1';
1 row deleted.

6)
SQL> update item set uprice=uprice+uprice*15/100;
4 rows updated.
SQL> select * from item;

INO INAME UPRICE


---------- ---------- ----------
i2 watch 575
i3 bag 115
i4 book 138
i5 cap 138
7)
SQL> select cname,ino,ordamt,w.city from customer c,orders o,orderitems
oi,warehouse w,shipment s where c.cno=o.cno and oi.ono=o.ono and s.wno=w.wno
and o.ono=s.ono and oi.ono=s.ono;

CNAME INO ORDAMT CITY


---------- ---------- ---------- ----------------------
praveen i2 1000 bangalore
praveen i3 500 hubli
manoj i2 1500 bangalore
STUDENT ENROLMENT AND BOOK ADOPTION DATABASE

Consider the following database of student enrolment in courses & books adopted for
each course.

STUDENT (regno: string, sname: string, major: string, bdate:date)


COURSE (cno:int, cname:string, dept:string)
ENROLL ( regno:string, cno:int, sem:int, marks:int)
TEXT(bisbn:string, btitle:string, publisher:string, author:string)
BOOKADOPTION (cno :int, sem:int, bisbn:string)

(1) Create the above tables by properly specifying the primary keys and the foreign
keys.
(2) Enter at least five tuples for each relation.
(3) Demonstrate how you add a new text book to the database and make this book be
adopted by some department.
(4) Produce a list of text books (include cno, bisbn, btitle) in the alphabetical order for
courses offered by the ‘CS’ department that use more than two books.
(5) List any department that has all its adopted books published by a specific publisher.
(6) List the Students enrolled for a particular course.
(7) Delete a particular course and also delete the corresponding entries from the Enroll
and Bookadoption table.

1)
SQL>create table student
(regno varchar(10) primary key,
sname varchar(10),
major varchar(10),
bdate date);

create table course


(cno number(5) primary key,
cname varchar(10),
dept varchar(10));
create table enroll
(regno varchar(10),
cno number(5),
sem number(3),
marks number(5),
primary key(regno,cno),
foreign key(regno) references student on delete cascade,
foreign key(cno) references course on delete cascade);

create table text


(bisbn varchar(10) primary key,
btitle varchar(10),
publisher varchar(10),
author varchar(10));

create table bookadoption


(cno number(5),
sem number(3),
bisbn varchar(10),
primary key(cno,bisbn),
foreign key(cno) references course on delete cascade,
foreign key(bisbn) references text on delete cascade);

2)
insert into student values('s1','sonu','cs','15-aug-1998');
insert into student values('s2','deepa','cs','21-may-1997');
insert into student values('s3','priya','cs','29-sep-1996');
insert into student values('s4','anish','phy','15-apr-1998');
insert into student values('s5','sonu','phy','14-nov-1998');
insert into course values(11,'bca','cs');
insert into course values(22,'bcs','cs');
insert into course values(33,'bsc','phy');
insert into course values(44,'ba','arts');
insert into course values(55,'bba','comm');

insert into enroll values('s1',11,1,500);


insert into enroll values('s2',11,3,500);
insert into enroll values('s3',22,5,600);
insert into enroll values('s4',33,1,500);
insert into enroll values('s5',33,1,500);

insert into text values('b1','cprog','pearson','kotur');


insert into text values('b2','cpp','pearson','balguru');
insert into text values('b3','java','pearson','balguru');
insert into text values('b4','dbms','pearson','navathe');
insert into text values('b5','vb','pearson','rajguru');

insert into bookadoption values(11,1,'b1');


insert into bookadoption values(11,3,'b2');
insert into bookadoption values(11,5,'b3');
insert into bookadoption values(22,5,'b4');
insert into bookadoption values(22,3,'b5');

SQL> select * from student;

REGNO SNAME MAJOR BDATE


---------- ---------- ---------- ---------
s1 sonu cs 15-AUG-98
s2 deepa cs 21-MAY-97
s3 priya cs 29-SEP-96
s4 anish phy 15-APR-98
s5 sonu phy 14-NOV-98

SQL> select * from course;

CNO CNAME DEPT


---------- ----------------- ----------
11 bca cs
22 bcs cs
33 bsc phy
44 ba arts
55 bba comm

SQL> select * from enroll;

REGNO CNO SEM MARKS


---------- ---------- ---------- ----------
s1 11 1 500
s2 11 3 500
s3 22 5 600
s4 33 1 500
s5 33 1 500

SQL> select * from text;

BISBN BTITLE PUBLISHER AUTHOR


---------- ---------- ---------- ---------------------
b1 cprog pearson kotur
b2 cpp pearson balguru
b3 java pearson balguru
b4 dbms pearson navathe
b5 vb pearson rajguru
b6 se pearson ravi
SQL> select * from bookadoption;

CNO SEM BISBN


---------- ---------- ----------
11 1 b1
11 3 b2
11 5 b3
22 5 b4
22 3 b5

Queries

3)
SQL> insert into text values('b6','se','pearson','ravi');
insert into bookadoption values(22,3,'b6');

4)
SQL> select b.cno,t.bisbn,btitle,cname from bookadoption b,text t,course c where
t.bisbn=b.bisbn and b.cno=c.cno and b.cno in(select b.cno from bookadoption b
group by b.cno having count(*)>2) and c.dept='cs' order by cname;

CNO BISBN BTITLE CNAME


------ ---------- ---------- --------
11 b1 cprog bca
11 b2 cpp bca
11 b3 java bca
22 b4 dbms bcs
22 b5 vb bcs
22 b6 se bcs
5)
SQL> select distinct c.dept,t.publisher from text t,bookadoption b, course c
where t.bisbn=b.bisbn and c.cno=b.cno and c.dept in(select c.dept from text t,course c,
bookadoption b where b.bisbn=t.bisbn and c.cno=b.cno group by c.dept having
count(distinct publisher)=1) and publisher like 'pearson';

DEPT PUBLISHER
---------- -----------------
cs pearson

6)
SQL> select s.regno,sname,c.cno,cname from student s,course c,enroll e where
s.regno=e.regno and c.cno=e.cno and cname='bca';

REGNO SNAME CNO CNAME


---------- ---------- ---------- ---------
s1 sonu 11 bca
s2 deepa 11 bca

7)
SQL> delete from course where cname='ba';
1 row deleted.

SQL> select * from course;


CNO CNAME DEPT
---------- ---------- ----------
11 bca cs
22 bcs cs
33 bsc phy
55 bba comm
BOOK DEALER DATABASE

The following tables are maintained by a book dealer.

AUTHOR (aid:string, name:string, city:string, country:string)


PUBLISHER (pid:string, name:string, city:string, country:string)
CATEGORY (cid:string, des:string)
CATALOGS (bid:string, title:string, aid:string, pid:string, cid:string, year:int, price:int)
ORDERDETAILS (ono:string, bid:string, qty:int)

(1) Create the above tables by properly specifying the primary keys and the foreign
keys.
(2) Enter at least five tuples for each relation.
(3) Give the details of the authors who have 2 or more books in the catalog and the
price of the books is greater than the average price of the books in the catalog and the
year of publication is after 2000.
(4) Find the author of the book which has maximum sales.
(5) Demonstrate how you increase the price of books published by a specific publisher
by 10%.
(6) List the Books for which no orders have been placed.
(7) Display title, Name of the author, Publisher Name along with the Year of publication
and Price.

1)
SQL> create table author
(aid varchar(10) primary key,
name varchar(10),
city varchar(10),
country varchar(10));

Table created.

SQL> create table publisher


(pid varchar(10) primary key,
name varchar(10),
city varchar(10),
country varchar(10));
Table created.

SQL> create table category


(cid varchar(10) primary key,
des varchar(20));

Table created.

SQL> create table catalogs


(bid varchar(10) primary key,
title varchar(10),
aid varchar(10) references author,
pid varchar(10) references publisher,
cid varchar(10) references category,
year number(5),
price number(5));

Table created.

SQL> create table orderdetails


(ono varchar(10) primary key,
bid varchar(10) references catalogs,
qty number(5));

Table created.

2)
insert into author values('a1','praveen','jkd','india');
insert into author values('a2','kiran','bijapur','india');
insert into author values('a3','manoj','hubli','india');
insert into author values('a4','santosh','jkd','india');
insert into author values('a2','amit','bijapur','india');

insert into publisher values('p1','pearson','newyork','us');


insert into publisher values('p2','sapnabooks','bangalore','india');
insert into publisher values('p3','bpb','delhi','india');
insert into publisher values('p4','ebpb','bangalore','india');
insert into publisher values('p5','upkars','hubli','india');

insert into category values('c1','education');


insert into category values('c2','magzines');
insert into category values('c3','sports');
insert into category values('c2','spiritual');
insert into category values('c1','travel');

insert into catalogs values('b1','cprog','a1','p1','c1',2010,700);


insert into catalogs values('b2','cpp','a1','p1','c1',2005,500);
insert into catalogs values('b3','java','a1','p1','c1',2012,600);
insert into catalogs values('b4','science','a2','p2','c2',2011,500);
insert into catalogs values('b5','physics','a3','p1','c1',2013,250);

insert into orderdetails values('o1','b1',2);


insert into orderdetails values('o2','b2',1);
insert into orderdetails values('o3','b3',3);
insert into orderdetails values('o4','b4',4);
insert into orderdetails values('o5','b1',1);

SQL> commit;
Commit complete.

Queries

3)
SQL> select a.aid,name from author a,catalogs c where a.aid=c.aid and year>2000 and
price>(select avg(price) from catalogs) group by a.aid,name having count(*)>1;

AID NAME
---------- ----------
a1 praveen
4)
SQL> select name from author where aid in(select aid from catalogs where bid in(select
bid from orderdetails group by bid having sum(qty) in(select max(sum(qty)) from
orderdetails group by bid)));

NAME
---------
kiran

5)
SQL> update catalogs set price=price+price*10/100 where pid=(select pid from
publisher where name=’pearson’);

4 rows updated.

SQL> commit;
Commit complete.

SQL> select * from catalogs;

BID TITLE AID PID CID YEAR PRICE


---------- ---------- ---------- ---------- ---------- ---------- ----------
b1 cprog a1 p1 c1 2010 770
b2 cpp a1 p1 c1 2005 550
b3 java a1 p1 c1 2012 660
b4 science a2 p2 c2 2011 500
b5 physics a3 p1 c1 2013 275

6)
SQL> select bid from catalogs minus select bid from orderdetails;

BID
----------
b5
7)
SQL> select title,a.name,p.name,year,price from author a,publisher p,catalogs c where
a.aid=c.aid and p.pid=c.pid;

TITLE NAME NAME YEAR PRICE


---------- ---------- ---------- ---------- ----------
physics manoj pearson 2013 275
java praveen pearson 2012 660
cpp praveen pearson 2005 550
cprog praveen pearson 2010 770
science kiran sapnabooks 2011 500
BANKING ENTERPRISE DATABASE

Consider the following database for a banking enterprise

BRANCH(branchname:string, branchcity:string, assets:int)


ACCOUNT(accno:int, branchname:string, balance:int)
CUSTOMER(customername:string, customerstreet:string, customercity:string)
DEPOSITOR(customername:string, accno:int)
LOAN(loannumber:int, branchname:string, amount:int)
BORROWER(customername:string, loannumber:int)

(1) Create the above tables by properly specifying the primary keys and the foreign keys
(2) Enter at least five tuples for each relation
(3) Find all the customers who have at least two accounts at the main branch(choose
one of the branch as main branch).
(4) Find all the customers who have an account at all the branches located in a specific
city.
(5) Display all the accounts in a specific branch along with Customer details.
(6) Display the Loan details of a specific customer.
(7) Demonstrate how you delete all account tuples at every branch located in a specific
city.

1)
SQL> create table branch
(branchname varchar(10) primary key,
branchcity varchar(15),
assets int);

Table created.

SQL> create table account


(accno int primary key,
branchname varchar(10) references branch,
balance int);

Table created.
SQL> create table customer
(customername varchar(10) primary key,
customerstreet varchar(10),
customercity varchar(10));

Table created.

SQL> create table depositor


(customername varchar(10),
accno int references account,
primary key(customername,accno),
foreign key(customername) references customer on delete cascade,
foreign key(accno) references account on delete cascade);

Table created.

SQL> create table loan


(loannumber int primary key,
branchname varchar(10) references branch,
amount int);

Table created.

SQL> create table borrower


(customername varchar(10) references customer,
loannumber int references loan,
primary key(customername,loannumber));

Table created.
2)
insert into branch values('krmarket','bangalore',10000);
insert into branch values('jkd','jamkhandi',10000);
insert into branch values('whitefield','bangalore',10000);
insert into branch values('jaynagar','hubli',10000);
insert into branch values('sainagar','dharwad',10000);

insert into account values(1,'krmarket',500);


insert into account values(2,'krmarket',500);
insert into account values(3,'whitefield',500);
insert into account values(5,'krmarket',500);
insert into account values(4,'jkd',500);

insert into customer values('praveen','vijaynagar','bangalore');


insert into customer values('vivek','vidyanagar','hubli');
insert into customer values('srikanth','rameshwar','jamkhandi');
insert into customer values('santosh','shrinagar','belagavi');
insert into customer values('amit','ringroad','bangalore');

insert into depositor values('praveen',1);


insert into depositor values('praveen',2);
insert into depositor values('praveen',3);
insert into depositor values('vivek',4);
insert into depositor values('srikanth',5);

insert into loan values(11,'krmarket',500);


insert into loan values(12,'krmarket',500);
insert into loan values(13,'whitefield',500);
insert into loan values(14,'jkd',500);
insert into loan values(15,'krmarket',500);
insert into borrower values('praveen',11);
insert into borrower values('praveen',12);
insert into borrower values('vivek',13);
insert into borrower values('srikanth',15);
insert into borrower values('vivek',14);

SQL> commit;
Commit complete.

Queries :
Note that in this database ‘krmarket’ branch is assumed as main branch

3)
SQL> select d.customername from depositor d,account a where a.accno=d.accno and
a.branchname= 'krmarket' group by d.customername having count(*)>=2;

CUSTOMERNAME
----------
praveen

4)
SQL> select d.customername from account a,depositor d,branch b where
d.accno=a.accno and a.branchname=b.branchname and b.branchcity='bangalore' group
by d.customername having(count(distinct b.branchname))=(select count(branchname)
from branch where branchcity='bangalore');

CUSTOMERNAME
----------
praveen
5)
SQL> select a.accno,c.customername,customercity from account a,customer
c,depositor d where a.accno=d.accno and d.customername=c.customername and
branchname='krmarket';

ACCNO CUSTOMERNAME CUSTOMERCITY


---------- ---------- ----------
1 praveen bangalore
2 praveen bangalore
5 srikanth jamkhandi

6)
SQL> select l.loannumber,customername,branchname,amount from loan l,borrower b
where l.loannumber= b.loannumber and customername='praveen';

LOANNUMBER CUSTOMERNAME BRANCHNAME AMOUNT


---------- ---------- ---------- ----------
11 praveen krmarket 500
12 praveen krmarket 500

7)
SQL> delete from account where branchname in(select branchname from branch where
branchcity='bangalore');

4 rows deleted.
EMPLOYEE DETAILS
employee(eno:int, ename:string, ecity:string, gender:string, esal:int)

1) Create the above table by specifying primary key.


2) Insert atleast five rows.
3) Display all female employees.
4) Display employees of specific city.
5) Raise the employee salary by 20% and display the updated table.
6) Display city wise number of employees.
7) Delete the employee records of a specific city and display the table after deletion.

1)
SQL> create table employee
(eno int primary key,
ename varchar(20),
ecity varchar(20),
gender varchar(20),
esal int);

Table created.

2)
SQL> insert into employee values(1,'praveen','hubli','male',15000);

1 row created.

SQL> insert into employee values(2,'santosh','hubli','male',12000);

1 row created.

SQL> insert into employee values(3,'vittal','pune','male',20000);

1 row created.
SQL> insert into employee values(4,'shruti','pune','female',15000);

1 row created.

SQL> insert into employee values(5,'savita','hubli','female',20000);

1 row created.

SQL> commit;
Commit complete.

3)
SQL> select * from employee where gender='female';

ENO ENAME ECITY GENDER ESAL


---------- -------------------- -------------------- -------------------- ----------
4 shruti pune female 15000
5 savita hubli female 20000

4)
SQL> select * from employee where ecity='pune';

ENO ENAME ECITY GENDER ESAL


---------- -------------------- -------------------- -------------------- ----------
3 vittal pune male 20000
4 shruti pune female 15000
5)
SQL> update employee set esal=esal+esal*20/100;

5 rows updated.

SQL> select * from employee;

ENO ENAME ECITY GENDER ESAL


---------- -------------------- -------------------- -------------------- ----------
1 praveen hubli male 18000
2 santosh hubli male 14400
3 vittal pune male 24000
4 shruti pune female 18000
5 savita hubli female 24000

SQL> commit;

Commit complete.

6)
SQL> select ecity,count(*) from employee group by ecity;

ECITY COUNT(*)
-------------------- ----------
pune 2
hubli 3

7)
SQL> delete from employee where ecity='hubli';

3 rows deleted.
SQL> select * from employee;

ENO ENAME ECITY GENDER ESAL


---------- -------------------- -------------------- -------------------- ----------
3 vittal pune male 24000
4 shruti pune female 18000

SQL> commit;

Commit complete.

Column Descriptions

eno – employee number,


ename- employee name,
gender- gender of an employee,
esal-salary of an employee
ecity-city of an employee.
STUDENT DETAILS
student(regno:string, name:string, sub1:int, sub2:int, sub3:int)

1) Create the above table by specifying primary key.


2) Insert at least five tuples .
3) Display regno, name, totalmarks and average of all students.
4) Display the regno, name of student who has secured highest in sub1.
5) Display name, sub2 between 60 to 80 in ascending order.
6) Update the sub1 by 10 of a specific student regno and display the table.
7) Display regno, name, sub1 where sub1 are 77 55 45.

1)
SQL> create table student
(regno varchar(10) primary key,
name varchar(10),
sub1 int,
sub2 int,
sub3 int);

Table created.

2)
SQL> insert into student values('m1','ravi',56,78,87);
1 row created.

SQL> insert into student values('m2','shiva',77,85,56);


1 row created.

SQL> insert into student values('m3','amit',45,97,72);


1 row created.
SQL> insert into student values('m4','deepa',55,77,88);
1 row created.

SQL> insert into student values('m5','raju',45,75,89);


1 row created.

3)
SQL> select regno,name,sub1+sub2+sub3 as totalmarks,(sub1+sub2+sub3)/3 as average
from student;

REGNO NAME TOTALMARKS AVERAGE


---------- ---------- ---------- -----------------------------------
m1 ravi 221 73.6666667
m2 shiva 218 72.6666667
m3 amit 214 71.3333333
m4 deepa 220 73.3333333
m5 raju 209 69.6666667

4)
SQL> select regno, name from student where sub1=(select max(sub1) from student);

REGNO NAME
---------- ----------------
m2 shiva

SQL> select * from student;

REGNO NAME SUB1 SUB2 SUB3


---------- ---------- ---------- -------------------------- ----------
m1 ravi 56 78 87
m2 shiva 77 85 56
m3 amit 45 97 72
m4 deepa 55 77 88
m5 raju 45 75 89
5)
SQL> select name,sub2 from student where sub2 between 60 and 80 order by name;

NAME SUB2
---------- ----------
deepa 77
raju 75
ravi 78

6)
SQL> update student set sub1=sub1+10 where regno='m1';

1 row updated.

SQL> select * from student;

REGNO NAME SUB1 SUB2 SUB3


---------- ---------- ---------- ---------- ----------
m1 ravi 66 78 87
m2 shiva 77 85 56
m3 amit 45 97 72
m4 deepa 55 77 88
m5 raju 45 75 89

SQL> commit;

Commit complete.
7)
SQL> select regno,name,sub1 from student where sub1 in(77,55,45);

REGNO NAME SUB1


---------- ---------- -------------------
m2 shiva 77
m3 amit 45
m4 deepa 55
m5 raju 45

Column Descriptions

regno- register number of student


Name- name of student
sub1- subject 1 marks
sub2- subject 2 marks
sub3- subject 3 marks
SHOPPING DETAILS

shopping(orderno:int, cname:string, odate:date, esite:string, amount:int)

1) Create the above table by specifying primary key.


2) Insert atleast 5 records(tuples) and display the table.
3) Display orderno, cname, amount whose amount is greater than 5000.
4) Display customerwise number of orders.
5) Decrease amount by 5% and display the table.
6) Display the orderno, odate, amount where orders placed in 2011.
7) Find the number of orders placed in flipkart site.

1)
SQL> create table shopping
(orderno int,
cname varchar(10),
odate date,
esite varchar(10),
amount int);

Table created.

2)
SQL> insert into shopping values(1,'praveen','20-jan-2011','flipkart',8000);

1 row created.

SQL> insert into shopping values(2,'praveen','15-dec-2011','flipkart',7000);

1 row created.
SQL> insert into shopping values(3,'santosh','11-jul-2010','amazon',3000);

1 row created.

SQL> insert into shopping values(4,'pavan','23-aug-2010','snapdeal',2000);

1 row created.

SQL> insert into shopping values(5,'raj','18-mar-2012','flipkart',9000);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from shopping;


ORDERNO CNAME ODATE ESITE AMOUNT
---------- ---------- --------- ---------- ---------------------------------------------
1 praveen 20-JAN-11 flipkart 8000
2 praveen 15-DEC-11 flipkart 7000
3 santosh 11-JUL-10 amazon 3000
4 pavan 23-AUG-10 snapdeal 2000
5 raj 18-MAR-12 flipkart 9000

3)
SQL> select orderno,cname,amount from shopping where amount>5000;

ORDERNO CNAME AMOUNT


---------- ---------- -------------------------
1 praveen 8000
2 praveen 7000
5 raj 9000
4)
SQL> select cname,count(*) from shopping group by cname;

CNAME COUNT(*)
---------- ----------
praveen 2
santosh 1
raj 1
pavan 1

5)
SQL> update shopping set amount=amount-amount*5/100;

5 rows updated.

SQL> select * from shopping;

ORDERNO CNAME ODATE ESITE AMOUNT


---------- ---------- --------- ---------- -------------------------------------------
1 praveen 20-JAN-11 flipkart 7600
2 praveen 15-DEC-11 flipkart 6650
3 santosh 11-JUL-10 amazon 2850
4 pavan 23-AUG-10 snapdeal 1900
5 raj 18-MAR-12 flipkart 8550

SQL> commit;

Commit complete.
6)
SQL> select orderno,odate,amount from shopping where odate like '%11';

ORDERNO ODATE AMOUNT


---------- --------- ------------------------------------
1 20-JAN-11 7600
2 15-DEC-11 6650

7)
SQL> select count(*) from shopping where esite='flipkart';

COUNT(*)
----------
3

SQL> commit;

Commit complete.

Column Descriptions

orderno - order number


Cname - customer name
Odate - order date
Esite – website
Amount - Total amount
ELECTRICITY BILL

EBill (billno:string, bdate:date, cname:string, mread:int, rate:int)

1) Create the above table by specifying primary key.


2) Insert at least five records.
3) Display billno, cname, totalbill of all customers.
4) Raise the rate of all bills by 10% and display table.
5) Display cname, totalbill of previously paid bill of particular customer.
6) Find the customer whose meter reading is minimum.
7) Display monthwise total meter readings.

1)
SQL> create table EBill
(billno varchar(10) primary key,
bdate date,
cname varchar(10),
mread int,
rate int);

Table created.

2)
SQL> insert into EBill values('b1','12-aug-2008','praveen',150,10);
1 row created.

SQL> insert into EBill values('b2','24-aug-2008','praveen',120,10);


1 row created.

SQL> insert into EBill values('b3','16-feb-2008','naveen',130,11);


1 row created.

SQL> insert into EBill values('b4','25-feb-2008','kiran',110,13);


1 row created.

SQL> insert into EBill values('b5','18-aug-2008','naveen',140,14);


1 row created.

SQL> commit;
Commit complete.

3)
SQL> select billno,cname,mread*rate as totalbill from EBill;

BILLNO CNAME TOTALBILL


---------- ---------- ------------------------
b1 praveen 1650
b2 praveen 1320
b3 naveen 1560
b4 kiran 1540
b5 naveen 2100

4)
SQL> update EBill set rate=rate+rate*10/100;

5 rows updated.

SQL> select * from EBill;

BILLNO BDATE CNAME MREAD RATE


---------- --------- ---------- ---------- --------------------------------
b1 12-AUG-08 praveen 150 11
b2 24-AUG-08 praveen 120 11
b3 16-FEB-08 naveen 130 12
b4 25-FEB-08 kiran 110 14
b5 18-AUG-08 naveen 140 15
SQL> commit;

Commit complete.

5)
SQL> select cname,sum(mread*rate) from EBill where cname='praveen' group by
cname;

CNAME SUM(MREAD*RATE)
---------- -------------------------------
praveen 2700

6)
SQL> select cname,mread from EBill where mread=(select min(mread) from EBill);

CNAME MREAD
---------- ---------------
kiran 110

7)
SQL> select to_char(bdate,'mon'),sum(mread) from EBill group by
to_char(bdate,'mon');

TO SUM(MREAD)
--- ------------------------
feb 240
aug 410

Column Descriptions

billno - bill number


Bdate – bill date
Cname – customer name
mread – meter reading
rate – rate per unit
RESERVATION DETAILS

Reserve (ticketno:int, rdate:date, npass:int, rate:int)

1) Create the above table specifying primary key.


2) Insert 5 records.
3) Raise the rate by 2% and display the table.
4) Calculate amount by using npass*rate.
5) Cancel the tickets whose rate is lowest and display the table.
6) Display ticketno, rdate in ascending order by date.
7) Display sum of all ticket amounts monthwise.

1)
SQL> create table Reserve
(ticketno int primary key,
rdate date,
npass int,
rate int);

Table created.

2)
SQL> insert into Reserve values(1,'12-jan-2015',2,100);
1 row created.

SQL> insert into Reserve values(2,'23-jan-2015',1,300);


1 row created.

SQL> insert into Reserve values(3,'28-dec-2015',2,200);


1 row created.
SQL> insert into Reserve values(4,'05-feb-2015',1,400);
1 row created.

SQL> insert into Reserve values(5,'22-feb-2015',3,100);


1 row created.

SQL> commit;
Commit complete.

3)
SQL> update Reserve set rate=rate+rate*2/100;
5 rows updated.

SQL> select * from Reserve;

TICKETNO RDATE NPASS RATE


---------- --------- ---------- ----------------------------------
1 12-JAN-15 2 102
2 23-JAN-15 1 306
3 28-DEC-15 2 204
4 05-FEB-15 1 408
5 22-FEB-15 3 102

4)
SQL> select ticketno,npass*rate as amount from Reserve;

TICKETNO AMOUNT
---------- ----------
1 204
2 306
3 408
4 408
5 306

5)
SQL> Delete from Reserve where rate=(select min(rate) from Reserve);
2 rows deleted.
SQL> select * from Reserve;

TICKETNO RDATE NPASS RATE


---------- --------- ---------- -------------------------------
2 23-JAN-15 1 306
3 28-DEC-15 2 204
4 05-FEB-15 1 408

SQL> commit;
Commit complete.

6)
SQL> select ticketno,rdate from Reserve order by rdate;

TICKETNO RDATE
---------- ---------------------------------
2 23-JAN-15
4 05-FEB-15
3 28-DEC-15

7)
SQL> select to_char(rdate,'mon'),sum(npass*rate) as amount from Reserve group by
to_char(rdate,'mon');

TO_ AMOUNT
--- ----------------------
jan 306
feb 408
dec 408

Column Descriptions

ticketno – ticket number


rdate – reservation date
npass – number of passangers
rate – ticket rate