DBMS Lab Question

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Question:

Consider the following relations:

Student (snum: integer, sname: string, major:


string, level: string, age: integer)
Class (name: string, meets at: string, room: string, d:
integer)
Enrolled (snum: integer, cname: string)
Faculty (fid: integer, fname: string, deptid: integer)
The meaning of these relations is straightforward; for
example, Enrolled has one record per student-class pair such
that the student is enrolled in the class. Level is a two
character code with 4 different values (example: Junior: JR
etc)
Write the following queries in SQL. No duplicates should be
printed in any of the answers.

Queries:

1. Find the names of all Juniors (level = JR)


who are enrolled in a class taught by Prof.
Harshith
SELECT DISTINCT S.Sname
FROM STUDENT S, CLASS C, ENROLLED E,
FACULTY F
WHERE S.snum = E.snum AND
E.cname=C.cname AND
C.fid=F.fid AND
F.fname='harshith' AND
S.levels='JR';
2. Find the names of all classes that either
meet in room R128 or have five or more
Students enrolled.
SELECT DISTINCT C.cname
FROM CLASS C
WHERE C.room='r128' OR
C.cname IN
(SELECT E.cname
FROM ENROLLED E
GROUP BY E.cname
HAVING COUNT(*)>= 5);

3. Find the names of all students who are


enrolled in two classes that meet at the
same time.
SELECT DISTINCT S.sname
FROM STUDENT S
WHERE S.snum IN
(SELECT E1.snum
FROM ENROLLED E1, ENROLLED E2, CLASS C1,
CLASS C2
WHERE E1.snum=E2.snum AND
E1.cname<>E2.cname AND
E1.cname=C1.cname AND
E2.cname=C2.cname AND
C1.meetsat=C2.meetsat);

4. Find the names of faculty members who


teach in every room in which some class is
taught.
SELECT DISTINCT F.fname
FROM FACULTY F
WHERE NOT EXISTS(
SELECT room FROM CLASS)
Minus
(SELECT C1.room
FROM CLASS C1
WHERE C1.fid=F.fid ));

5. Find the names of faculty members for


whom the combined enrollment of the
courses that they teach is less than five.
SELECT DISTINCT F.fname
FROM FACULTY F
WHERE 5 > ( SELECT COUNT(E.snum)
FROM CLASS C, ENROLLED E
WHERE C.cname=E.cname AND
C.fid=F.fid);
Question:
The following relations keep track of airline flight
information:

Flights (no: integer, src: string, dst: string, distance:


integer, Departs: time, arrives: time, price: real)
Aircraft (aid: integer, aname: string, cruisingrange:
integer)
Certified (eid: integer, aid: integer)
Employees (eid: integer, ename: string, salary: integer)
Note that the Employees relation describes pilots and other
kinds of employees as well. Every pilot is certified for some
aircraft, and only pilots are certified to fly.
Write each of the following queries in SQL.

Queries:

1. Find the names of aircraft such that all


pilots certified to operate them have
salaries more than Rs.80, 000.
Method 1.

SELECT DISTINCT A.aname


FROM AIRCRAFT A, CERTIFIED C
WHERE A.aid=C.aid AND
C.eid IN ( SELECT eid
FROM EMPLOYEES
WHERE salary > 80000);

Method 2.

SELECT DISTINCT a.aname


FROM aircraft a
WHERE a.aid NOT IN
( SELECT c.aid
FROM certified c,employees e
WHERE c.eid=e.eid AND
e.salary<80000);

2. For each pilot who is certified for more


than three aircrafts, find the eid and the
maximum cruisingrange of the aircraft for
which she or he is certified.
SELECT C.eid, MAX(a.cruisingrange)
FROM CERTIFIED C, AIRCRAFT A
WHERE C.aid=A.aid
GROUP BY C.eid
HAVING COUNT(*) > 3;

3. Find the names of pilots whose salary is


less than the price of the cheapest route
from Bengaluru to Frankfurt.
SELECT E.ename
FROM EMPLOYEES E
WHERE E.salary < (SELECT min(price)
FROM FLIGHTS
WHERE fromPlace=bangalore AND
oPlace='frankfurt');

4. For all aircraft with cruising range over


1000 Kms, .find the name of the aircraft
and the average salary of all pilots certified
for this aircraft.
select aname, avg(salary) as avgsal
from aircraft a, certified c, employees
e
where a.crusingrange>1000 and
c.aid=a.aid and
c.eid=e.eid
group by aname;

5. Find the names of pilots certified for


some Boeing aircraft.
SELECT DISTINCT E.ename
FROM AIRCRAFT A, CERTIFIED C, EMPLOYEES
E
WHERE A.aid=C.aid AND
C.eid=E.eid AND
A.aname LIKE 'boeing%';

6. Find the aids of all aircraft that can be


used on routes from Bengaluru to New
Delhi
SELECT A.aid
FROM AIRCRAFT A
WHERE A.cruisingrange > (SELECT
min(distance) FROM FLIGHTS
WHERE fromPlace='bangalore' AND
toPlace = 'delhi');
Question:
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, bookisbn: int)
TEXT (bookisbn: int, btitle: string, publisher:
string, author: string)
Queries:
1. Create the above tables by properly
specifying the primary keys and the foreign
keys.
CREATE TABLE STUDENT (
regno varchar(10),
name varchar(10),
major varchar(8),
bdate date,
constraint spk PRIMARY KEY(regno));

CREATE TABLE COURSE (


course# int,
cname varchar(10),
dept varchar(10),
constraint cpk PRIMARY KEY(course#));

CREATE TABLE ENROLL(


regno varchar(10),
course# int,
sem int,
marks int,
constraint epk PRIMARY
KEY(regno,course#,sem),
constraint efk1 FOREIGN KEY(regno)
references STUDENT(regno),
constraint efk2 FOREIGN KEY(course#)
references COURSE(course#));

CREATE TABLE TEXT (


book_isbn int,
book_title varchar(10),
publisher varchar(10),
author varchar(10),
constraint tpk PRIMARY KEY(book_isbn));

CREATE TABLE BOOK_ADOPTION (


course# int,
sem int,
book_isbn int,
constraint bpk PRIMARY KEY(course#,
sem),
constraint bfk1 FOREIGN KEY(course#)
references COURSE(course#),
constraint bfk2 FOREIGN KEY(book_isbn)
references TEXT(book_isbn));

2. Enter at least five tuples for each


relation.
INSERT INTO STUDENT
VALUES('&no','&name','&major','&bdate');

INSERT INTO COURSE


VALUES('&course','&cname','&dept');

INSERT INTO ENROLL


VALUES('&no','&course#','&sem','&marks');

INSERT INTO TEXT


VALUES('&book_isbn','&book_title','&publi
sher','&author');

INSERT INTO BOOK_ADOPTION


VALUES('&course','&sem','&book_isbn');

3. Demonstrate how you add a new text


book to the database and make this book be
adopted by some department.
INSERT INTO TEXT VALUES(1686, 'DBMS',
'pearson', 'navathe');

INSERT INTO BOOK_ADOPTION VALUES(15, 2,


1686);

4. Produce a list of text books (include


Courseno, Book-ISBN, Book-title) in the
alphabetical order for courses offered by
the CS department that use more than
two books.
SELECT C.course#, T.book_isbn,
T.book_title
FROM COURSE C, BOOK_ADOPTION B, TEXT T
WHERE C.course# = B.course# AND
B.book_isbn = T.book_isbn AND
C.dept='cse' AND
C.course# IN (SELECT course#
FROM BOOK_ADOPTION
GROUP BY course#
HAVING COUNT(*)>2)
ORDER BY C.course#, T.book_isbn,
T.book_title;

5. List any department that has all its


adopted books published by a specific
publisher.
SELECT C.dept, COUNT(*)
FROM COURSE C, BOOK_ADOPTION B
WHERE C.course# = B.course#
GROUP BY C.dept
INTERSECT
SELECT C.dept, COUNT(*)
FROM COURSE C, BOOK_ADOPTION B, TEXT T
WHERE C.course# = B.course# AND
B.book_isbn = T.book_isbn AND
T.publisher = 'sapna'
GROUP BY C.dept;

6. Generate suitable reports.


ttitle 'BOOKS ADOPTED BY PARTICULAR
DEPARTMENT' btitle 'BOOK DATABASE'
column ISBN heading 'BOOK ISBN'
column title heading 'BOOK TITLE'
column cname heading 'COURSE NAME'
column dept heading 'DEPARTMENT NAME'
break on dept skip 2 compute count of
ISBN on dept set linesize 100 set agesize
40

select t.ISBN,t.title,c.cname,c.dept
from course c, BOOK_ADOPTION a,text t
where c.course#=a.course# and
a.ISBN=t.ISBN order by dept;

btitle off
ttitle off
clear breaks
clear columns
Question:
The following tables are maintained by a book dealer.

AUTHOR (authorid: int, name: string, city:


string, country: string)
PUBLISHER (publisherid: int, name: string, city:
string, country: string)
CATALOG (bookid: int, title: string, authorid:
int, publisherid: int, categoryid: int, year: int, price: int)
CATEGORY (categoryid: int, description: string)
ORDER_DETAILS (orderno: int, bookid: int, quantity:
int)

Queries:
1. Create the above tables by properly
specifying the primary keys and the foreign
keys.

CREATE TABLE AUTHOR (


author_id int,
name varchar(20),
city varchar(20),
country varchar(20),
constraint apk PRIMARY KEY(author_id));

CREATE TABLE PUBLISHER (


publisher_id int,
name varchar(20),
city varchar(20),
country varchar(20),
constraint ppk PRIMARY
KEY(publisher_id));

CREATE TABLE CATEGORY (


category_id int,
description varchar(20),
constraint cpk PRIMARY KEY(category_id));
CREATE TABLE CATALOG (
book_id int,
title varchar(20),
author_id int,
publisher_id int,
category_id int,
year int,
price int,
constraint cpk PRIMARY KEY(book_id),
constraint cfk1 FOREIGN KEY(author_id)
references AUTHOR(author_id),
constraint cfk2 FOREIGN KEY(publisher_id)
references PUBLISHER(publisher_id),
constraint cfk3 FOREIGN KEY(category_id)
references CATEGORY(category_id));

CREATE TABLE ORDER_DETAILS (


order_no int,
book_id int,
quantity int,
constraint opk PRIMARY KEY(order_no,
book_id),
constraint ofk FOREIGN KEY(book_id)
references CATALOG(book_id));

2. Enter at least five tuples for each


relation.
INSERT INTO AUTHOR
VALUES('&author_id','&name','&city','&cou
ntry');

INSERT INTO PUBLISHER


VALUES('&publisher_id','&name','&city','&
country');

INSERT INTO CATEGORY


VALUES('&category_id','&description');

INSERT INTO CATALOG


VALUES('&book_id','&title','&author_id','
&publisher_id','&category_id','&year','&p
rice');

INSERT INTO CATALOG


VALUES('&book_id','&title','&author_id',
'&publisher_id','&category_id','&year','&
price');

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.
SELECT *
FROM AUTHOR a
WHERE a. author_id IN
(SELECT c. author_id
FROM CATALOG
WHERE c. year > 2000 AND
c.price > (SELECT AVG(price) FROM
CATALOG)
GROUP BY c.author_id
having count(c.authorid)>=2);

4. Find the author of the book which has


maximum sales.
create view new as (
select sum(o.quantity) as "SUM",
o.book_id
from order_details o
group by o.book_id);

select a.aname
from author a,catalog c,new n
where n.book_id=c.book_id and
a.author_id=c.author_id and

n.sum in(select max(sum) from new);

5. Demonstrate how you increase the price


of books published by a specific publisher
by 10%.
UPDATE CATALOG
SET price = price*1.1
WHERE Publisher_id IN
( SELECT Publisher_id
FROM PUBLISHER

WHERE name='shiva');

6. Generate suitable reports.


ttitle 'STOCK DETAILS'
btitle 'BOOK DEALER DATABASE'
column book_id heading 'BOOK_ID'
column title heading 'BOOK_NAME'
column name heading 'AUTHOR_NAME'
column publisher_id heading
'PUBLISHER_NAME'
column price heading 'PRICE'
break on name skip 2
compute count of book_id on name
set linesize 100
set pagesize 40

select c.book_id, c.title, a.name,


p.name, c.price
from author a, publisher p, catalog c
where a.author_id=c.author_id and
p.publisher_id=c.publisher_id
order by p.name;

btitle off
ttitle off
clear breaks

clear columns
Question:
Consider the following database for a banking enterprise

BRANCH(branchname: string, branchcity:


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

1. Create the above tables by properly


specifying the primary keys and
the foreign keys
CREATE TABLE BRANCH (
branch_name varchar(20),
branch_city varchar(20),
assets real,
constraint bpk PRIMARY KEY(branch_name));

CREATE TABLE CUSTOMER(


customer_name varchar(20),
customer_street varchar(20),
customer_city varchar(20),
constraint cpk PRIMARY
KEY(customer_name));

CREATE TABLE ACCOUNT (


accno int,
branch_name varchar(20),
balance real,
constraint apk PRIMARY KEY(accno),
constraint afk FOREIGN KEY(branch_name)
references BRANCH(branch_name));

CREATE TABLE DEPOSITOR (


customer_name varchar(20),
Accno int,
constraint dpk PRIMARY KEY(customer_name,
accno),
constraint dfk1 FOREIGN
KEY(customer_name) references
CUSTOMER(customer_name),
constraint dfk2 FOREIGN KEY(accno)
references ACCOUNT(accno) On Delete
Cascade);

CREATE TABLE LOAN (


loan_number int,
branch_name varchar(20),
amount real,
constraint lpk PRIMARY KEY(loan_number),
constraint lfk FOREIGN KEY(branch_name)
references BRANCH(branch_name));

CREATE TABLE BORROWER (


customer_name varchar(20),
Loan_number int,
constraint bpk PRIMARY KEY(customer_name,
Loan_number), constraint bfk1 FOREIGN
KEY(customer_name) references
CUSTOMER(customer_name),
constraint bfk2 FOREIGN KEY(Loan_number)
references LOAN(Loan_number));

2. Enter at least five tuples for each


relation
INSERT INTO BRANCH
VALUES('&branch_name','&branch_city','&as
sets');

INSERT INTO CUSTOMER


VALUES('&customer_name','&customer_street
','&customer_city');

INSERT INTO ACCOUNT VALUES('&accno',


'&branch_name', '&balance');

INSERT INTO DEPOSITOR


VALUES('&customer_name', '&accno');

INSERT INTO LOAN VALUES('&loan_number',


'&branch_name', '&amount')

INSERT INTO BORROWER


VALUES('&customer_name', '&loan_number');
3. Find all the customers who have at least
two accounts at the Main branch.
SELECT D.customer_name
FROM DEPOSITOR D, ACCOUNT A
WHERE A.accno = D.accno AND
A.branch_name= 'rt nagar'
GROUP BY D.customer_name
HAVING COUNT(*) >= 2;

4. Find all the customers who have an


account at all the branches located in a
specific city.
SELECT DISTINCT D.customer_name
FROM DEPOSITOR D
WHERE NOT EXISTS (
( SELECT branch_name
FROM BRANCH
WHERE branch_city='mangaluru')
MINUS
(SELECT A.branch_name
FROM ACCOUNT A, DEPOSITOR K
WHERE A.accno = K.accno AND
D.customer_name=K.customer_name));

5. Demonstrate how you delete all account


tuples at every branch located in a specific
city.
DELETE
FROM ACCOUNT
WHERE branch_name IN
( SELECT branch_name
FROM BRANCH
WHERE branch_city = 'mysore');

6. Generate suitable reports.


ttitle 'LOAN DETAILS' btitle 'BANK
DATABASE' column cname heading 'CUSTOMER
NAME column lnum heading 'LOAN NUMBER'
column bname heading 'BRANCH NAME' column
amt heading 'LOAN AMOUNT' break on bname
skip 2 compute count of lnum on bname set
linesize 100 set pagesize 50

select c.cname, b.lnum, l.bname, l.amt


from customers c, loan l, borrower b
where c.cname=b.cname and l.lnum=b.lnum
order by l.bname;

btitle off
ttitle off
clear breaks
clear columns

You might also like