Escolar Documentos
Profissional Documentos
Cultura Documentos
Exercise 1:
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. i. Find the names of all juniors (level = JR) who are enrolled in a class taught by Prof. Harshith ii. Find the names of all classes that either meet in room R128 or have five or more Students enrolled. iii. Find the names of all students who are enrolled in two classes that meet at the same time. iv. Find the names of faculty members who teach in every room in which some class is taught. v. Find the names of faculty members for whom the combined enrollment of the courses that they teach is less than five.
Page 1
SQL>desc student;
Com ment
-
Table
STUDENT
Column
SNUM SNAME MAJOR SLEVEL AGE
Dat a T ype
Number Varchar2 Varchar2 Varchar2 Number
Lengt h
10 5 7 -
Precisi on
4 -
Scale
0 -
Primar y Key
-
Nullabl e
Def ault
-
SNAME
SUHAS RAJESH VINUTHA VARSHA SHRUTHI VISHAK VISHAL AMIT ATUL
MAJOR
CS CS IS CS CS CS EC EC EC
SLEVEL
FR JR JR JR SO JR SR SR JR
AGE
20 20 22 20 20 20 21 20 20
SQL>desc enrolled;
Table
ENROLLED
Column
SNUM CNAME
Dat a T ype
Number Varchar2
Lengt h
10
Precisi on
-
Scale
-
Primar y Key
-
Nullabl e
Def ault
-
Comment
-
SNUM
3 4 5 9 8 4 3 7 9 2 2 8 2 2 6 9
CNAME
DS LAB DS LAB DS LAB DBMS LAB DS LAB EC DBMS LAB EC SE DBMS LAB DS LAB SE DBMS C SHARP DBMS LAB EC
SQL>desc class;
Tabl e
CLAS S
Colum n
NAME MEETSA T ROOM D
Dat a Type
Varchar2 Varchar2 Varchar2 Number
Lengt h
10 5 7 -
Precisi o n
-
Scal e
-
Primar y Key
-
Nullabl e
Def au lt
-
Comme nt
-
MEETS AT
11AM 9AM 11AM 1PM 10AM 11AM 10AM 9AM
ROOM
R128 R128 R138 R128 R138 R128 R128 R138
D
333 111 444 555 222 333 555 555
Page 3
SQL>desc faculty;
Table
FACULT Y
Colum n
FID FNAME DEPTID
Dat a Type
Number Varchar2 Number
Lengt h
10 -
Precisi o n
-
Scal e
-
Primar y Key
-
Nullabl e
Def au lt
-
Comme nt
-
FNAME
HARSHITH HARSH SNEHA VANDANA HARSHA
DEPTID
2 3 1 1 2
i)
SQL>select distinct sname from student s,enrolled e,class c,faculty f where s.slevel='JR' and s.snum=e.snum and e.cname=c.name and c.d=f.fid and f.fname='HARSHITH';
SNAME
VARSHA
ii)
SQL>select name from class c where c.room='R128' UNION (select cname from enrolled group by cname having count(*)>=5);
DBMS DS LAB SS
iii)
SQL>select sname from student s,enrolled e,class c where s.snum=e.snum and e.cname=c.name group by s.snum, sname, c.meetsat having count(*)>1;
Page 4
SNAME
VARSHA
iv)
SQL>select fname from faculty f,class c where f.fid=c.d group by fid, fname having count(distinct c.room) = (select count(distinct room) from class);
FNAME
HARSH
v)
SQL>select f.fname from faculty f,class c,enrolled e where f.fid=c.d and c.name=e.cname group by f.fid, f.fname having count (*)<5;
FNAME
HARSH SNEHA HARSHITH
Page 5
Exercise 2:
The following relations keep track of airline flight information: Flights (no: integer, from: string, to: 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 certified for some aircraft, and only pilots are certified to fly. Write each of the following queries in SQL. i) Find the names of aircraft such that all pilots certified to operate them have salaries more than Rs.80, 000. ii) For each pilot who is certified for more than three aircrafts, find the eid and the maximum cruising range of the aircraft for which she or he is certified. iii) Find the names of pilots whose salary is less than the price of the cheapest route from Bengaluru to Frankfurt. iv) For all aircraft with cruisingrange over 1000 Kms, .find the name of the aircraft and the average salary of all pilots certified for this aircraft. v) Find the names of pilots certified for some Boeing aircraft. vi) Find the aids of all aircraft that can be used on routes from Bengaluru to New Delhi.
SQL>create table flights(no number, source varchar(10),dest varchar(10),distance number, departs timestamp, arrives timestamp, price decimal(10,2)); Table created. SQL>create table aircraft(aid number,aname varchar(10),cruisingrange number); Table created. SQL>create table certified(eid number,aid number);
Department of Computer Science & Engineering Page 6
Table created. SQL>create table employees(eid number,ename varchar(10),salary number); Table created. SQL>desc flights;
Co mm ent
-
Table
FLIGHTS
Column
NO SOURCE DEST DISTANCE DEPARTS ARRIVES PRICE
Dat a T ype
Number Varchar2 Varchar2 Number Timestamp(6) Timestamp(6) Number
Lengt h
10 10 11 11 -
Precisi on
10
Scale
6 6 2
Primar y Key
-
Nullabl e
Def ault
-
NO
100 112 102 111 101 110
SOURCE
BANGALURU BANGALURU BANGALURU BANGALURU BANGALURU BANGALURU
DEST
FRANKFURT NEW DELHI DUBAI MUMBAI NEW YORK FRANKFURT
DIST ANCE
90000 1000 30000 1500 90000 90000
DEP ARTS
09-SEP-12 01.00.00.000000 AM 11-SEP-12 09.00.00.000000 AM 13-SEP-12 05.00.00.000000 AM 11-SEP-12 10.00.00.000000 AM 12-SEP-12 02.00.00.000000 PM 14-SEP-12 02.00.00.000000 AM
ARRI VES
10-SEP-12 06.00.00.000000 PM 11-SEP-12 12.00.00.000000 PM 13-SEP-12 11.00.00.000000 AM 11-SEP-12 01.00.00.000000 PM 13-SEP-12 05.00.00.000000 PM 15-SEP-12 07.00.00.000000 PM
PRICE
50000 6000 15000 6200 60000 40000
Page 7
SQL>desc aircraft;
AIRCRAFT AID ANAME CRUISINGRANGE Number Varchar2 Number 10 -
AN AME
FLIGHT FLIGHT BOEING BOEING
CRUI SINGRANGE
11000 21000 900 800
SQL>desc certified;
Table
CERTIFI ED
Colum n
EID AID
Dat a Type
Number Number
Lengt h
-
Precisi on
-
Scal e
-
Primar y Key
-
Nullab le
Def au lt
-
Comme nt
-
AI D
888 998 812 999 812 999 998 888
Page 8
SQL>desc employees;
EMPLOYEES EID ENAME SALARY Number Varchar2 Number 10 -
ENAME
ANJU CHITRA RAVI RAJ RAJESH KIRAN RAMESH
SAL ARY
35000 10000 25000 85000 90000 15000 30000
i) SQL> select a.aid, a.aname from aircraft a, certified c, employees e where a.aid=c.aid and c.eid=e.eid and e.salary>80000 group by a.aid,a.aname having count(*)=(select count(*) from employees e where e.salary>80000)
AI D
999 998
AN AME
FLIGHT FLIGHT
ii) SQL>Select c.eid, MAX(a.cruisingrange)from certified c, aircraft a where c.aid=a.aid group by c.eid having count(*)>3;
EID
111
WHERE e.eid=c.eid and E.SALARY < ( SELECT MIN (F.PRICE)FROM FLIGHTS F WHERE F.SOURCE = 'BANGALURU' AND F.DEST = 'FRANKFURT');
ENAME
ANJU
iv) SQL> create table temp(aname varchar(20),salary number); SQL>insert into temp(aname,salary) select a.aid, e.salary From AIRCRAFT A,CERTIFIED C,EMPLOYEES E WHERE A.CRUISINGRANGE>1000 AND A.AID=C.AID AND C.EID=E.EID ; SQL>select aname,avg(salary) from temp group by aname;
AN AME
998 999
v) SQL>SELECT DISTINCT E.ENAME FROM EMPLOYEES E, CERTIFIED C, AIRCRAFT A WHERE E.EID = C.EID AND C.AID = A.AID AND A.ANAME = 'BOEING'; ENAME
RAJESH ANJU
vi) SQL>SELECT A.AID FROM AIRCRAFT A WHERE A.CRUISINGRANGE > (SELECT MIN (F.DISTANCE) FROM FLIGHTS F WHERE F.SOURCE = 'BANGALURU'AND F.DEST = 'NEW DELHI');
AI D
999 998
Page 10
Exercise 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 (course #:int, cname:string, dept:string) ENROLL ( regno:string, course#:int, sem:int, marks:int) BOOK _ ADOPTION (course# :int, sem:int, book-ISBN:int) TEXT (book-ISBN:int, book-title:string, publisher:string, author:string) i. Create the above tables by properly specifying the primary keys and the foreign keys. ii. Enter at least five tuples for each relation. iii. Demonstrate how you add a new text book to the database and make this book be adopted by some department. iv. 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. v. List any department that has all its adopted books published by a specific publisher. vi. Generate suitable reports. vii. Create suitable front end for querying and displaying the results.
1. Create the above tables by properly specifying the primary keys and the foreign keys.
(regno
varchar(10)primary
key,name
varchar(10),major
SQL>create table courses(course_id int primary key,cname varchar(10),dept varchar(20)); Table created. SQL>create table enrolls(regno varchar(10),course_id int,se mint,marks int,primary key(regno)references students(regno),foreign key(course_id)references courses(course_id));
Page 11
Table created. SQL>create table texts(book_isbn int primary key,book_title varchar(15),publisher varchar(10),author varchar(15)); Table created. SQL>create table book_adoptions(course_id int,sem int,book_isbn int,primary key(course_id,sem),foreign key(course_id)references courses(course_id),foreign key(book_isbn)references texts(book_isbn)); Table created. 2. Enter at least five tuples for each relation SQL>insert into students values (®no,&name,&major,&date); SQL>insert into COURSES values (&course_id,&cname,&dept); SQL>insert into text values (&book_isbn,&book_title,&publisher,&author); SQL>insert into book_adoption values (&course_id,&sem,&book_isbn); SQL>select * from STUDENTS;
Page 12
COURSE# 5 1 2 3 4 Be Be Be Be Be
DEPT
COURSE# 3 2 7 1 5
SEM
MARKS 21 24 25 19 23
Page 13
BOOK_TITLE Ada microproc engee math let us c data struc data base
COURSE# 4 2 4 5 3 1 2 4 2 2 2 5 7 2 3 1
SEM
Page 14
3. 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 ('444','analog','pearson','rajesh'); Select * from text;
BOOK_ISBN 765 543 876 123 987 654 444 BOOK_TITLE Ada microproc engee math let us c data struc data base Analog PUBLISHER nandi mcgrachill prism pearson nandi pearson Pearson AUTHOR padhma red douglashal dsc yashwant padhma red navathe rajesh
SQL>insert into book_adoption values(3,4,444); COURSE# 4 2 4 5 3 1 2 4 3 2 2 2 5 7 2 3 1 4 SEM BOOK_ISBN 123 123 123 543 765 987 876 654 444
Page 15
4. 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.
select distinct c.course#,t.book_isbn,t.book_title from text t,book_adoption b,course c where c.dept='CS' and c.course#=b.course# and b.book_isbn=t.book_isbn and c.couse# in (select course# from book_adoption b group by course# having count(*)>2) order by t.book_title;
5. List any department that has all its adopted books published by a specific publisher.
COURSE#
765 543 876 123
BOOK_TITLE
nandi mcgrachill prism pearson
CNAME
padhma red douglashal dsc yashwant
Page 16
Exercise 4:
The following tables are maintained by a book dealer. AUTHOR (author-id:int, name:string, city:string, country:string) PUBLISHER (publisher-id:int, name:string, city:string, country:string) CATALOG (book-id:int, title:string, author-id:int, publisher-id:int, category-id:int, year:int, price:int) CATEGORY (category-id:int, description:string) ORDER-DETAILS (order-no:int, book-id:int, quantity:int)
i. Create the above tables by properly specifying the primary keys and the foreign keys. ii. Enter at least five tuples for each relation. iii. 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. iv. Find the author of the book which has maximum sales. v. Demonstrate how you increase the price of books published by a specific publisher by 10%. vi. Generate suitable reports. vii. Create suitable front end for querying and displaying the results.
1. Create the above tables by properly specifying the primary keys and the foreign keys. Create table authors(aid int primary key,name varchar(15),city varchar(15),country varchar(15)); Table created. Create table publishers(pi dint primary key,name varchar(15),city varchar(15),country varchar(15)); Table created.
Page 17
Create table categorys(cid int primary key,description varchar(15)); Table created. Create table catalogs(bid int primary key,title varchar(15),aid int,pi dint,cid int,year int,price int Foreign key(aid)references authors(aid), Foreign key(pid)referencespublishers(pid), Foreign key(cid)references categorys(cid)); Table created. Create table order_details(orderno int,bid int,qty int,primary key(orderno,bid),foreign key(bid) references catalogs(bid)); Table created. 2. Enter at least five tuples for each relation. SQL>desc authors;
Column
Length
Page 18
AID 5 2 1 3
SQL>desc publishers;
Column
Length
SQL>select * from publishers; SQL>insert into categorys values(&cid,&description); SQL>select * from categorys; SQL>insert into catalogs values(&bid, &title, &aid, &pid, &cid, &year,&price);
Page 19
SQL>select * from catalogs; SQL>insert into order_details values(&orderno,&bid,&qty); SQL>select * from order_details; 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. SQL>select distinct a.aid,a.name,a.city,a.country from authors a, catalogs c where a.aid=c.aid and c.price>(select avg(price) from catalogs) and c.year >2000 and a.aid=(select aid from catalogs group by aid having count(*)>=2);
AID 5 CITY KENGARI HOSUR
COUNTRY
INDIA INDIA
4. Find the author of the book which has maximum sales. SQL>select name, qty from order_details o,authors a,catalogs c where o.bid=c.bid and c.aid=a.aid and qty in(select max(qty) from order_details);
NAME
QTY
ASHOKA
5. Demonstrate how you increase the price of books published by a specific publisher by 10%.
Page 20
SQL>Update catalogs set price=price*0.1+price where pid in(select pid from publisher where name=pearson); Two rows updated
Page 21
Exercise 5:
Consider the following database for a banking enterprise. BRANCH (branch-name: string, branch-city: string, assets: real) ACCOUNT (accno: int, branch-name: string, balance: real) DEPOSITOR (customer-name: string, accno: int) CUSTOMER (customer-name: string, customer-street: string, customer-city: string) LOAN (loan-number: int, branch-name: string, amount: real) BORROWER (customer-name: string, loan-number: int) (i) (ii) Create the above tables by properly specifying the primary keys and foreign keys. Enter atleast five tuples for each relation.
(iii) Find all the customers who have atleast two accounts at the main branch. (iv) Find all the customers who have an account at all the branches located in a specific city. (v) Demonstrate how you delete all account tuples at every branch located in a specific city.
Create table branch(bname varchar(20) primary key, bcity varchar(20),assets real); Table created. Create table account(accno int primary key, bname varchar(20),balance real,foreign key (bname) references branch(bname)); Table created. Create table customer(cname varchar(20) primary key, cstreet varchar(20),city varchar(20)); Table created. create table depositor(cname varchar(20), accno int,primary key(cname,accno),foreign key (cname) references customer(cname),foreign key (accno) references account(accno));
Page 22
Table created. Create table loan(lnumber int primary key, bname varchar(20),amount real,foreign key (bname) references branch(bname)); Table created. Create table borrower(cname varchar(20),lnumber int,foreign key (cname) references customer(cname),foreign key(lnumber) references loan(lnumber)); Table created. insert into branch values('jayanagar','bangalore',1500) select * from branch
BNAME jayanagar majestic jubleehills panjagutta krpuram BCITY bangalore bangalore hyderabad hyderabad chennai ASSETS 1500 2600 4000 3500 1020
insert into account values(5600,'krpuram',25000) Select * from account ACCNO 5467 5060 5065 BNAME jubleehills majestic jayanagar BALANCE 16000 10000 20000
Page 23
select * from customer CNAME sudhakar naveen naveen j pradeep dilip CSTREET udayanagar road market road bagalur road pujagutta road band box road CITY bangalore bangalore chennai hyderabad bomanahalli
insert into depositor values('naveen j',5600) select * from depositor CNAME dilip dilip naveen naveen j ACCNO 5467 5468 5111 5600
Page 24
insert into loan values(10,'majestic',150000) select * from loan ACCNO 15 10 18 16 BNAME jubleehills majestic jayanagar krpuram BALANCE 1000000 150000 150000 200000
insert into borrower values('sudhakar',10) select * from borrower CNAME dilip naveen naveen j sudhakar LNUMBER 15 15 16 10
Page 25
3. SELECT d.CNAME FROM DEPOSITOR D,ACCOUNT A,CUSTOMER C, branch b WHERE D.ACCNO=A.ACCNO AND C.CNAME=D.CNAME AND A.BNAME=B.BNAME AND A.BNAME IN (SELECT BNAME FROM BRANCH WHERE BCITY='bangalore') GROUP BY d.CNAME HAVING COUNT(DISTINCT B.BNAME)>=2;
CNAME
sudhakar
4. Select cname from customer c where not exists(select bname from branch where city=bangalore minus select bname from depositor d,account a where d.accno=a.accno and d.cname=c.cname) and exists (select bname from branch where city=bangalore);
bNAME mejestic
5. Delete from account where bname in (select bname from branch where city=chennai);
Page 26