DBMS LAB MANUAL
DBMS LAB MANUAL
Mark Distribution
Note:
1. Create the tables by properly specifying the primary keys and the foreign keys.
2. Enter at least four tuples for each relation
I.Insurance Database
1. Find the total number of people who owned cars that were involved in accidents in
1989.
2. Find the number of accidents in which the cars belonging to “John Smith” were
involved.
3. Update the damage amount for the car with reg number “KA-12” in the accident with
report number “1” to $3000.
Consider the following relations for an order processing database application in a company:
III. Consider the following database of student enrollment in courses & books adopted for
each course:
1. Find all the customers who have atleast 2 accounts at all the branches located in a
specific city.
2. Find all the customers who have accounts in atleast 1 branch located in all the cities
3. Find all the customers who have accounts in atleast 2 branches located in a specific
city.
create Database Insurance
use Insurance
1. Find the total number of people who owned cars that were involved
in accidents in 1989.
2a. Find the number of accidents in which the cars belonging to “John
Smith” were involved.
3. Add a new accident to the database; assume any values for required
attributes.
5. Update the damage amount for the car with reg number “KA-12” in
the accident with report number “1” to $3000.
)
)
) as T
and
P.regno in ( select regno from PARTCIPATED where driverid =
P.driverid group by regno
having sum(dmgamt) >= all ( select sum(dmgamt)
from PARTCIPATED
1. List the names of people who owned cars that were involved in
accidents in 2008.
2. Find the name of owner and his car that has maximum number of
accidents in 2008
3. List the name of owners who own atleast two TOYOTA cars.
select P.fname
from PERSON P,OWNS O,CAR C
where P.driverid=O.driverid and C.regno=O.regno and C.model='TOYATA'
group by P.fname
having count(C.regno)>=2
select P.fname
from PERSON P,CAR C,OWNS O
where P.driverid=O.driverid and C.regno=O.regno and C.model='TOYATA'
group by P.fname
having count(C.regno)>=all(select count(distinct C.regno)
from PERSON P1,CAR C1,OWNS O1
where P1.driverid=O1.driverid and
C1.regno=O1.regno and C1.model='TOYATA'
group by P1.fname)
5. Find the name of owner who owns cars having minimum damage
amount for accidents in 2008
select P.fname,R.dmgamt
from PERSON P,OWNS C,PARTCIPATED R,ACCIDENT A
where P.driverid=R.driverid and C.regno=R.regno and
R.reportno=A.reportno and year(A.accdate)='1998'
group by P.fname,R.dmgamt
having R.dmgamt in (select min(dmgamt)
from PARTCIPATED B,ACCIDENT C
where B.reportno=C.reportno and
year(C.accdate)='1998')
6. List the names of owners whose every car is involved in
accidents in 2008
select P.fname
from PERSON P
where not exists(
select Z.regno from OWNS Z
where P.driverid=Z.driverid and
Z.regno not in
(select C.regno
from CAR C,ACCIDENT A,PARTCIPATED R
where P.driverid=R.driverid and C.regno=R.regno
and A.reportno=R.reportno and
year(A.accdate)='1998'))
select P.fname
from PERSON P
where not exists( select O.regno
from OWNS O
where O.driverid=P.driverid
and O.regno not in( select R.regno
from PARTCIPATED R,CAR
C,ACCIDENT A
where R.driverid=P.driverid and
C.regno=R.regno
and A.reportno=R.reportno and
A.accdate='22 july 1998'))
8. List the names of people who owned cars that were involved in
accidents on a specific day and
atleast two cars of each owner are involved.
select P.fname
from PERSON P,OWNS C,PARTCIPATED R,ACCIDENT A
where P.driverid=R.driverid and C.regno=R.regno and
R.reportno=A.reportno
and A.accdate='22 july 1998'
group by P.fname
having count(C.regno)>=2
select P.fname,C.regno,count(A.reportno),avg(R.dmgamt)
from PERSON P,OWNS C,PARTCIPATED R,ACCIDENT A
where P.driverid=R.driverid and C.regno=R.regno and
A.reportno=R.reportno and year(A.accdate)='1998'
group by P.fname,C.regno
11.Find the total number of people who owned cars that were involved
in accidents in 2008
select count(P.reportno)
from CAR C,PARTCIPATED P
where C.regno=P.regno and C.model='TOYATA'
select A.location
from ACCIDENT A,PARTCIPATED P
where A.reportno=P.reportno and year(A.accdate)='1998'
group by A.location
having count(A.reportno)>=ALL( select count(A1.reportno)
from ACCIDENT A1,PARTCIPATED P1
where A1.reportno=P1.reportno and
year(A1.accdate)='1998'
group by A1.location)
14. Find the people who owned cars that were involved in accidents
at every location.
select P.fname
from PERSON P
where not exists( select distinct A.location
from ACCIDENT A
where A.location not in( select A1.location
from ACCIDENT A1,PARTCIPATED
P1,OWNS O
where A1.reportno=P1.reportno
and P1.driverid=P.driverid
and P1.regno=O.regno))
16. Find the location at which maximum number of Mazda cars are
involved in accidents
select A.location
from ACCIDENT A,PARTCIPATED P,CAR C
where A.reportno=P.reportno and C.regno=P.regno and C.model='SWIFT'
group by A.location
having count(distinct C.regno)>=ALL( select count(distinct C1.regno)
from CAR C1,ACCIDENT
A1,PARTCIPATED P1
where C1.regno=P1.regno and
A1.reportno=P1.reportno and C1.model='SWIFT'
group by A1.location)
select C.regno,C.model,sum(P.dmgamt)
from CAR C,PARTCIPATED P
where C.regno=P.regno
group by C.regno,C.model
2)list the owner/owners of the car with the maximum damage amount
select P.fname,R.dmgamt
from OWNS O,PERSON P,PARTCIPATED R
where P.driverid=O.driverid and R.driverid=O.driverid and
R.regno=O.regno
group by P.fname,R.dmgamt
having R.dmgamt=(select max(dmgamt) from PARTCIPATED)
select O.driverid,P.fname,C.regno,C.model
from OWNS O,PERSON P,CAR C
where P.driverid=O.driverid and O.regno=C.regno
select O.driverid,P.fname,count(C.regno)
from PERSON P,OWNS O,CAR C
where O.driverid=P.driverid and C.regno=O.regno
group by O.driverid,P.fname
select C.regno,C.model,count(P.reportno)
from CAR C,PARTCIPATED P
where C.regno=P.regno
group by C.regno,C.model
6)Give the details of the owners who owned 2 or more cars and
registered after 2000
and total damage amount for a car is between 10,000 and 25,000
select P.driverid,P.fname
from PERSON P,PARTCIPATED R,CAR C
where C.cyear > 1979 and R.driverid=P.driverid and C.regno=R.regno
group by P.driverid,P.fname
having (sum(R.dmgamt)>=10000 and sum(dmgamt)<=210000)and
count(C.regno)>=2
select P.driverid,P.fname,C.model,count(C.regno)
from OWNS O,CAR C,PERSON P
where P.driverid=O.driverid and C.regno=O.regno
group by P.driverid,P.fname,C.model
select P.fname
from PERSON P
where not exists( select distinct year(A.accdate)
from ACCIDENT A
where year(A.accdate) not in( select distinct
year(A1.accdate)
from ACCIDENT
A1,PARTCIPATED P1,OWNS O
where
A1.reportno=P1.reportno and O.driverid=P.driverid
and
P1.driverid=O.driverid))
create database ord_proc
use ord_proc
select count(*)
from SHIPMENT s,WAREHOUSE w
where w.warehouseid=s.warehouseid and city='MAGALORE'
2.List the order# for orders that were shipped from all the warehouses
that the company has in a specific city.
using not in
------------
select O.orderid from C_ORDER O
where not exists (select warehouseid from WAREHOUSE where city =
'MAGALORE' and warehouseid not in
(select warehouseid from
SHIPMENT where orderid = O.orderid)
)
using count
-----------
select C.cname,count(O.orderid),avg(O.ordamt)
from CUSTOMER C,C_ORDER O
where C.custid=O.custid
group by C.cname
2. List the order# for orders that were shipped from all the
warehouses that the company has
in a specific city.
select T.cname,T.custid,T.city,T.avg_amt
from tb1 T
group by T.cname,T.custid,T.city,T.avg_amt
having T.avg_amt > ( select avg_amt from tb2
where T.custid=custid)
4. Find the customer with maximum order amount for the year 2008
select C.cname,O.ordamt
from CUSTOMER C,C_ORDER O
where C.custid=O.custid and YEAR(O.odate)='2008'
group by C.cname,O.ordamt
having O.ordamt in ( select max(A.ordamt)
from C_ORDER A
where YEAR(A.odate)='2008')
select C.cname,I.qty
from CUSTOMER C,C_ORDER O,ORDER_ITEM I
where C.custid=O.custid and O.orderid=I.orderid
group by C.cname,I.qty
having I.qty in( select MIN(qty)
from ORDER_ITEM)
6. Find the item on which the company makes highest profit for the
year 2008
select I.itemid,T.qty,I.price
from ITEM I,C_ORDER O,ORDER_ITEM T
where I.itemid=T.itemid and O.orderid=T.orderid and
YEAR(O.odate)='2008'
group by I.itemid,T.qty,I.price
having T.qty*I.price in ( select max(B.qty*A.price)
from ITEM A,ORDER_ITEM B,C_ORDER C
where A.itemid=B.itemid and
C.orderid=B.orderid and YEAR(odate)='2008')
OR
select I.itemid
from ITEM I,C_ORDER O,ORDER_ITEM T
where I.itemid=T.itemid and O.orderid=T.orderid and
YEAR(O.odate)='2001'
group by I.itemid
having sum(T.qty*I.price)>=ALL ( select sum(B.qty*A.price)
from ITEM A,ORDER_ITEM B,C_ORDER C
where A.itemid=B.itemid and
C.orderid=B.orderid and YEAR(odate)='2001'
group by A.itemid)
7. List the order# for orders that have been ordered for every item
that the company produces.
select C.orderid
from C_ORDER C
where not exists( select itemid
from ITEM
where itemid not in( select itemid
from ORDER_ITEM I
where C.orderid=I.orderid))
select W.city
from WAREHOUSE W,SHIPMENT S,ORDER_ITEM I
where W.warehouseid=S.warehouseid and S.orderid=I.orderid
group by W.city
having SUM(I.qty) >=ALL ( select SUM(C.qty)
from WAREHOUSE A,SHIPMENT B,ORDER_ITEM C
where A.warehouseid=B.warehouseid and
B.orderid=C.orderid
group by A.city )
10. List the order# for orders that were shipped from atmost two
warehouses that the company has
in a specific city
select S.orderid
from SHIPMENT S,WAREHOUSE W
where S.warehouseid=W.warehouseid and W.city='UDUPI'
group by S.orderid
having count(W.warehouseid)<=2
select C.custid,C.cname,I.itemid
from C_ORDER O,CUSTOMER C,ORDER_ITEM I
where C.custid=O.custid and O.orderid=I.orderid
group by C.custid,C.cname,I.itemid
select C.cname,C.custid,C.city
from CUSTOMER C,C_ORDER O
where C.custid=O.custid
group by C.cname,C.custid,C.city
having COUNT(O.orderid) >=ALL ( select COUNT(orderid)
from CUSTOMER A,C_ORDER B
where A.custid=B.custid
group by A.custid)
select I.itemid
from ORDER_ITEM O,ITEM I
where I.itemid=O.itemid
group by I.itemid
having COUNT(O.orderid) >=ALL ( select COUNT(O.orderid)
from ORDER_ITEM O,ITEM I
where I.itemid=O.itemid
group by I.itemid)
select I.itemid
from ORDER_ITEM O,ITEM I
where I.itemid=O.itemid
group by I.itemid
having sum(O.qty) >=ALL ( select SUM(A.qty)
from ORDER_ITEM A,ITEM B
where A.itemid=B.itemid
group by B.itemid)
f)Give the details of total amount earned for each item .(itemno,
total amount earned)
select I.itemid,SUM(I.price*O.qty)
from ITEM I,ORDER_ITEM O
where I.itemid=O.itemid
group by I.itemid
g) List any customer whose all ordered items are shipped from a
specific warehouse.
select C.cname
from CUSTOMER C
where not exists( select O.orderid
from C_ORDER O
where O.custid=C.custid and O.orderid not in(select
S.orderid
from
WAREHOUSE W,SHIPMENT S
where
W.warehouseid=S.warehouseid
and
W.warehouseid=2))
4. Find the total price of the items that were shipped between 2005
and 2008
2.Find the customer with minimum number of orders but with maximum
order amount
select T.cname
from tb1 T,CUSTOMER C,C_ORDER O
where T.cname=C.cname and C.custid=O.orderid
group by T.cname
having SUM(O.ordamt)>=ALL(select SUM(O1.ordamt)
from CUSTOMER C1,C_ORDER O1,tb1 T1
where T1.cname=C1.cname and
C1.custid=O1.orderid
group by C1.cname)
create database st_enroll
use st_enroll
2. List any department that has all its adopted books published by
a specific publisher
OR
1. for each dept list course that adopts maximum number of books
select C.course,T.bookISBN,T.title
from COURSE C,BOOK_ADAPTION B,TEXTBOOK T
where C.course=B.course and B.bookISBN=T.bookISBN and C.dept='CS'
and C.course in( select course
from BOOK_ADAPTION
where course=C.course
group by course
having COUNT(distinct bookISBN)>=2)
order by T.title
2. List any department that has all its adopted books published by a
specific publisher. (*)
from TEXTBOOK T
where T.publisher='Mcgraw'))
select C.dept
from COURSE C,BOOK_ADAPTION B,TEXTBOOK A
where C.course=B.course and B.bookISBN=A.bookISBN
group by C.dept
having COUNT(B.bookISBN) >=ALL (select COUNT(distinct D.bookISBN)
from COURSE F,BOOK_ADAPTION D,TEXTBOOK E
where F.course=D.course and
D.bookISBN=E.bookISBN
group by F.dept)
select C.course
from COURSE C,BOOK_ADAPTION B,TEXTBOOK A
where C.course=B.course and B.bookISBN=A.bookISBN and C.dept='CS'
and A.title='Fundamentals of DBMS'
5. List the publishers and authors of the books adopted for a
specific course offered by
“CS” department.
select S.regno,S.fname,S.major
from STUDENT S
where not exists(select C.course
from COURSE C
where C.dept='CS' and C.course not in(select course
from ENROLL
where
regno=S.regno))
select s.regno,S.fname,S.major
from STUDENT S
where not exists ( select distinct C.course
from COURSE C,BOOK_ADAPTION B,TEXTBOOK A
where C.course=B.course and B.bookISBN=A.bookISBN
and C.dept='CS'
and C.course in (select course
from BOOK_ADAPTION
group by course
having COUNT(distinct
bookISBN)>=2)and C.course not in( select D.course
from ENROLL D
where S.regno=D.regno))
order by S.fname
select C.dept
from COURSE C,ENROLL E
where C.course=E.course
group by C.dept
having COUNT(distinct E.regno)>=ALL ( select COUNT(distinct F.regno)
from ENROLL F,COURSE D
where F.course=D.course
group by D.dept)
select T.bookISBN,T.title,T.author
from TEXTBOOK T,BOOK_ADAPTION B,COURSE C,ENROLL E
where T.bookISBN=B.bookISBN and C.course=E.course and
B.course=E.course and C.dept='CS'
group by C.course,T.bookISBN,T.title,T.author
having COUNT(distinct E.regno)=2
order by T.title
11. For each department that offers more than 2 courses, list the
dept name, total number of students enrolled in those
courses and total number of books adapted by those courses.
d)List the books(if any) which are adopted by more than one dept.
select B.bookISBN
from BOOK_ADAPTION B,COURSE C
where C.course=B.course
group by B.bookISBN
having COUNT(distinct C.dept)>1
select C.dept
from STUDENT S,COURSE C,ENROLL E
where S.regno=E.regno and C.course=E.course
group by C.dept
having COUNT(distinct S.regno) >=ALL( select COUNT(distinct S1.regno)
from STUDENT S1,COURSE B,ENROLL
E1
where S1.regno=E1.regno and
B.course=E1.course
group by B.dept)
select C.dept
from COURSE C
group by C.dept
having COUNT(distinct C.course) >=ALL(select COUNT(distinct course)
from COURSE
group by dept)
i)Give the details of the student who has taken maximum no. of
courses
select S.regno,S.fname,S.major
from STUDENT S,ENROLL E
where S.regno=E.regno
group by S.regno,S.fname,S.major
having count(distinct E.course) >= ALL( select count(distinct
E1.course)
from STUDENT S1,ENROLL E1
where S1.regno=E1.regno
group by
S1.regno,S1.fname,S1.major)
j) Give the details of the student who has obtained maximum marks
select S.regno,S.fname,S.major
from STUDENT S,ENROLL E
where S.regno=E.regno
group by S.regno,S.fname,S.major
having AVG(E.marks)>=ALL( select AVG(E1.marks)
from ENROLL E1,STUDENT S1
where S1.regno=E1.regno
group by S1.regno,S1.fname,S1.major)
group by B1.course
select C.cname,C.course
from COURSE C,tb1 T
where C.dept=T.dept
create database bk_shop
use bk_shop
1. 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.
update catalog set price = price * 1.1 where pubid in ( select pubid
from publisher where pname ='Pearson')
1. 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.(*)
select A.authorid,A.aname,A.city,A.country
from AUTHOR A,CATALOGUE C
where C.authorid=A.authorid and C.yr=2000
group by A.authorid,A.aname,A.city,A.country
having count(distinct C.bookid)>=2 and sum(C.price) >(select
AVG(price) from CATALOGUE)
select A.authorid,A.aname
from AUTHOR A,CATALOGUE C,ORDER_DET O
where A.authorid=C.authorid and C.bookid=O.bookid
group by A.authorid,A.aname,C.bookid
having SUM(O.qty)>=ALL( select SUM(O1.qty)
from AUTHOR A1,CATALOGUE C1,ORDER_DET O1
where A1.authorid=C1.authorid and
C1.bookid=O1.bookid
group by A1.authorid,A1.aname,C1.bookid)
3. List the order-no# for orders that were ordered for every book of
a specific author.
4. List the order-no# for orders that were ordered for every book
published by a specific
publisher.
5. List the order-no# for orders that were ordered for every book of
a specific category.
6. List names of authors who have written atleast one book in every
category.
select A.aname
from AUTHOR A
where not exists( select distinct catid
from CATEGORY
where catid not in( select distinct C.catid
from CATALOGUE C
where C.authorid=A.authorid))
7. List names of authors who have written atleast two books in every
category.
select A.aname
from AUTHOR A
where not exists( select distinct catid
from CATEGORY
where catid not in( select C.catid
from CATALOGUE C
where C.authorid=A.authorid
group by C.catid
having count(C.bookid)>=2))
8. List the order-no# for orders that were ordered for every book
published by a specific
publisher and written by a specific author.
select C.catid
from CATEGORY C,ORDER_DET O,CATALOGUE E
where C.catid=E.catid and O.bookid=E.bookid
group by C.catid
having SUM(O.qty) >=ALL( select SUM(O1.qty)
from CATEGORY C1,ORDER_DET O1,CATALOGUE E1
where C1.catid=E1.catid and
O1.bookid=E1.bookid
group by C1.catid)
10. Find the publisher of the book which has maximum sales.
select P.pname,P.pubid
from PUBLISHER P,CATALOGUE C,ORDER_DET O
where C.pubid=P.pubid and O.bookid=C.bookid
group by P.pubid,P.pname
having count(O.qty)>=all( select count(O1.qty)
from PUBLISHER P1,CATALOGUE C1,ORDER_DET O1
where C1.pubid=P1.pubid and
O1.bookid=C1.bookid
group by P1.pubid,P1.pname)
11. Find the price of the book which has maximum sales.
select C.price
from CATALOGUE C,ORDER_DET O
where C.bookid=O.bookid
group by C.bookid,C.price
having sum(O.qty)>=ALL( select SUM(O1.qty)
from CATALOGUE C1,ORDER_DET O1
where C1.bookid=O1.bookid
group by C1.bookid)
13. Find the average amount earned from the book which has maximum
sales.
14. Find the number of books that were sold for the book which has
maximum sales.
15. Find the publication year of the book which has maximum sales.
select C.yr
from CATALOGUE C,ORDER_DET O
where C.bookid=O.bookid
group by C.bookid,C.yr
having SUM(O.qty) >=ALL( select SUM(O1.qty)
from CATALOGUE C1,ORDER_DET O1
where C1.bookid=O1.bookid
group by C1.bookid)
select P.pname,P.city,P.country
from PUBLISHER P
where not exists( select catid
from CATEGORY
where catid not in (select C.catid
from CATALOGUE C,CATEGORY C1
where C.catid=C1.catid
and C.pubid=P.pubid
group by C.catid,C.pubid
having count(C.bookid)>=1))
select C.catid,C.bookid,A.aname,P.pname,C.title
from AUTHOR A,CATALOGUE C,PUBLISHER P,CATEGORY B
where C.catid=B.catid and C.authorid=A.authorid and C.pubid=P.pubid
group by C.catid,C.bookid,A.aname,P.pname,C.title
select O.ordno,C.bookid,C.authorid,A.aname,O.qty
from ORDER_DET O,AUTHOR A,CATALOGUE C
where O.bookid=C.bookid and A.authorid=C.authorid
group by O.ordno,C.bookid,C.authorid,A.aname,O.qty
select C.bookid,A.aname,P.pname,C.title
from AUTHOR A,CATALOGUE C,PUBLISHER P,ORDER_DET O
where P.pubid=C.pubid and A.authorid=C.authorid and O.bookid=C.bookid
group by C.bookid,A.aname,P.pname,C.title
having count(O.ordno)>=ALL ( select count(O1.ordno)
from AUTHOR A1,CATALOGUE C1,PUBLISHER
P1,ORDER_DET O1
where P1.pubid=C1.pubid and
A1.authorid=C1.authorid
and O1.bookid=C1.bookid
group by
C1.bookid,A1.aname,P1.pname,C1.title)
select C.catid,C.descript
from CATEGORY C,CATALOGUE C1,ORDER_DET O
where C.catid=C1.catid and O.bookid=C1.bookid
group by C1.bookid,C.catid,C.descript
having SUM(O.qty)>=ALL( select SUM(O1.qty)
from CATEGORY C3,CATALOGUE C2,ORDER_DET O1
where C3.catid=C2.catid and
O1.bookid=C2.bookid
group by C2.bookid)
select C.descript
from CATEGORY C,CATALOGUE B,ORDER_DET O
where C.catid=B.catid and O.bookid=B.bookid
group by B.bookid,C.descript
having count(O.ordno)<=ALL( select count(O1.ordno)
from CATEGORY C1,CATALOGUE B1,ORDER_DET O1
where C1.catid=B1.catid and
O1.bookid=B1.bookid
group by B1.bookid)
f)What is the total amount earned by the dealer from the book having
maximum sales.
select C.catid
from CATEGORY C
where not exists( select B.bookid
from CATALOGUE B
where B.catid=C.catid and B.bookid not in( select
distinct bookid
from
ORDER_DET))
create database bank
use bank
2A. Find all the customers who have an account at all the branches
located in a specific city.
OR
OR
2B. Find all the customers who have atleast 2 accounts at all the
branches
located in a specific city.
or
4)Find all the customers who have accounts in atleast 1 branch located
in all the cities
)
)
OR
Find all the customers who have accounts in atleast 2 branches located
in all the cities
1)Give the details of all the branches having more than two account
.
select B.bname,B.bcity
from BRANCH B,ACCOUNT A
where B.bname=A.bname
group by B.bname,B.bcity
having count(A.accno)>=2
1. Find all the customers who have at least two accounts at the Main
branch. (*)
select D.cname
from DEPOSITOR D,ACCOUNT A
where A.accno=D.accno and A.bname='state_udupi'
group by D.cname
having count(*)>=2
2. Find all the customers who have an account at all the branches
located in a specific city(*)
select distinct D.cname
from DEPOSITOR D
where not exists( select B.bname
from BRANCH B
where B.bcity='karkala'
and B.bname not in( select bname
from ACCOUNT A,DEPOSITOR D1
where A.accno=D1.accno
and A.bname=B.bname
and D1.cname=D.cname))
select D.cname
from DEPOSITOR D,ACCOUNT A,BRANCH B
where D.accno=A.accno and A.bname=B.bname and B.bcity='karkala'
group by D.cname
having count(*)>=2
select C.cname
from CUSTOMER C
where not exists(select distinct(B1.bcity)
from BRANCH B1
where not exists(select count( distinct B.bname)
from BRANCH B, ACCOUNT A
,DEPOSITOR D
where A.bname = B.bname
and D.accno =A.accno and B.bcity
= B1.bcity
and D.cname =C.cname
group by B.bcity
having count(*) >=1))
select C.cname
from CUSTOMER C
where not exists( select distinct B1.bname
from BRANCH B1
where not exists( select COUNT(B.bname)
from BRANCH B,DEPOSITOR D,ACCOUNT
A
where B.bname=A.bname and
D.accno=A.accno
and B.bcity=B1.bcity and
D.cname=C.cname
group by B.bcity
having COUNT(*)>=2))
select B.bname
from BRANCH B,ACCOUNT A,DEPOSITOR D
where A.accno=D.accno and A.bname=B.bname and B.bcity='karkala'
group by B.bname
having COUNT(distinct D.cname) >=ALL (select COUNT(distinct D1.cname)
from BRANCH B1,ACCOUNT
A1,DEPOSITOR D1
where A1.accno=D1.accno and
A1.bname=B1.bname
and B1.bcity='karkala'
group by B1.bname)
select B.bname
from BRANCH B,ACCOUNT A,DEPOSITOR D
where A.accno=D.accno and A.bname=B.bname and B.bcity='karkala'
group by B.bname
having COUNT(distinct A.accno) >=ALL (select COUNT(A1.accno)
from BRANCH B1,ACCOUNT
A1,DEPOSITOR D1
where A1.accno=D1.accno and
A1.bname=B1.bname
and B1.bcity='karkala'
group by B1.bname)
select D.cname
from DEPOSITOR D,ACCOUNT A,BRANCH B
where A.accno=D.accno and A.bname=B.bname and B.bcity='karkala'
group by D.cname
having SUM(A.balance) >= ALL(select SUM(A1.balance)
from DEPOSITOR D1,ACCOUNT A1,BRANCH B1
where A1.accno=D1.accno and
A1.bname=B1.bname and B1.bcity='karkala'
group by D1.cname)
select D.cname,A.balance,B.bcity
from ACCOUNT A,DEPOSITOR D,BRANCH B
where D.accno=A.accno and B.bname=A.bname
group by B.bcity,D.cname,A.balance
10. Find the customers who have borrowed loan from all the branches
located in a specific city
11. Find the customers who have borrowed loan from atleast one
branch located in all the cities
12. Find the customers who have borrowed loan from atleast 2 branch
located in all the cities
a). Give the details of all the branches having more than two
account
select B.bname,B.bcity
from BRANCH B,ACCOUNT A
where B.bname=A.bname
group by B.bname ,B.bcity
having COUNT(distinct A.accno)>=2
select L.loanno,L.bname,B1.cname
from LOAN L,BRANCH B,BORROWER B1
where L.loanno=B1.loanno and B.bname=L.bname
list the customers who have borrowed money from every branch located
in a specific city
select C.cname
from CUSTOMER C
where not exists( select B.bname
from BRANCH B
where B.bcity='Mangalore'
and B.bname not in( select distinct L.bname
from BORROWER B1,LOAN L
where B1.loanno=L.loanno and
L.bname=B.bname
and B1.cname=C.cname))
select * from tb
select C.cname
from BORROWER C,LOAN L
where C.loanno=L.loanno
group by C.cname
having COUNT(distinct L.loanno)>=ALL( select COUNT(distinct L1.loanno)
from LOAN L1,BORROWER B
where L1.loanno=B.loanno
group by B.cname)
select B.bname,B.bcity
from BRANCH B,ACCOUNT A,DEPOSITOR D
where B.bname=A.bname and D.accno=A.accno
group by B.bname,B.bcity
having COUNT(distinct D.cname)>=ALL( select COUNT(distinct D1.cname)
from BRANCH B1,ACCOUNT
A1,DEPOSITOR D1
where B1.bname=A1.bname and
D1.accno=A1.accno
group by B1.bname,B1.bcity)
g)Find the customer(if any) who does not have an account at all the
branch located in
a specific city.
select C.cname
from CUSTOMER C
where not exists( select B.bname
from BRANCH B
where B.bcity='Udupi'
and B.bname in( select B1.bname
from ACCOUNT A,BRANCH B1,DEPOSITOR D
where B1.bname=A.bname and
D.accno=A.accno
and D.cname=C.cname
group by B1.bname))
select D.cname
from ACCOUNT A,DEPOSITOR D
where D.accno=A.accno
group by D.cname
having COUNT(distinct A.accno) >=ALL (select COUNT(distinct A1.accno)
from ACCOUNT A1,DEPOSITOR D1
where A1.accno=D1.accno
group by D1.cname )