Database Applications Laboratory Manual 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. Department of Computer Science & Engineering Page 1
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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.
Department of Computer Science & Engineering Page 1
Database Applications Laboratory Manual
SQL>desc student;
Table Column Data Type Length Precision Scale Primary Key Nullable DefaultComment
STUDENT SNUM Number - 4 0 - - -
SNAME Varchar2 10 - - - - -
MAJOR Varchar2 5 - - - - -
SLEVEL Varchar2 7 - - - - -
AGE Number - - - - - -
SQL>insert into student('3','SUHAS','CS','FR','20');
SQL>select * from student;
SNUM SNAME MAJOR SLEVEL AGE
3 SUHAS CS FR 20
14 RAJESH CS JR 20
9 VINUTHA IS JR 22
2 VARSHA CS JR 20
5 SHRUTHI CS SO 20
6 VISHAK CS JR 20
7 VISHAL EC SR 21
10 AMIT EC SR 20
12 ATUL EC JR 20
SQL>desc enrolled;
TableColum
nData Type
Length
Precision
Scale
Primary Key
Nullable
Default
Comment
ENROLLED
SNUM Number - - - - - -
CNAME Varchar2 10 - - - - -
SQL>INSERT INTO ENROLLED VALUES(08,'SE');
SQL>SELECT * FROM ENROLLED;
Department of Computer Science & Engineering Page 2
Database Applications Laboratory Manual
SNUM CNAME
3 DS LAB
4 DS LAB
5 DS LAB
9 DBMS LAB
8 DS LAB
4 EC
3 DBMS LAB
7 EC
9 SE
2 DBMS LAB
2 DS LAB
8 SE
2 DBMS
2 C SHARP
6 DBMS LAB
9 EC
SQL>desc class;
Table
Column
Data Type
Length
Precision
Scale
Primary Key
Nullable
Default
Comment
CLASS
NAME Varchar2 10 - - - - -
MEETSAT
Varchar2 5 - - - - -
ROOM Varchar2 7 - - - - -
D Number - - - - - -
SQL>INSERT INTO CLASS VALUES('SE','9AM','R138',555);
SQL>SELECT * FROM CLASS;
NAME MEETSAT ROOM D
DBMS 11AM R128 333
SS 9AM R128 111
DS LAB 11AM R138 444
DBMS 1PM R128 555
EC 10AM R138 222
DBMS 11AM R128 333
DBMS 10AM R128 555
SE 9AM R138 555
Department of Computer Science & Engineering Page 3
Database Applications Laboratory Manual
SQL>desc faculty;
TableColum
nData Type
Length
Precision
Scale
Primary Key
Nullable
Default
Comment
FACULTY
FID Number - - - - - -
FNAME Varchar2 10 - - - - -
DEPTID Number - - - - - -
SQL>select * from faculty;
FID FNAME DEPTID
333 HARSHITH 2
555 HARSH 3
222 SNEHA 1
111 VANDANA 1
444 HARSHA 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 cwhere 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.namegroup by s.snum, sname, c.meetsathaving count(*)>1;
SNAME
VARSHA
Department of Computer Science & Engineering Page 4
Database Applications Laboratory Manual
iv) SQL>select fname from faculty f,class c where f.fid=c.dgroup by fid, fnamehaving count(distinct c.room) = (select count(distinct room) from class);
FNAME
HARSH
v) SQL>select f.fname from faculty f,class c,enrolled ewhere f.fid=c.d and c.name=e.cnamegroup by f.fid, f.fname having count (*)<5;
FNAME
HARSH
SNEHA
HARSHITH
Exercise 2:
Department of Computer Science & Engineering Page 5
Database Applications Laboratory Manual
The following relations keep track of airline flight information:
Flights (no: integer, from: string, to: string, distance: integer, Departs: time, arrives: time, price: real)
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>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;
ANAME AVG(SALARY)
998 87500
999 87500
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';
ENAMERAJESH
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');
AID
999
998
Department of Computer Science & Engineering Page 10
Database Applications Laboratory Manual
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)
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.
SQL> create table students (regno varchar(10)primary key,name varchar(10),major varchar(10),bdate date);
Table created.
SQL>create table courses(course_id int primary key,cname varchar(10),dept varchar(20));
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;
REGNO NAME MAJOR BDATE
123 akhilaa data stru 01-DEC-09
567 sinchana ada 01-DEC-09
345 anusha maths 01-DEC-09
234 arnitha data base 01-DEC-09
456 sneha microproce 01-DEC-09
Department of Computer Science & Engineering Page 12
Database Applications Laboratory Manual
Select * from courses;
COURSE# CNAME DEPT
5 Be tce
1 Be ise
2 Be CS
3 Be ece
4 Be CS
Select * from enroll;
Department of Computer Science & Engineering Page 13
REGNO COURSE# SEM MARKS
234 2 3 21
123 1 2 24
345 3 7 25
456 4 1 19
567 5 5 23
Database Applications Laboratory Manual
Select * from text;
BOOK_ISBN BOOK_TITLE PUBLISHER AUTHOR
765 Ada nandi padhma red
543 microproc mcgrachill douglashal
876 engee math prism dsc
123 let us c pearson yashwant
987 data struc nandi padhma red
654 data base pearson navathe
Select * from book_adoption;
Department of Computer Science & Engineering Page 14
Database Applications Laboratory Manual
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 BOOK_TITLE PUBLISHER AUTHOR
765 Ada nandi padhma red
543 microproc mcgrachill douglashal
876 engee math prism dsc
123 let us c pearson yashwant
987 data struc nandi padhma red
Department of Computer Science & Engineering Page 15
COURSE# SEM BOOK_ISBN
4 2 123
2 2 123
4 2 123
5 5 543
3 7 765
1 2 987
2 3 876
4 1 654
Database Applications Laboratory Manual
654 data base pearson navathe
444 Analog Pearson rajesh
SQL>insert into book_adoption values(3,4,444);
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.