0% found this document useful (0 votes)
44 views38 pages

DBMS - Lab

Find the names of all Juniors (level = JR) who are enrolled in a class taught by Prof. Harshith. Find the names of all classes that either meet in room R128 or have five or more students enrolled. Find the names of all students who are enrolled in two classes that meet at the same time.

Uploaded by

reyojiw404
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views38 pages

DBMS - Lab

Find the names of all Juniors (level = JR) who are enrolled in a class taught by Prof. Harshith. Find the names of all classes that either meet in room R128 or have five or more students enrolled. Find the names of all students who are enrolled in two classes that meet at the same time.

Uploaded by

reyojiw404
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

B.M.S.

COLLEGE OF ENGINEERING
(Autonomous College, Affiliated to VTU)
Bull Temple Road, Bangalore-560019

ME

Laboratory Certificate

This is to certify that Mr. Kavan H.M (USN: 1BM23MC044) has


satisfactorily completed the integrated course of practical in Database
Management Systems (22MCA1PCDB) Laboratory, first semester MCA course
in this college during the year 2023-2024.

Signature of Batch In-charge Signature of HOD

Student Name : Kavan H.M


USN :1BM23MC044
Table Content
Sl. Page
Details
No. No:
Consider the following relations. 01-06

Student (snum: integer, sname: string, major: string, level: string, age: integer)
Class (cname: string, meets at: string, room: string, fid: 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.
1.
i. Find the names of all juniors (level=JR) who are enrolled in a class DBMS taught by
Prof.Harshith.
ii. Find the names of all classes that either meet in room R128 or have five or more
students.
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 enrolment of the courses
that they teach is less than five.

The following relations keep track of airline flight information 07-13

Flight(flno:integer,fromplace:string,toplace:string,distance:integer,departs:time,arrives
:time price:integer)
Aircraft(aid:intger,aname:string,cruiserange:integer)
Certified(eid: integer,aid: intger)
Employees(eid: integer,ename:string,salary:intger)
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.


2.
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 two air-crafts, find the eid and the
maximum cruisingrange 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 Bangalore to Frankfurt.
iv. For all aircraft form cruisingrange over 1000Kms.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 bengalore to delhi.
3. Consider the following database of Student Enrollment in courses & books 18-23
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, book-ISBN: int)
TEXT (book-ISBN: int, book-title: string, pub: 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.
4. The following tables are maintained by a book dealer. 24-32

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, pub-id: int, cat-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 the books published by a specific
publisher by 10%..
5. Consider the following database for a banking enterprise 33-39

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 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 to all the branches located in a
specific city.
v. Demonstrate how u 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.
Database Management Systems 22MCA1PCDB

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.


TABLE CREATION
CREATE TABLE Student (
snum INT primary key,
sname VARCHAR(30),
major VARCHAR(20),
level1 varchar(20),
age INT,
);

CREATE TABLE Faculty (


fid INT primary key,
name VARCHAR(30),
deptid INT,
);

CREATE TABLE Class (


cname VARCHAR(30) primary key,
meets_at VARCHAR(30),
room VARCHAR(30),
fid INT,
FOREIGN KEY (fid) REFERENCES Faculty(fid)
);

Department of Computer Applications, BMSCE Page 5


Database Management Systems 22MCA1PCDB
CREATE TABLE Enrolled (
snum INT,
cname VARCHAR(30),
FOREIGN KEY (snum) REFERENCES Student(snum),
FOREIGN KEY (cname) REFERENCES Class(cname)
);

INSERTING VALUES
INSERT INTO STUDENT values (1001,'Kavan','cs','jr',21);
Query OK, 1 row affected (0.01 sec)
INSERT INTO STUDENT values (1002,'Bhuvan','cs','sr',22);
Query OK, 1 row affected (0.00 sec)
INSERT INTO STUDENT values (1003,'Bharat','CA','jr',21);
Query OK, 1 row affected (0.00 sec)
INSERT INTO STUDENT values (1004,'Suman','cs','SR',23);
Query OK, 1 row affected (0.00 sec)
INSERT INTO STUDENT values (1005,'Aditya','DS','jr',21);
Query OK, 1 row affected (0.00 sec)

select * from student;


+------+--------+-------+------+------+
| snum | sname | major | level1 | age |
+------+--------+-------+------+------+
| 1001 | Kavan | cs | jr | 21 |
| 1002 | Bhuvan | cs | sr | 22 |
| 1003 | Bharat | CA | jr | 21 |
| 1004 | Suman | cs | SR | 23 |
| 1005 | Aditya | DS | jr | 21 |
+------+--------+-------+------+------+
5 rows in set (0.00 sec)

INSERT INTO faculty values (1,'Harshit',101);


Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO faculty values (2,'Hari',102);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO faculty values (3,'Sham',103);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO faculty values (4,'Ankita',104);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO faculty values (5,'Anish',105);
Query OK, 1 row affected (0.00 sec)

Department of Computer Applications, BMSCE Page 6


Database Management Systems 22MCA1PCDB
select * from faculty;
+-----+-----------+--------+
| fid | name | deptid |
+-----+-----------+--------+
| 1 | Harshit | 101 |
| 2 | Hari | 102 |
| 3 | Sham | 103 |
| 4 | Ankita | 104 |
| 5 | Anish | 105 |
+-----+-----------+--------+
5 rows in set (0.00 sec)

INSERT INTO class values ('DBMS','12pm','r128',1);


Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO class values ('SE','1pm','r120',2);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO class values ('OS','2pm','CA1',3);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO class values ('WEB','3pm','CA2',4);
Query OK, 1 row affected (0.00 sec)
mysql> select * from class;
+-------+----------+------+------+
| cname | meets_at | room | fid |
+-------+----------+------+------+
| DBMS | 12pm | r128 | 1 |
| SE | 1pm | r120 | 2 |
| OS | 2pm | CA1 | 3 |
| WEB | 3pm | CA2 | 4 |
+-------+----------+------+------+
4 rows in set (0.00 sec)

INSERT INTO enrolled values (1001,'DBMS');


Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO enrolled values (1002,'SE');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO enrolled values (1003,'OS');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO enrolled values (1004,'WEB');
Query OK, 1 row affected (0.00 sec)
mysql> select * from enrolled;
+------+-------+
| snum | cname |
+------+-------+
| 1001 | DBMS |
| 1002 | SE |
| 1003 | OS |
| 1004 | WEB |

Department of Computer Applications, BMSCE Page 7


Database Management Systems 22MCA1PCDB
+------+-------+
4 rows in set (0.00 sec)

QUERIES
i Find the names of all 1st sem who are enrolled in a class taught by Harshith.

SELECT DISTINCT sname


-> FROM Student
-> JOIN Enrolled ON Student.snum = Enrolled.snum
-> JOIN Class ON Enrolled.cname = Class.cname
-> JOIN Faculty ON Class.fid = Faculty.fid
-> WHERE Student.sem = 'JR' AND Faculty.name = 'Harshit';
+--------+
| sname |
+--------+
| Kavan |
+--------+
1 row in set (0.00 sec)

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

SELECT DISTINCT cname


-> FROM Class
-> WHERE room = 'r128'
-> OR (
-> SELECT COUNT(*)
-> FROM Enrolled
-> WHERE Enrolled.cname = Class.cname
-> ) >= 5;
+-------+
| cname |
+-------+
| DBMS |
+-------+
1 row in set (0.00 sec)

Department of Computer Applications, BMSCE Page 8


Database Management Systems 22MCA1PCDB
iii.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.meets_at=c2.meets_at;
+--------+
| sname |
+--------+
| Kavan |
| Bhuvan |
| Bharat |
| Suman |
| Aditya|
+--------+
1 rows in set (0.00 sec)

IV. Find the names of faculty members who teach in every room in which some class is taught.
SELECT f.fname,f.fid
-> FROM faculty f
-> WHERE f.fid in ( SELECT fid FROM class
-> GROUP BY fid HAVING COUNT(*)=(SELECT COUNT(DISTINCT room)
-> FROM class) );
+--------+-----+
| fname | fid |
+--------+-----+
| Harshit | 1 |
+--------+-----+
1 row in set (0.00 sec)

V.Find the names of faculty members for whom the combined enrollment of the courses that they teach is
less than five
SELECT F.name
-> FROM Faculty F
-> WHERE (
-> SELECT COUNT(*)
-> FROM Class C
-> JOIN Enrolled E ON C.cname = E.cname
-> WHERE C.fid = F.fid
-> ) < 5;
+-----------+
| name |
+-----------+
| Harshit |
| Hari |
| Sham |
| Ankita |
| Anish |
+-----------+

Department of Computer Applications, BMSCE Page 9


Database Management Systems 22MCA1PCDB

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. 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
cruisingrange 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.

TABLE CREATION

CREATE TABLE flight(


-> no INT,
-> frm VARCHAR(20),
-> too VARCHAR(20),
-> distance INT,
-> departs VARCHAR(20),
-> arrives VARCHAR(20),
-> price REAL,
-> PRIMARY KEY (no) );
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE aircraft(


->
-> aid INT,
-> aname VARCHAR(20),
-> cruisingrange INT,
-> PRIMARY KEY (aid) );
Query OK, 0 rows affected (0.01 sec)

Department of Computer Applications, BMSCE Page 10


Database Management Systems 22MCA1PCDB
mysql> CREATE TABLE employees(
-> eid INT,
-> ename VARCHAR(20),
-> salary INT,
-> PRIMARY KEY (eid) );
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE certified(


-> eid INT,
-> aid INT,
-> PRIMARY KEY (eid,aid),
-> FOREIGN KEY (eid) REFERENCES employees (eid),
-> FOREIGN KEY (aid) REFERENCES aircraft (aid) );
Query OK, 0 rows affected (0.01 sec)

Inserting values
INSERT INTO flight (no,frm,too,distance,departs,arrives,price) VALUES
-> (1,'Bangalore','Mangalore',360,'10:45:00','12:00:00',10000),
-> (2,'Bangalore','Delhi',5000,'12:15:00','04:30:00',25000),
-> (3,'Bangalore','Mumbai',3500,'02:15:00','05:25:00',30000),
-> (4,'Delhi','Mumbai',4500,'10:15:00','12:05:00',35000),
-> (5,'Delhi','Frankfurt',18000,'07:15:00','05:30:00',90000),
-> (6,'Bangalore','Frankfurt',19500,'10:00:00','07:45:00',95000),
-> (7,'Bangalore','Frankfurt',17000,'12:00:00','06:30:00',99000);
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> select * from flight;
+----+-----------+-----------+----------+----------+----------+-------+
| no | frm | too | distance | departs | arrives | price |
+----+-----------+-----------+----------+----------+----------+-------+
| 1 | Bangalore | Mangalore | 360 | 10:45:00 | 12:00:00 | 10000 |
| 2 | Bangalore | Delhi | 5000 | 12:15:00 | 04:30:00 | 25000 |
| 3 | Bangalore | Mumbai | 3500 | 02:15:00 | 05:25:00 | 30000 |
| 4 | Delhi | Mumbai | 4500 | 10:15:00 | 12:05:00 | 35000 |
| 5 | Delhi | Frankfurt | 18000 | 07:15:00 | 05:30:00 | 90000 |
| 6 | Bangalore | Frankfurt | 19500 | 10:00:00 | 07:45:00 | 95000 |
| 7 | Bangalore | Frankfurt | 17000 | 12:00:00 | 06:30:00 | 99000 |
+----+-----------+-----------+----------+----------+----------+-------+
7 rows in set (0.00 sec)

Department of Computer Applications, BMSCE Page 11


Database Management Systems 22MCA1PCDB
INSERT INTO aircraft values
-> (123,'Airbus',1000),
-> (302,'Boeing',5000),
-> (306,'Jet01',5000),
-> (378,'Airbus380',8000),
-> (456,'Aircraft',500),
-> (789,'Aircraft02',800),
-> (951,'Aircraft03',1000);
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql> select * from aircraft;


+-----+------------+---------------+
| aid | aname | cruisingrange |
+-----+------------+---------------+
| 123 | Airbus | 1000 |
| 302 | Boeing | 5000 |
| 306 | Jet01 | 5000 | | 378 | Airbus380 | 8000 |
| 456 | Aircraft | 500 |
| 789 | Aircraft02 | 800 |
| 951 | Aircraft03 | 1000 |
+-----+------------+---------------+
7 rows in set (0.00 sec)

INSERT INTO employees VALUES


-> (1,'Suman',30000),
-> (2,'Harish',85000),
-> (3,'Sanket',56000),
-> (4,'Manish',45000),
-> (5,'Deepak',90000),
-> (6,'Rekshak',75000),
-> (7,'Ram',100000);
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> select * from employees;
+-----+--------+--------+
| eid | ename | salary |
+-----+--------+--------+
| 1 | Suman| 30000 |
| 2 | Harish | 85000 |
| 3 | Sanket | 56000 |
| 4 | Manish | 45000 |
| 5 | Deepak | 90000 |
| 6 | Rekshak | 75000 |
| 7 | Ram | 100000 |
+-----+--------+--------+
rows in set (0.00 sec)

Department of Computer Applications, BMSCE Page 12


Database Management Systems 22MCA1PCDB
INSERT INTO certified VALUES
-> (1,123),
-> (2,123),
-> (1,302),
-> (5,302),
-> (7,302),
-> (1,306),
-> (2,306),
-> (1,378),
-> (2,378),
-> (4,378),
-> (6,456),
-> (3,456),
-> (5,789),
-> (6,789),
-> (3,951),
-> (1,951),
-> (1,789);
Query OK, 17 rows affected (0.00 sec)
Records: 17 Duplicates: 0 Warnings: 0

mysql> select * from certified;


+-----+-----+
| eid | aid |
+-----+-----+
| 1 | 123 |
| 1 | 302 |
| 1 | 306 |
| 1 | 378 |
| 1 | 789 |
| 1 | 951 |
| 2 | 123 |
| 2 | 306 |
| 2 | 378 |
| 3 | 456 |
| 3 | 951 |
| 4 | 378 |
| 5 | 302 |
| 5 | 789 |
| 6 | 456 |
| 6 | 789 |
| 7 | 302 |
+-----+-----+
17 rows in set (0.00 sec)

Department of Computer Applications, BMSCE Page 13


Database Management Systems 22MCA1PCDB
i. Find the names of aircraft such that all pilots certified to operate them have salaries more than
Rs.80, 000.

SELECT c.eid,MAX(cruisingrange)
-> FROM certified c,aircraft a
-> WHERE c.aid=a.aid
-> GROUP BY c.eid
-> HAVING COUNT(*)>3;
+-----+--------------------+
| eid | MAX(cruisingrange) |
+-----+--------------------+
| 1 | 8000 |
+-----+--------------------+
1 row in set (0.00 sec)

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.

SELECT DISTINCT a.aname


-> FROM aircraft a,certified c,employees e
-> WHERE a.aid=c.aid
-> AND c.eid=e.eid
-> AND NOT EXISTS
-> (SELECT *
-> FROM employees e1
-> WHERE e1.eid=e.eid
-> AND e1.salary<80000);
+------------+
| aname |
+------------+
| Airbus |
| Jet01 |
| Airbus380 |
| Boeing |
| Aircraft02 |
+------------+

iii. Find the names of pilots whose salary is less than the price of the cheapest route from Bengaluru
to Frankfurt.

SELECT DISTINCT e.ename


-> FROM employees e
-> WHERE e.salary<
-> (SELECT MIN(f.price)
-> FROM flight f
-> WHERE f.frm='Bangalore'
-> AND f.too='Frankfurt');

Department of Computer Applications, BMSCE Page 14


Database Management Systems 22MCA1PCDB
+--------+
| ename |
+--------+
| Suman|
| Harish |
| Sanket |
| Manish |
| Deepak |
| Rekshak |
+--------+
6 rows in set (0.00 sec)
7

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.

SELECT a.aid,a.aname,AVG(e.salary)
-> FROM aircraft a,certified c,employees e
-> WHERE a.aid=c.aid
-> AND c.eid=e.eid
-> AND a.cruisingrange>1000
-> GROUP BY a.aid,a.aname;
+-----+-----------+---------------+
| aid | aname | AVG(e.salary) |
+-----+-----------+---------------+
| 302 | Boeing | 73333.3333 |
| 306 | Jet01 | 57500.0000 |
| 378 | Airbus380 | 53333.3333 |
+-----+-----------+---------------+
2 rows in set (0.00 sec)
v. Find the names of pilots certified for some Boeing aircraft.

SELECT distinct e.ename


-> FROM employees e,aircraft a,certified c
-> WHERE e.eid=c.eid -> AND c.aid=a.aid
-> AND a.aname='Boeing';
+--------+
| ename |
+--------+
| Suman |
| Deepak |
| Ram |
+--------+

Department of Computer Applications, BMSCE Page 15


Database Management Systems 22MCA1PCDB
vi. 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(f.distance)
FROM flight f
WHERE f.frm='Bangalore' AND f.too='Delhi');
+-----+
| aid |
+-----+
| 378 |
+-----+
1 row in set (0.00 sec)

Department of Computer Applications, BMSCE Page 16


Database Management Systems 22MCA1PCDB

3. 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.
create table author(
author_id int primary key,
author_name varchar(20),
author_city varchar(20),
author_country varchar(20));

create table publisher(


publisher_id int primary key,
p_name varchar(20),
p_city varchar(20),
p_country varchar(20));

create table category(


category_id int primary key,
description varchar(30));

create table catalog(


book_id int primary key,
title varchar(30),
author_id int,
publisher_id int,
category_id int,
year int,
price int,
FOREIGN KEY(author_id) REFERENCES author(author_id),
FOREIGN KEY(publisher_id) REFERENCES publisher(publisher_id),
FOREIGN KEY(category_id) REFERENCES category(category_id));

Department of Computer Applications, BMSCE Page 17


Database Management Systems 22MCA1PCDB

create table order_details(


order_id int primary key,
book_id int,
quantity int,
foreign key(book_id)references catalog(book_id));

ii. Enter at least five tuples for each relation

INSERT INTO author VALUES


(1111,'JK Rowling','London','England');
INSERT INTO author VALUES
(1112,'Chetan Bhagat','Mumbai','India');
INSERT INTO author VALUES
(1113,'John McCarthy','Chicago','USA');
INSERT INTO author VALUES
(1114,'Dan Brown','California','USA') ;

select * from author;

INSERT INTO publisher VALUES


(2001,'Bloomsbury','London','England');
INSERT INTO publisher VALUES
(2002,'Scholastic','Washington','USA');
INSERT INTO publisher VALUES
(2003,'Pearson','London','England');
INSERT INTO publisher VALUES
(2004,'Rupa','Delhi','India');

Department of Computer Applications, BMSCE Page 18


Database Management Systems 22MCA1PCDB
select * from publisher;

INSERT INTO category VALUES


(3001,'Fiction');
INSERT INTO category VALUES
(3002,'Non-Fiction');
INSERT INTO category VALUES
(3003,'thriller');
INSERT INTO category VALUES
(3004,'action');
INSERT INTO category VALUES

select * from category;

Department of Computer Applications, BMSCE Page 19


Database Management Systems 22MCA1PCDB

INSERT INTO catalog VALUES


(4001,'HP and Goblet Of Fire',1111,2001,3001,2002,600); (3005,'fiction');
INSERT INTO catalog VALUES
(4002,'HP and Order Of Phoenix',1111,2002,3001,2005,650);
INSERT INTO catalog VALUES
(4003,'Two States',1112,2004,3001,2009,540);
INSERT INTO catalog VALUES
(4004,'3 Mistakes of my life',1112,2004,3001,2007,355);
INSERT INTO catalog VALUES
(4005,'Da Vinci Code',1114,2003,3001,2004,450);
INSERT INTO catalog VALUES
(4006,'Angels and Demons',1114,2003,3001,2003,350);
INSERT INTO catalog VALUES
(4007,'Artificial Intelligence',1113,2002,3002,1970,500);

select * from catalog;

Department of Computer Applications, BMSCE Page 20


Database Management Systems 22MCA1PCDB
INSERT INTO order_details VALUES
(5001,4001,5);
INSERT INTO order_details VALUES
(5002,4002,7);
INSERT INTO order_details VALUES
(5003,4003,15);
INSERT INTO order_details VALUES
(5004,4004,11);
INSERT INTO order_details VALUES
(5005,4005,9);
INSERT INTO order_details VALUES
(5006,4006,8); INSERT INTO order_details VALUES
(5007,4007,2);

select * from order_details;

➢ Queries
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.
SELECT * FROM author
WHERE author_id IN
(SELECT author_id FROM catalog WHERE
year>2000 AND price>
(SELECT AVG(price) FROM catalog)
GROUP BY author_id HAVING COUNT(*)>1);

Department of Computer Applications, BMSCE Page 21


Database Management Systems 22MCA1PCDB

iv.Find the author of the book which has maximum sales.

SELECT author_name FROM author a,catalog c WHERE a.author_id=c.author_id


AND book_id IN (SELECT book_id FROM order_details WHERE quantity= (SELECT MAX(quant FROM
order_details)) quantity) FROM order_details));

v.Demonstrate how you increase the price of books published by a specific publisher by
10%.

UPDATE catalog SET price=1.1*price


WHERE publisher_id IN
(SELECT publisher_id FROM publisher WHERE p_name='Rupa');
3 rows updated.

Department of Computer Applications, BMSCE Page 22


Database Management Systems 22MCA1PCDB

4.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.
create table branch(
branch_name varchar(20) primary key,
branch_city varchar(20),
assets real);

create table account(


accno int primary key,
branch_name varchar(20),
balance real,
FOREIGN KEY(branch_name) REFERENCES branch(branch_name));

create table loan(


loan_number int primary key,
branch_name varchar(20),
amount real,
FOREIGN KEY(branch_name) REFERENCES branch(branch_name));

create table customer(


cust_name varchar(20) primary key,
cust_street varchar(20),
cust_city varchar(20));

create table depositor(


cust_name varchar(20),
accno int,
FOREIGN KEY (cust_name) REFERENCES customer (cust_name),
FOREIGN KEY (accno) REFERENCES account (accno));

create table borrower(


cust_name varchar(20),
loan_number int,
foreign key(cust_name) references customer (cust_name),
foreign key(loan_number) references loan (loan_number));

Department of Computer Applications, BMSCE Page 23


Database Management Systems 22MCA1PCDB
ii. Enter at least five tuples for each relation

insert into branch values


('basavangudi', 'BANGALORE', 50000);
1 row created.
insert into branch values
('RESIDENCY ROAD', 'BANGALORE', 10000);
1 row created.
insert into branch values
('somwarpet', 'kodagu', 100000);
1 row created.
SQL> insert into branch values
('MGROAD', 'kushalnagar', 100000);
1 row created.
SQL> insert into branch values
('shrinagar', 'Delhi', 100000);

insert into account values


(1,'basavangudi',2000)
1 row created.
SQL> insert into account values
(2,'somwarpet',5000);
1 row created.
insert into account values
(3,'MGROAD',6000);
SQL> insert into account values
(4,'RESIDENCY ROAD',9999);
1 row created.
SQL> insert into account values
(5,'shrinagar',999);
1 row created.
SQL> insert into account values
(7,'MGROAD',10000);
1 row created.
SQL> insert into account values
(8,'shrinagar',5000);
Department of Computer Applications, BMSCE Page 24
Database Management Systems 22MCA1PCDB
1 row created.

insert into customer values


('sachin','Bull temple road','banglore');
1 row created.
SQL> insert into customer values
('vilas','Bannergatta road','banglore');
1 row created.
SQL> insert into customer values
('advith','N R colony','banglore');
1 row created.
SQL> insert into customer values
('ashik','cp','Delhi');
1 row created.
SQL> insert into customer values
('yathish','jk','Delhi');
1 row created.

Department of Computer Applications, BMSCE Page 25


Database Management Systems 22MCA1PCDB

SQL> insert into depositor values


('sachin',1);
1 row created.
SQL> insert into depositor values
('vilas',2);
1 row created.
SQL> insert into depositor values
('advith',4);
1 row created.
SQL> insert into depositor values
('ashik',8);
1 row created.
SQL> insert into depositor values
('yathish',5);
1 row created.
SQL> insert into depositor values
('yathish',3);
1 row created.
insert into depositor values
('vilas',2);

Department of Computer Applications, BMSCE Page 26


Database Management Systems 22MCA1PCDB

insert into loan values (1,'basavangudi',1000);


1 row created.
SQL> insert into loan values
(2,'shrinagar',2000);
1 row created.
SQL> insert into loan values
(3,'somwarpet',3000);
1 row created.
SQL> insert into loan values
(4,'MGROAD',4000);
1 row created.
SQL> insert into loan values
(5,'RESIDENCY ROAD',5000);
1 row created.

insert into borrower values


('vilas',1);
1 row created.
SQL> insert into borrower values
('yathish',2);
1 row created.
SQL> insert into borrower values
('ashik',3);
1 row created.
SQL> insert into borrower values
('advith',4);
1 row created.
SQL> insert into borrower values
('sachin',5);
1 row created.

Department of Computer Applications, BMSCE Page 27


Database Management Systems 22MCA1PCDB

iii. Find all the customers who have at least two accounts at the Main branch.
SELECT * FROM Customer C
WHERE EXISTS (
SELECT D.cust_name, COUNT(D.cust_name)
FROM Depositor D, Account A
WHERE
D.accno = A.accno AND C.cust_name = D.cust_name AND
A.branch_name = 'somwarpet'
GROUP BY D.cust_name
HAVING COUNT(D.cust_name) >= 2);

iv. Find all the customers who have an account at all the branches located in a specific
city.

SELECT d.cust_name
FROM account a,branch b,depositor d
WHERE b.branch_name=a.branch_name AND
a.accno=d.accno AND
b.branch_city='kushalnagar'
GROUP BY d.cust_name
HAVING COUNT(distinct b.branch_name)=(
SELECT COUNT(branch_name)
FROM branch
WHERE branch_city='kushalnagar');

Department of Computer Applications, BMSCE Page 28


Database Management Systems 22MCA1PCDB

v. 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 = 'BANGALORE');

Department of Computer Applications, BMSCE Page 29


Database Management Systems 22MCA1PCDB

5. Consider the following database a Company. Write the queries in SQL.


EMPLOYEE(fname, lastname, SSN, Bdate, city, Sex, Salary, Supervisor_SSN, DeptNo)
DEPT(Dname, DeptNo, Mgr_SSN, Mgr_StartDate) DEPT_Locations(DeptNo, DLocation)
PROJECT(Pname, Pnumber, Plocation, DeptNo) WORKS_ON(Essn, Pnumber, Hours)
Dependent(Essn, Dependent_Name, Sex, Bdate, Relationship)

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) Retrieve the names of all employees who work in the department that has the employee
with the highest salary among the employees.

iv) Retrieve the names of all employees whose supervisor’s supervisor has ‘8888’ for SSN.
v) Retrive the names of the employees who make atleast Rs. 5000 more than the employee
who is paid the least in the company.
vi) Create a view that has the dept name, manger name, and manager salary for every
department.

vii)Create a view that has project name, controlling department name, number of
employees, and total hours worked per week on the project for each project.
viii)Create a view that has emp name, supervisor name, and emp salary for each employee
who works in the ‘Research’ Dept

i) Create the above tables by properly specifying the primary keys and the foreign
keys

CREATE TABLE EMPLOYEE1 (


SSN VARCHAR(10) PRIMARY KEY,
fname VARCHAR(20),
lastname VARCHAR(20),
Bdate DATE, city VARCHAR(20),
Sex VARCHAR(1),
Salary NUMBER(10,2),
Supervisor_SSN VARCHAR(10),
DeptNo NUMBER(10),
FOREIGN KEY (Supervisor_SSN) REFERENCES EMPLOYEE1(SSN) );

Department of Computer Applications, BMSCE Page 30


Database Management Systems 22MCA1PCDB

CREATE TABLE DEPT (


DeptNo NUMBER(10) PRIMARY KEY,
Dname VARCHAR(20),
Mgr_SSN VARCHAR(10),
Mgr_StartDate DATE,
FOREIGN KEY (Mgr_SSN) REFERENCES EMPLOYEE1(SSN));

CREATE TABLE DEPT_Locations (


DeptNo NUMBER(10),
DLocation VARCHAR(20),
PRIMARY KEY (DeptNo),
FOREIGN KEY (DeptNo) REFERENCES DEPT(DeptNo));

CREATE TABLE PROJECT (


Pnumber NUMBER PRIMARY KEY,
Pname VARCHAR(50),
Plocation VARCHAR(20),
DeptNo NUMBER(10),
FOREIGN KEY (DeptNo) REFERENCES DEPT(DeptNo));

CREATE TABLE WORKS_ON (


Essn VARCHAR(10),
Pnumber NUMBER,
Hours NUMBER(5,2),
FOREIGN KEY (Essn) REFERENCES EMPLOYEE1(SSN),
FOREIGN KEY (Pnumber) REFERENCES PROJECT(Pnumber));

CREATE TABLE Dependent (


Essn VARCHAR(10),
Dependent_Name VARCHAR(50),
Sex VARCHAR(1),
Bdate DATE,
Relationship VARCHAR(15),
FOREIGN KEY (Essn) REFERENCES EMPLOYEE1(SSN));

Department of Computer Applications, BMSCE Page 31


Database Management Systems 22MCA1PCDB
INSERT INTO EMPLOYEE1 VALUES('123456789', 'Kavan', 'HM', '1990-05-15', 'Benagluru', 'M', 50000.00,
NULL, 1);
INSERT INTO EMPLOYEE1 VALUES('234567890', 'Bharath', 'DS', '1992-08-20', 'Mangaluru', 'F', 60000.00,
'123456789', 1);
INSERT INTO EMPLOYEE1 VALUES('345678901', 'Bhuvan', 'KR', '1988-03-10', 'Mysuru', 'M', 55000.00,
'123456789', 2);
INSERT INTO EMPLOYEE1 VALUES('456789012', 'Aditya', 'HG', '1995-11-25', 'Kodagu', 'F', 48000.00,
'345678901', 2);
INSERT INTO EMPLOYEE1 VALUES('567890123', 'Suman', 'SB', '1993-06-30', 'Hassan', 'M', 52000.00,
'345678901', 3);

SQL> INSERT INTO DEPT1 values (1, 'Sales', '123456789', '2010-01-01');


1 row created.
SQL> INSERT INTO DEPT1 values (2, 'Marketing', '345678901', '2012-05-01');
1 row created.
SQL> INSERT INTO DEPT1 values (3, 'Engineering', '567890123', '2015-09-01');
1 row created.
SQL> INSERT INTO DEPT1 values (4, 'HR', NULL, NULL);
1 row created.
SQL> INSERT INTO DEPT1 values (5, 'Finance', NULL, NULL);
1 row created.

INSERT INTO DEPT_Locations1 VALUES (1, 'Benagluru');


1 row created.
SQL> INSERT INTO DEPT_Locations1 VALUES (2, 'Mangaluru');
1 row created.
SQL> INSERT INTO DEPT_Locations1 VALUES (3, 'Mysuru');
1 row created.
SQL> INSERT INTO DEPT_Locations1 VALUES (4, 'Kodagu');
1 row created.
SQL> INSERT INTO DEPT_Locations1 VALUES (5, 'Hassan');
1 row created.

INSERT INTO PROJECT1 VALUES (1, 'Project A', 'Benagluru', 1);


1 row created.
SQL> INSERT INTO PROJECT1 VALUES (2, 'Project B', 'Mangaluru', 2);
1 row created.
SQL> INSERT INTO PROJECT1 VALUES (3, 'Project C', 'Mysuru', 3);
1 row created.
SQL> INSERT INTO PROJECT1 VALUES (4, 'Project D', 'Kodagu', 3);
1 row created.
SQL> INSERT INTO PROJECT1 VALUES (5, 'Project E', 'Hassan', 3);
1 row created.

Department of Computer Applications, BMSCE Page 32


Database Management Systems 22MCA1PCDB
INSERT INTO WORKS_ON1 VALUES ('123456789', 1, 40.0);
1 row created.
SQL> INSERT INTO WORKS_ON1 VALUES ('234567890', 2, 35.5);
1 row created.
SQL> INSERT INTO WORKS_ON1 VALUES ('345678901', 3, 42.0);
1 row created.
SQL> INSERT INTO WORKS_ON1 VALUES ('456789012', 4, 38.5);
1 row created.
SQL> INSERT INTO WORKS_ON1 VALUES ('567890123', 5, 41.0);
1 row created.

Department of Computer Applications, BMSCE Page 33


Database Management Systems 22MCA1PCDB
SQL> INSERT INTO Dependent1 VALUES ('123456789', 'Sarah M', 'F', '2015-03-01', 'Daughter');
1 row created.
SQL> INSERT INTO Dependent1 VALUES ('345678901', 'Michael TS', 'M', '2018-07-10', 'Son');
1 row created.
SQL> INSERT INTO Dependent1 VALUES ('567890123', 'Emma B', 'F', '2016-11-20', 'Daughter');
1 row created.
SQL> INSERT INTO Dependent1 VALUES ('234567890', 'James NM', 'M', '2019-02-15', 'Son');
1 row created.
SQL> INSERT INTO Dependent1 VALUES ('456789012', 'Olivia KV', 'F', '2020-01-05', 'Daughter');
1 row created.

Department of Computer Applications, BMSCE Page 34


Database Management Systems 22MCA1PCDB
iii) Retrieve the names of all employees who work in the department that has the employee
with the highest salary among the employees
SELECT fname,lastname
from employee1
where salary in (select max(Salary)
from employee1);
iv) Retrieve the names of all employees whose supervisor’s supervisor has ‘8888’ for SSN
select fname,lastname
from employee1
where Supervisor_SSN like '8888'
;
v) Retrive the names of the employees who make atleast Rs. 5000 more than the employee
who is paid the least in the company.

Department of Computer Applications, BMSCE Page 35


Database Management Systems 22MCA1PCDB
select fname,lastname
from employee1
where salary >(select min(Salary)+5000
from employee1);
vi) Create a view that has the dept name, manger name, and manager salary for every
department
CREATE VIEW DeptManagerInfo1
AS
SELECT D.Dname, E.fname AS Manager_Fname,
E.lastname AS Manager_Lname,
E.Salary AS Manager_Salary
FROM DEPT1 D
JOIN EMPLOYEE1 E
ON
D.Mgr_SSN like E.SSN;
SELECT * FROM DeptManagerInfo1;

Department of Computer Applications, BMSCE Page 36


Database Management Systems 22MCA1PCDB
vii)Create a view that has project name, controlling department name, number of
employees, and total hours worked per week on the project for each project.
CREATE VIEW ProjectSummary AS
SELECT P.Pname, D.Dname AS Controlling_Dept, COUNT(W.Essn) AS Num_Employees,
SUM(W.Hours)AS Total_Hours_Per_Week
FROM PROJECT1 P
JOIN DEPT1 D ON P.DeptNo = D.DeptNo
JOIN WORKS_ON1 W ON P.Pnumber = W.Pnumber
GROUP BY P.Pname, D.Dname;

Department of Computer Applications, BMSCE Page 37


Database Management Systems 22MCA1PCDB
viii)Create a view that has emp name, supervisor name, and emp salary for each employee
who works in the ‘Research’ Dept.
CREATE VIEW ResearchEmployeeI AS
SELECT E.fname AS Emp_Fname, E.lastname AS Emp_Lname, S.fname AS
Supervisor_Fname, S.lastname AS Supervisor_Lname, E.Salary AS Emp_Salary
FROM EMPLOYEE1 E
JOIN DEPT1 D ON E.DeptNo = D.DeptNo
JOIN EMPLOYEE1 S ON E.Supervisor_SSN = S.SSN
WHERE D.Dname = 'Marketing';

Department of Computer Applications, BMSCE Page 38

You might also like