Sei sulla pagina 1di 24

CHAPTER 1

INSURANCE DATABASE
Consider the Insurance database given below:
PERSON (driver id: String, name: string, address: string)
CAR (Regno: string, model: string, year: int)
ACCIDENT (report-number: int, date: date, location: string)
OWNS (driver-id: string, Regno: string)
PARTICIPATED (driver-id: string, Regno: string, report-number:int, damage amount:int)
ER-Diagram:

Driver-id

Name

Regno

N
PERSON

Model

N
OWNS

CAR

Address

Year
PARTICIPATED

N
ACCIDENT

Report-number

Date

i) Create tables without primary key and foreign key.

SQL>create table person (driver_id varchar(20), name varchar(20),address varchar(20));


SQL>create table car (reg_no varchar(20), model varchar(20),year number(10));
SQL>create table accident (report_no number(10),ddate date, location varchar(20));
SQL>create table owns (driver_id varchar(20),reg_no varchar (20));
SQL>create table participated (driver_id varchar(20),reg_no varchar(20),report_no
number(10),damage_amount number(10));
ii) Alter tables with ALTER TABLE option to add primary key, foreign key and
unique constraints.
SQL>alter table person add(primary key(driver_id));
SQL>alter table car add(primary key(reg_no));
SQL>alter table accident add(primary key(report_no));
SQL>alter table owns(primary key(driver_id,reg_no));
SQL>alter table owns(foreign key(driver_id)references person(driver_id),foreign
key(reg_no)references car(reg_no));
SQL>alter table participated add(primary key(driver_id,reg_no,report_no));
SQL>alter table participated add(foreign key(driver_id)references
person(driver_id),foreign key(reg_no)references car(reg_no),foreign
SQL>key(report_no)references accident(report_no));
iii) Demonstrate "create table from select" (NOTE: with AS)
SQL> create table newcar AS select * from car;
Table created.
SQL> desc newcar;
Name
-------------REG_NO
MODEL
YEAR

Null?
----------

Type
------------------VARCHAR2(20)
VARCHAR2(20)
NUMBER(10)

iv) Enter at least 5 tuples for each relation.


SQL> insert into person values('&driver_id','&name','&address');
SQL> insert into car values('&reg_no','&model',&year);
SQL> insert into accident values(&report_no,'&date','&location');
SQL> insert into owns values('&driver_id','&reg_no');
SQL> insert into participated
values('&driver_id','&reg_no',&report_no,&damage_amount);
v) With the help of above tables, demonstrate group by, order by and having with
group functions with examples.

a) Count the number of cars for each model.


SQL> select model, count(*)
from car
group by model;
MODEL
------------alto
ferrari
ford
maruthi
safari

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

b) List all models of a car in ascending order.


SQL> select model
from car
order by model;
MODEL
---------alto
alto
ferrari
ferrari
ford
maruthi
maruthi
safari
c) List the cars named as alto
SQL> select model
from car
group by model
having model='alto';
MODEL
----------alto
vi) Demonstrate how you
a) Update the damage amount for the car with a specific reg_no in the accident with

report_no 12 to 25000.
SQL> update participated set damage_amount=25000 where report_no=12;
Table updated.
DRIVER_ID
---------------100
400
500
100
400
123
100
100

REG_NO
----------------33333
22222
44444
33333
22222
66666
33333
33333

REPORT_NO DAMAGE_AMOUNT
------------------- -------------------------------11
10000
12
25000
13
4566
14
2443
16
3545
17
55656
15
3333324
18
366785

b) Add a new accident to the database.


SQL> insert into accident values(101,'11-nov-99','tumkur');
REPORT_NO
DDATE
LOCATION
------------------ ------------- -------------------11
01-JAN-02
houston
12
11-MAR-03 spring
13
12-JUN-02
bellaire
14
01-JAN-02
spring
15
13-JAN-02
houston
16
25-MAY-84 california
17
15-APR-01
tumkur
101
11-NOV-99
tumkur
vii) Find the total number of people who owned cars that were involved in accidents
in 2002.
SQL> select count(*)
from accident a,participated p
where a.report_no=p.report_no and ddate like '_______02';
COUNT(*)
--------------3
viii) Find the number of accidents in which cars belonging to a specific model were
involved.

SQL> select count(*)


from car c.participated p
where model='alto' and c.reg_no=p.reg_no;
COUNT(*)
--------2
ix) Find the owner of the car, which involved in the accident on 11-mar-03 and
damage amount is greater than 10,000.
SQL> select p.driver_id, p.name, p.address
from person p.participated pd.accident a
where pd.damage_amount >10000 and a.ddate='11-mar-03' and
p.driver_id=pd.driver_id and pd.report_no=a.report_no;
DRIVER_ID
---------------400

NAME
-----------------ahmed

ADDRESS
-----------------fine

x) Find the name and address of the person who involved in the accident at a given
location on a given date.
SQL> select p.name, p.address
from person p, participated pd, accident a
where a.ddate='11-mar-03' and a.location='spring' and a.report_no=pd.report_no
and pd.driver_id=p.driver_id;
NAME
-----------ahmed

ADDRESS
-----------------fine

xi) Find the driver_id who is involved in maximum number of accidents.


SQL> select driver_id, count(report_no)
from participated
group by driver_id
having count (report_no) >=all (select count (report_no)
from participated
group by driver_id);
DRIVER_ID
----------------100

COUNT(REPORT_NO)
-----------------------------2

400

xii) Find the car number that is involved in minimum number of accidents.
SQL> select reg_no, count(report_no)
from participated
group by reg_no
having count(report_no)<=all (select count(report_no)
from participated
group by reg_no);
REG_NO
-----------44444
66666

COUNT(REPORT_NO)
-----------------------------1
1

xiii) Find the car involved in accidents at only Houston.


SQL> (select pd.report_no, pd.reg_no
from participated pd, accident a
where pd.report_no=a.report_no and a.location='houston')
minus
(select pd.report_no, pd.reg_no
from participated pd, accident a
where pd.report_no=a.report_no and a.location<>'houston');
REPORT_NO
REG_NO
----------------- -----------------11
33333
15
33333
xiv) Find the car involved in accidents at both Houston and Spring.
SQL> (select pd.reg_no
from participated pd, accident a
where a.location='houston' and pd.report_no=a.report_no)
intersect
(select pd.reg_no
from participated pd,accident a
where a.location='spring' and pd.report_no=a.report_no);
REG_NO
-----------22222
xv) Find the person who involved in accidents happened on same date with different
cars.

SQL> select p.driver_id, p.name, p.address


from person p
where p.driver_id in (select driver_id
from participated
where report_no in (select report_no
from accident
where ddate='1-jan-02'));
DRIVER_ID
---------------100

NAME
ADDRESS
---------------- --------------johny
dallas

CHAPTER 2

ORDER PROCESSING DATABASE


Consider the following relations for an order processing database application in a
company:
CUSTOMER (cust_no: int, cname: string, city: string)
ORDER (order _no: int, odate: date, cust_no: int, ord_amt: int)
ORDER ITEM (order_no: int, item_no: int, qty: int)
ITEM (item_no: int, unit price: int)
SHIPMENT (order_no: int, warehouse_no: int, ship-date: date)
WAREHOUSE (warehouse_no: int, city: string)
ER-Diagram:

Cust_no

Cname

Order_no

Odate

CUSTOMER

ORDER

ORDER
N

N
Order_amt

SHPMENT

ORDER-ITEM

WAREHOUSE

Warehouse_no

ITEM

City

Item_no

Unit price

i) Create the tables by properly specifying primary key and foreign key.

SQL> create table customer (cust_no number(10),cname varchar(20),city


varchar(20),primary key(cust_no));
SQL> create table cust_order (order_no number(10),odate date,cust_no
number(10),ord_amt number(10),primary key(order_no),foreign key(cust_no) references
customer(cust_no));
SQL> create table item (item_no number(10),unit_price number(10),primary
key(item_no));
SQL> create table orditem (order_no number(10), item_no number(10),qty number(10)
, primary key(order_no,item_no),foreign key(order_no) references cust_order(order_no)
,foreign key(item_no) references item(item_no) on delete set null);
SQL> create table warehouse(warehouse_no number(10),city varchar(20),primary
key(warehouse_no));
SQL> create table shipment (order_no number(10),warehouse_no number(10),ship_date
date, primary key(order_no,warehouse_no), foreign key(order_no) references
cust_order(order_no), foreign key(warehouse_no)references warehouse(warehouse_no));
ii) Enter atleast 5 tuples.
SQL> insert into customer values(&cust_no,'&cname','&city');
SQL> insert into cust_order values(&order_no,'&odate',&cust_no,&ord_amt);
SQL> insert into item values(&item_no,&unit_price);
SQL> insert into orditem values(&order_no,&item_no,&qty);
SQL> insert into warehouse values(&warehouse_no,'&city');
SQL> insert into shipment values(&order_no,&warehouse_no,'&ship_date');
iii) Produce a listing: custname, no.of orders, avg_order_amt, where the middle
column is the total no. of orders by the customer and the last column is the average
order amount for that customer.
SQL> select c.cname as cust_name, count (co.order_no) as no_of_orders,
avg (co.ord_amt) as avg_amt
from customer c,cust_order co
where c.cust_no=co.cust_no
group by c.cname, co.cust_no;
CUST_NAME
------------------ahmed
girish
johny
ramesh

NO_OF_ORDERS AVG_AMT
------------------------ ---------------1
34444
1
23323
3
17774.333
1
34534

iv) List the order_no for orders that were shipped from all the warehouses that the
company has in a specific city.
SQL> select order_no,warehouse_no
from shipment
where warehouse_no in (select warehouse_no
from warehouse
where city='tumkur');
ORDER_NO
----------------15
11

WAREHOUSE_NO
------------------------100
100

v) Demonstrate how you delete item# 10 from the ITEM table and make that field
null in the ORDER_ITEM table.
SQL> delete from item
where item_no=10;
vi) Demonstrate how you increase or decrease no. of items ordered by a customer.
SQL> update orditem set qty=qty+10 where order_no in (select order_no
from cust_order
where cust_no=2);
ORDER_NO ITEM_NO
QTY
----------------- -------------- --------12
222
100
14
333
44
15
555
789
11
111
355
13
444
600
SQL> update orditem set qty=qty-10 where order_no in (select order_no
from cust_order
where cust_no=5);
ORDER_NO ITEM_NO
QTY
----------------- -------------- --------12
222
90
14
333
44
15
555
789

STUDENT ENROLLMENT DATABASE

CHAPTER 3

Consider the following database of student enrollment in courses & books adopted
for each course:
STUDENT (regno: string, name: string, major: string, bdate: date)
COURSE (courseno: int, cname: string, dept: string)
ENROLL (regno: string, courseno: int, sem: int, marks: int)
BOOK _ ADOPTION (courseno: int, sem: int, book-ISBN: int)
TEXT (book-ISBN: int, book-title: string, publisher: string, author: string)
ER-Diagram:
Regno

Name

STUDENT

Major

Bdate

Marks

Course_no

ENROLL

Cname

COURSE

Sem

Dept

BOOK-ADOPTION

Sem

Publisher

TEXT

Author

Book-ISBN
book-title
i) Create the above tables by properly specifying the primary keys and foreign keys.
SQL> create table student (reg_no varchar(20), name varchar(20), major varchar(20),
bdate date, primary key(reg_no));

SQL> create table course (course_no number(10),cname varchar(20),dept varchar(20),


primary key (course_no));
SQL> create table text (ISBN number(10), title varchar(20), publisher varchar(20),
author varchar(20), primary key(ISBN));
SQL> create table bookadpt (course_no number(10), sem number(10), ISBN
number(10), primary key (course_no,sem), foreign key (course_no) references course
(course_no), foreign key (ISBN) references text (ISBN));
SQL> create table enroll (reg_no varchar(20), course_no number(10),sem number(10),
marks number(10), primary key (reg_no,course_no), key(reg_no) references
student(reg_no), foreign key (course_no) references course (course_no),foreign key
(sem) references bookadpt (sem));
ii) Enter atleast five tuples for each relation.
SQL> insert into student values('&reg_no','&name','&major','&bdate');
SQL> insert into course values(&course_no,'&cname','&dept')
SQL> insert into enroll values('&reg_no',&course_no,&sem,&marks);
SQL> insert into text values(&ISBN,'&title','&publisher','&author');
SQL> insert into bookadpt values(&course_no,&sem,&ISBN);
iii) Demonstrate check constraint with suitable example.
Qn) Check whether sem entered in enroll relation is less than or equal to 8.
SQL> alter table enroll add constraint max check (sem<9);
SQL> insert into enroll values ('333',44,9,100);
ERROR at line 1:
ORA-02290: check constraint (SCOTT.MAX) violated
iv) Demonstrate inner join, left outer join, right outer join, and full outer join with
suitable example.
Inner join:
SQL> select s.name, s.bdate, e.sem, e.marks
from student s, enroll e
where s.reg_no=e.reg_no;
NAME
----------john

BDATE
-----------11-JAN-02

SEM MARKS
-------- -----------1
100

johny
ramesh
girish
ahmad

23-MAY-09
03-JUN-99
14-APR-93
16-NOV-02

2
6
3
3

200
125
95
100

Left outer join:


SQL> select s.name, s.bdate, e.sem, e.marks
from student s, enroll e
where s.reg_no(+)=e.reg_no;
NAME
---------john
johny
ramesh
girish
ahmad

BDATE
------------11-JAN-02
23-MAY-09
03-JUN-99
14-APR-93
16-NOV-02

SEM
--------1
2
6
3
3

MARKS
----------100
200
125
95
100

Right outer join:


SQL> select s.name, s.bdate, e.sem, e.marks
from student s, enroll e
where s.reg_no=e.reg_no(+);
NAME
-----------john
johny
ramesh
girish
ahmad
sunil

BDATE
------------11-JAN-02
23-MAY-09
03-JUN-99
14-APR-93
16-NOV-02
12-DEC-98

SEM MARKS
--------- -----------1
100
2
200
6
125
3
95
3
100

Full outer join:


SQL> (select s.name, s.bdate, e.sem, e.marks
from student s, enroll e
where s.reg_no(+)=e.reg_no)
union all
( select s.name, s.bdate, e.sem, e.marks
from student s,enroll e
where s.reg_no=e.reg_no(+));
NAME

BDATE

SEM

MARKS

-----------john
johny
ramesh
girish
ahmad
john
johny
ramesh
girish
ahmad
sunil

--------------11-JAN-02
23-MAY-09
03-JUN-99
14-APR-93
16-NOV-02
11-JAN-02
23-MAY-09
03-JUN-99
14-APR-93
16-NOV-02
12-DEC-98

--------1
2
6
3
3
1
2
6
3
3

-----------100
200
125
95
100
100
200
125
95
100

v) Demonstrate Update, Insert and Delete with select and sub select with suitable
example.
Insert
SQL> create table newbook (course_no number(10), sem number(10),ISBN
number(10));
SQL> insert into newbook (select * from bookadpt);
COURSE_NO
SEM
ISBN
------------------- --------- --------11
1
1122
22
2
2233
55
6
3344
44
3
4455
33
3
5566
33
4
1122
55
3
1122
Delete
SQL> delete from bookadpt
where ISBN in (select ISBN
from text
where publisher='sush');
COURSE_NO
-----------------11
22
55
33
33
55

SEM
ISBN
--------- --------1
1122
2
2233
6
3344
3
5566
4
1122
3
1122

Update
SQL> update enroll
set marks=marks*1.5
where course_no in (select course_no
from course
where dept='cse');
REG_NO
------------111
222
333
444
555

COURSE_NO
-----------------11
22
55
44
33

SEM MARKS
--------- ----------1
150
2
300
6
188
3
95
3
100

vi) Demonstrate how you add a new text book to the database and make this book
be adopted by some department.
SQL> insert into text values(6655,'compiler design','pearson','xyz');
SQL> insert into bookadpt values(11,3,6655);
ISBN
----------1122
2233
3344
4455
5566
6655
COURSE_NO
-----------------11
22
55
33
33
55
11

TITLE
PUBLISHER
---------------- ---------- ------------------analysis and design
sapna
data communications
shree
networks
suhas
graphics
sush
data base
vijay
compiler design
pearson
SEM
--------1
2
6
3
4
3
3

AUTHOR
-------------------balaguru
swamy
van
foley
vijaya
xyz

ISBN
--------1122
2233
3344
5566
1122
1122
6655

vii) Produce a list of text books (include Course #, Book-ISBN, Book-title) in the
alphabetical order for courses offered by the CS department that use more than
two books
SQL> select c.course_no, t.ISBN, t.title, count(*)

from course c, bookadpt ba, text t


where c.course_no=ba.course_no and ba.ISBN=t.ISBN and c.dept='cse'and
c.course_no in (select ba.course_no
from bookadpt ba
group by ba.course_no
having count(*)>2)
group by c.course_no, t.ISBN, t.title
order by c.course_no, t.title;
COURSE_NO
-----------------55
55

ISBN TITLE
--------- ----------------------4455 graphics
3344 networks

COUNT(*)
--------------1
1

viii) List any department that has all its adopted books published by a specific
publisher.
SQL> select distinct c.dept
from course c, bookadpt ba, text t
where t.ISBN=ba.ISBN and ba.course_no=c.course_no and t.ISBN in (select
t.ISBN
from text t
where t.publisher='sapna');
DEPT
---------cse
IT
mech

CHAPTER 4
BOOK DEALER DATABASE
The following tables are maintained by a book dealer:

AUTHOR (author_id: int, author_name: string, city: string, country: string)


PUBLISHER (publ_id: int, publ_name: string, publ_city: string, publ_country:
string)
CATEGORY (category_id: int, description: string)
CATALOG (book-id: int, title: string, author_id: int, publisher_id: int, category_id:
int, cyear: int, price: int)
ORDER-DETAILS (order_no: int, book_id: int, qty: int)
ER-Diagram:

Author-id

Name

AUTHOR

City

Title

Year

CATALOG

Country

Category-id

Publisher-id

Price

CATEGORY

Name

PUBLISHER

city

country

Description

N
Quantity

Book-id

ORDER-DETAILS
1
ORDER

Order-no

(i)Create the above tables by properly specifying the primary keys and the foreign
keys.
SQL> create table author (author_id number(5),author_name varchar(15),city
varchar(15),country varchar(15),primary key(author_id));
Table created.

SQL> create table publisher(publ_id number(5),publ_name varchar(15),publ_city


varchar(15),publ_country varchar(15),primary key(publ_id));
Table created.
SQL> create table category(category_id number(5),description varchar(15),primary key
(category_id));
Table created.
SQL> create table order_details(order_no number(5),book_id number(5),qty
number(5),primary key(order_no,book_id), foreign key(book_id) references
catalog(book_id));
Table created.
SQL> create table catalog (book_id number(5),title varchar(15),author_id
number(5),publ_id number(5),category_id number(5),cyear number(4),price numbe
r(5,2),primary key (book_id) foreign key(author_id) references author (author_id),foreign
key(publ_id) references publisher(publ_id),foreign key(categ
ory_id) references category(category_id), constraint cat1 unique ( title,author_id,
publ_id,category_id,cyear, price));
Table created.
(ii) Enter at least five tuples for each relation.
SQL> insert into author values (&author_id,'&author_name','&city','&country');
SQL> insert into publisher values(&publ_id,'&publ_name','&
publ_city','&publ_country');
SQL> insert into category values(&category_id,'&description');
SQL> insert into catalog values(&book_id,'&
title',&author_id,&publ_id,&category_id,&cyear,& price);
SQL> insert into order_details values(&order_no,&book_id,&qty);
(iii) Give the details of the authors who have 2 or more books in the catalog and the
average price of the books is greater than the average price of the books in the
catalog and the year of publication is after 2000.
SQL> select author_id,author_name
from author
where author_id in (select author_id
from catalog
where cyear>2000
group by author_id
having count(*)>1 and avg(price)>(select avg(price)
from catalog));
AUTHOR_ID AUTHOR_NAME
----------------- ----------------------

2222

rose

(iv) Find the author of the book, which has maximum sales.
SQL> create view salesdetails as
( select OD.book_id as book_no, C.price as cost, sum(OD.qty) as qty,
sum(OD.qty * C.price) as sales
from order_details OD, catalog C, author A
where OD.book_id=C.book_id and C.author_id=A.author_id
group by OD.book_id, C.price);
SQL> select a.author_id,a.AUTHOR_NAMe,s.book_no,s.sales
from author a,catalog c,salesdetails s
where a.author_id=c.author_id and s.book_no=c.book_id and
sales=(selectmax(sales) from salesdetails);
AUTHOR_ID AUTHOR_NAME BOOK_NO SALES
------------------ ---------------------- --------------- ---------2222
rose
222
100000
(v) Demonstrate how you increase the price of books published by a specific
publisher by 10%.
SQL> update catalog set price=price*1.1 where PUBL_ID=100;
3 rows updated.
(vi) Create View with a name BookObject with the information about author,
publisher, price and year of publication.
SQL> create or replace view book_object as
(select a.author_id as author_id, a.author_name as author_name,
p.publ_id as publ_id, p.publ_name as publ_name,c.price as price,c.cyear as
cyear
from author a,publisher p,catalog c
where a.author_id=c.author_id and p.publ_id=c.publ_id);
View created.
(vii) Demonstrate how do you add new book information to the database through
views.
SQL> create view book_update as (select author_id, author_name,
publ_id, publ_name, price, cyear from catalog);
view created.
SQL> insert into book_update values(888,frank,111,tom,500,2005);
(viii) Rename the publisher xyz by any new name.

SQL> update publisher set publ_name='seema' where PUBL_ID=300;


1 row updated.
(ix) Find the order number, which included all the books of specific author.
SQL> select order_no
from order_details
where book_id in (select c.book_id
from author a,catalog c
where a.author_id=c.author_id and publ_id=100);
ORDER_NO
--------------11
14
15

BANKING ENTERPRISE
Consider the following database for a banking enterprise
BRANCH (bname: string, bcity: string, assets: real)
ACCOUNT (accno: int, bname: string, balance: real)

CHAPTER 5

DEPOSITOR (cname: string, accno: int)


BANK_CUST (cname: string, cstreet: string, ccity: string)
LOAN (loan_no: int, bname: string, amount: real)
BORROWER (cname: string, loan_no: int)
ER-Diagram:
Cust-name

Cust-street

Accno

N
CUSTOMER

Branch-name

N
DEPOSITOR

ACCOUNT
N
Balance

ACCOUNT-BRANCH

BORROWER
Branch-city

Branch-name
1

Assets

BRANCH
1
LOAN-BRANCH
N
N
LOAN

Branch-name

Loan-no

Amount

(i) Create the above tables by properly specifying the primary keys and the foreign
keys.
SQL> create table branch(bname varchar(10),bcity varchar(10),assets real,primary key
(bname));
Table created.

SQL> create table account(accno integer,bname varchar(10),balance real,primary key


(accno));
Table created.
SQL> create table bank_cust(cname varchar(10),cstreet varchar(10),ccity varchar(10),
primary key (cname));
Table created.
SQL> create table depositor(cname varchar(10),accno integer, primary
key(cname,accno),foreign key(cname) references bank_cust(cname),foreign key(accno)
references account(accno) on delete cascade);
Table created.
SQL> create table loan(loan_no integer,bname varchar(10),amt real,primary
key(loan_no),foreign key(bname) references branch(bname));
Table created.
SQL> create table borrower(cname varchar(10),loan_no integer, primary
key(cname,loan_no),foreign key(cname) references bank_cust(cname),foreign
key(loan_no) references loan(loan_no));
Table created.
(ii) Enter atleast five tuples for each relation.
SQL>insert into branch values('&bname','&bcity',&assets);
SQL> insert into account values(&accno,'&bname',&balance);
SQL> insert into bank_cust values('&cname','&cstreet','&ccity');
SQL> insert into depositor values('&cname',&accno);
SQL> insert into loan values(&loan_no,'&bname',&amt);
SQL> insert into borrower values('&cname',&loan_no);
(iii) Find all the customers who have atleast two accounts at the Main branch.
SQL> select distinct c.cname
from bank_cust c, depositer d, account a
where a.bname='SBM' and a.accno=d.accno and d.cname=c.cname and c.cname
in (select d.cname
from depositor d
group by d.cname having count(*)>=2);
CNAME
---------john
(iv) Demonstrate how you delete all account tuples at every branch located in a
specific city.

SQL> delete from account


where accno in (select a.accno
from account a, branch b
where b.bcity='mysore' and b.bname=a.bname);
5 rows deleted.
(v) Find all customers who have both an account and a loan at the specific
branch.
SQL> (select cname
from depositor d, account a
where d.accno=a.accno and a.bname=SBI)
intersect
(select cname
from borrower b, loan l
where b.loan_no=l.loan_no and l.bname=SBI);
CNAME
---------jack
tom
(vi) Find the branch that has the highest average balance.
SQL> select bname
from account
group by bname
having avg(balance)> all (Select avg(balance) from account);
BNAME
---------SBM

(vii) Demonstrate how do you test for the empty relations with the query to find
all customers who have both an account and a loan at the bank.
SQL>

select cname
from borrower
where exists ( Select *
from depositor d, borrower b where d.cname=b.cname);

CNAME
---------jack
tom
rose
john

Potrebbero piacerti anche