DBMS - Lab
DBMS - Lab
COLLEGE OF ENGINEERING
(Autonomous College, Affiliated to VTU)
Bull Temple Road, Bangalore-560019
ME
Laboratory Certificate
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.
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.
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
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
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
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
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)
QUERIES
i Find the names of all 1st sem who are enrolled in a class taught by Harshith.
i Find the names of all classes that either meet in room R128 or have five or more Students enrolled.
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 |
+-----------+
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.
vi. Find the aids of all aircraft that can be used on routes from Bengaluru to New Delhi.
TABLE CREATION
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)
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.
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.
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.
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));
➢ 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);
v.Demonstrate how you increase the price of books published by a specific publisher by
10%.
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);
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');
v. Demonstrate how you delete all account tuples at every branch located in a specific
city.
i) Create the above tables by properly specifying the primary keys and the foreign keys
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