You are on page 1of 14

COLLEGE DATABASE

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.

Schema Diagram STUDENT1


SNUM SNAME MAJOR LEVELS AGE

FACULTY
FID FNAME DEPTID

CLASS
CNAME MEETSAT ROOM FID

ENROLLED
SNUM CNAME

E R Diagram Create the above tables by properly specifying the primary keys and foreign keys.

create table student(snum integer primary key,sname varchar2(10),major varchar2(10),levels varchar2(2), age number(2)); Table Created create table faculty(fid number(5) primary key,fname varchar2(10),deptid number(5)); Table Created create table class(cname varchar2(10) primary key, meetsat varchar2(10),room varchar2(5),fid references faculty(fid)); Table Created create table enrolled(snum references student(snum),cname references class(cname)); Table Created Enter at least five tuples for each relation SQL> insert into student values(&snum,'&sname','&major','&levels',&age); SNUM --------101 102 103 104 105 SNAME ---------Liki Lavi Gopi chandu Lahi MAJOR ---------maths maths SE OR SE LEVELS -----------JR SR SR JR SR AGE --------21 22 27 20 27

SQL> insert into Faculty values(&fid,&fname,&deptid); FID FNAME DEPTID --------- ---------- --------501 harshith 401 502 sathya 402 503 ramya 401 504 srinath 405 505 Lahari 403 506 asha 404 507 usha 402 SQL> insert into CLASS values(&cname,'&meetsat','&room','&fid); CNAME MEETSAT ROOM FID

---------3a 3b 4a 4b 5a 5b 6a 6b

---------- ----- --------10.00 r128 10.00 r02 11.00 r03 11.00 r128 12.00 r04 11.00 r128 10.00 r02 12.00 r03

501 502 503 502 504 501 501 501

SQL> insert into ENROLLED values(&snum'&cname'); SNUM CNAME --------- ---------101 3a 101 3b 101 4a 101 4b 101 5a 101 5b 101 6a 101 6b 102 3a 102 3b 103 3a 104 4a 105 5a 101 5b 103 4b 105 3a 104 3a i)Find the names of all Juniors who are enrolled in a class start by prof .Harshit. SQL> select distinct sname from student s,enrolled e,class c,faculty f where s.snum=e.snum and e.cname=c.cname and c.fid=f.fid and fname='harshith' and levels='JR; SNAME ---------Liki Chandu

ii) Find the names of all classes that either meet in room R128 or have five or more
Students enrolled

SQL> select distinct c.cname from class c,enrolled e where c.cname=e.cname and room='r128' or c.cname in(select cname from enrolled group by cname having count(snum)>=5); CNAME ---------3a 4b 5b iii) Find the names of all students who are enrolled in two classes that meet at the same
time.

SQL> 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.cname=c1.cname and e2.cname=c2.cname and e1.snum=e2.snum and e1.cname <>c2.cname and c1.meetsat=c2.meetsat); SNAME ---------adi arul 4. Find the names of faculty members who teach in every room in which some class is
taught

SQL> select f.fname from faculty f where not exists((select c.room from class c)minus(select c1.room from class c1 where c1.fid=f.fid)); no rows selected 5. Find the names of faculty members for whom the combined enrollment of the courses
that they teach is less than five.

SQL> select distinct f.fname from faculty f where 5>(select count(e.snum) from class c,enrolled e where c.cname=e.cname and f.fid=c.fid); FNAME ----------

asha illangos kc nirmala sathya usha AIRCRAFT DATABASE


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 is certified for some aircraft, and only pilots are certified to fly. Write each of the following queries in SQL. i. ii. iii. iv. v. vi. Find the names of aircraft such that all pilots certified to operate them have salaries more than Rs.80, 000. 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. Find the names of pilots whose salary is less than the price of the cheapest route from Bengaluru to Frankfurt. 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. Find the names of pilots certified for some Boeing aircraft. Find the aids of all aircraft that can be used on routes from Bengaluru to New Delhi.

create table flight(no integer primary key,frm varchar(20),end varchar(20),dist integer,dept date,arr date,price real); create table aircraft(aid int primary key,aname varchar2(15),crange integer) create table certified(eid int,aid int,primary key(eid,aid)); create table employees(eid int primary key,ename varchar2(15),salary real); SQL> desc flight; Name Null? Type ----------------------------------------- -------- ---------------------------NO FRM END DIST DEPT ARR NOT NULL NUMBER(38) VARCHAR2(20) VARCHAR2(20) NUMBER(38) DATE DATE

PRICE

FLOAT(63)

SQL> desc aircraft; Name Null? Type ----------------------------------------- -------- ---------------------------AID ANAME CRANGE NOT NULL NUMBER(38) VARCHAR2(20) NUMBER(38)

SQL> desc certified; Name Null? Type ----------------------------------------- -------- ---------------------------EID AID NOT NULL NUMBER(38) NOT NULL NUMBER(38)

SQL> desc employees; Name Null? Type ----------------------------------------- -------- ---------------------------EID ENAME SALARY NOT NULL NUMBER(38) VARCHAR2(20) NUMBER(38)

SQL> select * from flight; NO FRM END DIST DEPT ARR PRICE ---------- -------------------- -------------------- ---------- --------- --------- ---------255 bangalore frnkfurt 200 01-AUG-11 01-AUG-11 5000 256 bangalore frnkfurt 200 01-AUG-11 01-AUG-11 8000 257 bangalore delhi 200 01-AUG-11 01-AUG-11 5000 258 bangalore delhi 200 01-AUG-11 01-AUG-11 6000 259 bangalore mangalore 200 01-AUG-11 01-AUG-11 8000 SQL> select * from aircraft; AID ANAME CRANGE ---------- -------------------- ---------685 boeing15 1000 686 boeing10 2000 687 skytrain 1000 688 avenger 100

SQL> select * from certified; EID AID ---------- ---------101 685 101 686 101 687 101 688 102 685 103 686 103 687 SQL> select * from employees; EID ENAME SALARY ---------- -------------------- ---------101 asha 90000 102 arun 85000 103 anand 3000 104 ramya 4000
i.Find the names of aircraft such that all pilots certified to operate them have salaries more than Rs.80, 000. SQL> 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); ANAME -------------------avenger boeing15 ii) 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. SQL> select c.eid,max(a.crange) from certified c,aircraft a where c.aid =a.aid group by c.eid having count(c.aid)>3; EID MAX(A.CRANGE) ---------- ------------101 2000 iii) Find the names of pilots whose salary is less than the price of the cheapest route from Bengaluru to Frankfurt. SQL> select e.ename from employees e where e.salary<(select min(price) from flight where frm='bangalore' and end='frnkfurt');

ENAME -------------------anand ramya 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. SQL> select name,avgsal from(select a.aid,a.aname as name,avg(e.salary) as avgsal from aircraft a, certified c,employees e where a.aid=c.aid and c.eid=e.eid and crange>1000 group by a.aid,a.aname); NAME AVGSAL -------------------- ---------boeing10 46500 v) Find the names of pilots certified for some Boeing aircraft. SQL> select distinct e.ename from employees e,certified c,aircraft a where a.aid=c.aid and c.eid =e.eid and a.aname like 'boeing%'; ENAME -------------------anand arun asha vi)Find the aids of all aircraft that can be used on routes from Bengaluru to New Delhi. SQL> select a.aid from aircraft a where a.crange>(select min(dist) from flight where frm='bangalore' and end='delhi'); AID ---------685 686 687

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. iii. iv. v. vi. vii.

Enter at least five tuples for each relation. Demonstrate how you add a new text book to the database and make this book be adopted by some department. 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. List any department that has all its adopted books published by a specific publisher. Generate suitable reports. Create suitable front end for querying and displaying the results.

SCHEMA DIAGRAM ER DIAGRAM i)Create the above tables by properly specifying the primary keys and the foreign keys. create table STUDENT(regno varchar2(10)primary key,name varchar2(10)not null,major varchar2(8)not null,bdate date not null); Table Created create table COURSE(courseno number(4)primary key,cname varchar2(10)not null,dept varchar2(10)not null); Table Created create table ENROLL(regno references STUDENT(regno),courseno COURSe(courseno),sem number(4),marks number(3)not null); Table Created references

create table TEXT(book_isbn number(4)primary key,book_title varchar2(10)not null,publisher varchar2(10)not null,author varchar2(10)not null); Table Created create table BOOK_ADOPTION(courseno references COURSE(courseno),sem number(4)not null,book_isbn number(4)not null); Table Created ii) Enter at least five tuples for each relation. Insert into STUDENT values('&regno','&name','&major','&bdate'); select * from student7; REGNO NAME MAJOR BDATE ---------- ---------- ---------- --------111 ashu mca 25-JUL-87 112 thej mca 02-MAR-09 113 shru be 20-NOV-88 114 kiran ca 17-JUL-85 115 rams msw 12-MAR-82 116 ammu mtech 23-JUN-86

Insert into COURSE values('&courseno','&cname','&dept'); COURSENO CNAME DEPT ------- ---------- -------21 dbms mca 22 or mca 23 autocad be 24 mis mtech 25 account ca 26 psychology msw Insert into ENROLL values('&regno','&courseno,'&sem','&marks'); REGNO COURSE NO SEM MARKS ---------- --------- --------- --------113 24 2 78 111 22 3 79 112 21 3 90 114 23 1 89 115 25 2 69 116 26 5 66 4) Insert into TEXT values('&book_isbn','&'book_title,'&publisher','&author'); BOOK_ISBN BOOK_TITLE PUBLISHER AUTHOR ----- ---------- ---------- ---------31 dbms pearson navarthe 32 or mcgrawhill rajgopal 33 psychology himalaya shridhar 34 account manglore cgowda 35 mis ktaka prabhu 36 autocad dhamdhere harvey 30 simulation john jerry

Insert into BOOK_ADOPTION values('&courseno','&sem','&book_isbn'); COURSENO SEM BOOK_ISBN ------ --------- --------22 3 31 21 3 33 23 1 34 24 2 32 25 2 36 26 5 35 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. SCHEMA DIAGRAM ER DIAGRAM

SQL> desc AUTHOR; Name Null? Type ----------------------------------------- -------- ---------------------------AID NOT NULL NUMBER(38) NAME VARCHAR2(20) CITY VARCHAR2(10) COUNTRY VARCHAR2(10) SQL> desc PUBLISHER; Name Null? Type ----------------------------------------- -------- ---------------------------PID NOT NULL NUMBER(38) NAME VARCHAR2(20) CITY VARCHAR2(10) COUNTRY VARCHAR2(10) SQL> desc CATALOG; Name Null? Type ----------------------------------------- -------- ---------------------------BID NOT NULL NUMBER(38) TITLE VARCHAR2(15) AID NUMBER(38) PID NUMBER(38) CID NUMBER(38) YEAR NUMBER(38) PRICE NUMBER(38) SQL> desc CATEGORY; Name Null? Type ----------------------------------------- -------- ----------------------------

CID DESCRIPTION

NOT NULL NUMBER(38) VARCHAR2(20)

SQL> desc ORDERDETAILS: SP2-0565: Illegal identifier. SQL> desc ORDERDETAILS; Name Null? Type ----------------------------------------- -------- ---------------------------ONO NOT NULL NUMBER(38) BID NOT NULL NUMBER(38) QUANTITY NUMBER(38) ii) Enter at least five tuples for each relation. Insert into AUTHOR values('&aid','&name','&city','&country'); select * from AUTHOR; AID NAME CITY COUNTRY ---------- -------------------- ---------- ---------1 godse blore INDIA 2 M.V.Rao blore INDIA 3 padmareddy NY INDIA 4 SOMERVILLEY NY USA 5 DSC MYSORE INDIA / insert into PUBLISHER values('&pid','&name','&city','&country'); select * from publisher; PID NAME CITY COUNTRY --------- -------------------- ---------- ---------11 php blore india 12 php chicago usa insert into CATEGORY values('&cid','&description') SQL> select * from CATEGORY; CID DESCRIPTION ---------- -------------------101 CS 102 EC 103 IS 104 MCA 105 CS insert into CATALOG values('&bid','&title','&aid','&pid','&cid','&year','&price') SQL> select * from CATALOG; BID TITLE AID PID CID YEAR PRICE

---------- --------------- ---------- ---------- ---------- --------1111 ADA 3 11 101 2001 1112 CG 1113 DBMS 2 4 12 11 102 103 2000 2002

500 300 440

insert into ORDERDETAILS values('&ONO','&bid','&quantity') SQL> select * from orderdetails; ONO BID QUANTITY ---------- ---------- ---------1 1111 2500 2 1113 4000 3 1112 3700 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. SQL> select * from AUTHOR where AID in (select AID from CATALOG where(year>'2000') and price>(select AVG(price) from CATALOG) group by aid having count(*)>=2); iv)Find the author of the book which has maximum sales. SQL> select a.name,c.title from author a,catalog c where a.aid=c.aid and c.bid in (select bid from orderdetails group by bid having sum(quantity)>=ALL (select sum(quantity) from orderdetails group by bid)); NAME TITLE -------------------- --------------SOMERVILLEY DBMS v)Demonstrate how you increase the price of books published by a specific publisher by 10%. SQL> update catalog set price=price*0.1+price where pid in(select pid from publisher where name='php'); 3 rows updated. SQL> select * from catalog; BID TITLE AID PID CID YEAR PRICE ---------- --------------- ---------- ---------- ---------- --------1111 ADA 3 11 101 2001 550 1112 CG 1113 DBMS 2 4 12 11 102 103 2000 330

2002 484

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. Create the above tables by properly specifying the primary keys and the foreign keys ii. Enter at least five tuples for each relation iii. Find all the customers who have at least 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. vi. Generate suitable reports. vii. Create suitable front end for querying and displaying the results. i)Create the above tables by properly specifying the primary keys and the foreign keys.

You might also like