Bangalore Institute of Technology Department of Information Science and Engineering
Bangalore Institute of Technology Department of Information Science and Engineering
DEPARTMENT OF
INFORMATION SCIENCE AND ENGINEERING
(AFFILIATED TO VTU, BELAGAVI)
CODE: 15CSL58
Prepared By:
Prof. CHETHANA M
Dept of ISE, BIT
DBMS Laboratory with Mini Project
BOOK_ID AUTHOR_NAME
-------- --------------------
1 NAVATHE
2 NAVATHE
3 TANENBAUM
4 EDWARD ANGEL
5 GALVIN
Queries:
1. Retrieve details of all books in the library – id, title, name of publisher,authors,
number of copies in each branch, etc.
OUTPUT:
2. Get the particulars of borrowers who have borrowed more than 3 books, but from
Jan 2017 to Jun 2017.
SELECT CARD_NO
FROM BOOK_LENDING
WHERE DATE_OUT BETWEEN '01-JAN-2017' AND '01-JUL-2017'
GROUP BY CARD_NO
HAVING COUNT (*)>3;
OUTPUT:
CARD_NO
----------
101
3. Delete a book in BOOK table. Update the contents of other tables to reflect this data
manipulation operation.
Before Deleting:
4. Partition the BOOK table based on year of publication. Demonstrate its working
with a simple query.
OUTPUT:
TABLE_NAME PARTITION_NAME
------------------------------ ------------------------------
BOOKPART P2
BOOKPART P1
5. Create a view of all books and its number of copies that are currently available in
the Library.
OUTPUT:
SQL> SELECT * FROM BC;
Queries:
OUTPUT:
GRADE NO_OF_CUSTOMER
---------- --------------
400 2
300 1
2.Find the name and numbers of all salesmen who had more than one customer.
OUTPUT:
SALESMAN NAME
-------- -----
1000 JOHN
2000 RAVI
3.List all salesmen and indicate those who have and don’t have customers in their cities
(Use UNION operation.)
OUTPUT:
SALESMAN CITY
-------- --------------------
1000 BANGALORE
2000 BANGALORE
3000 NO MATCH OF CITIES
4000 NO MATCH OF CITIES
5000 NO MATCH OF CITIES
4.Create a view that finds the salesman who has the customer with the highest order of
a day.
OUTPUT:
SALESMAN ORD_DATE
-------- ---------
1000 04-MAY-17
3000 13-APR-17
2000 20-JAN-17
2000 24-FEB-17
2000 09-MAR-17
5.Demonstrate the DELETE operation by removing salesman with id 1000. All his
orders must also be deleted.
Use ON DELETE CASCADE at the end of foreign key definitions while creating child
table orders and then execute the following:
Use ON DELETE SET NULL at the end of foreign key definitions while creating child
table customers and then executes the following:
MOV_ID REV_STARS
---------- ----------
1001 4
1001 2
1002 2
1002 5
1003 5
1004 4
1005 3
1006 8
1007 0
Queries:
OUTPUT:
MOV_TITLE
-------------------------
PSYCHO
THE BIRDS
2. Find the movie names where one or more actors acted in two or more movies.
SELECT MOV_TITLE
FROM MOVIES M, MOVIE_CAST MV
WHERE M.MOV_ID=MV.MOV_ID AND ACT_ID IN (SELECT ACT_ID
FROM MOVIE_CAST GROUP BY ACT_ID
HAVING COUNT (ACT_ID)>=1)
GROUP BY MOV_TITLE HAVING COUNT (*)>1;
OUTPUT:
MOV_TITLE
-------------------------
BAHUBALI-1
BAHUBALI-2
LAST BUS
3. .List all actors who acted in a movie before 2000 and also in a movie after 2015
(use JOIN operation).
SELECT A.ACT_NAME
FROM ACTOR A
JOIN MOVIE_CAST C
ON A.ACT_ID=C.ACT_ID
JOIN MOVIES M
ON C.MOV_ID=M.MOV_ID
WHERE M.MOV_YEAR NOT BETWEEN 2000 AND 2015;
OUTPUT:
ACT_NAME
--------------------
ANUSHKA
PRABHAS
ARAVIND
KIM NEWMEN
4. Find the title of movies and number of stars for each movie that has at least one
rating and find the highest number of stars that movie received. Sort the result
by movie title.
OUTPUT:
MOV_TITLE MAX(REV_STARS)
------------------------- --------------
BAHUBALI-1 5
BAHUBALI-2 4
LAST BUS 3
PSYCHO 5
THE BIRDS 8
WAR HORSE 4
UPDATE RATING
SET REV_STARS=5
WHERE MOV_ID IN (SELECT M.MOV_ID FROM MOVIES M,DIRECTOR D
WHERE M.DIR_ID=D.DIR_ID AND
D.DIR_NAME = 'STEVEN SPIELBERG');
BEFORE UPDATING
-------------
SQL> SELECT * FROM RATING;
MOV_ID REV_STARS
---------- ----------
1001 4
1002 2
1003 5
1004 4 <-------
1005 3
1006 8
1007 0 <-------
1001 2
1002 5
AFTER UPDATING
--------------
SQL> SELECT * FROM RATING;
MOV_ID REV_STARS
---------- ----------
1001 4
1002 2
1003 5
1004 5 <-------
1005 3
1006 8
1007 5 <-------
1001 2
1002 5
SSID SEM S
----- ------ -----
CSE4A 4 A
CSE4B 4 B
CSE4C 4 C
CSE6A 6 A
CSE6B 6 B
CSE8A 8 A
CSE8B 8 B
CSE8C 8 C
USN SSID
---------- -----
1BI14CS007 CSE8A
1BI14CS018 CSE8A
1BI14CS020 CSE8A
1BI14CS051 CSE8A
1BI14CS066 CSE8C
1BI14CS078 CSE8B
1BI14CS112 CSE8B
1BI14CS114 CSE8B
1BI14CS132 CSE8C
1BI14CS161 CSE8C
1BI15CS012 CSE6A
1BI15CS015 CSE6A
1BI15CS027 CSE6A
1BI15CS101 CSE6B
1BI15CS191 CSE6B
1BI15CS200 CSE6B
1BI16CS001 CSE4A
1BI16CS009 CSE4C
1BI16CS011 CSE4A
1BI16CS021 CSE4C
1BI16CS049 CSE4B
1BI16CS065 CSE4B
1BI16CS071 CSE4B
1BI16CS091 CSE4B
1BI16CS093 CSE4C
1BI16CS100 CSE4C
1BI16CS113 CSE4A
Queries:
1.List all the student details studying in fourth semester ‘C’ section.
OUTPUT:
2.Compute the total number of male and female students in each semester and in each
section.
OUTPUT:
SEM S G COUNT
---------- - --- -----------
4 A F 1
4 A M 2
4 B F 2
4 B M 2
4 C F 2
4 C M 2
6 A F 2
6 A M 1
6 B F 1
6 B M 2
8 A F 1
8 A M 3
8 B F 2
8 B M 1
8 C F 2
8 C M 1
OUTPUT:
TEST1 SUBCODE
---------- --------
20 10CS61
12 10CS62
19 10CS63
20 10CS64
15 10CS65
4.Calculate the FinalIA (average of best two test marks) and update the
corresponding table for all students.
OUTPUT:
SELECT S.USN,S.SNAME,S.ADDRESS,S.PHONE,S.GENDER,IA.SUBCODE,
(CASE
WHEN IA.FINALIA BETWEEN 17 AND 20 THEN 'OUTSTANDING'
WHEN IA.FINALIA BETWEEN 12 AND 16 THEN 'AVERAGE'
ELSE 'WEAK'
END) AS CAT
FROM STUDENT S, SEMSEC SS, IAMARKS IA, SUBJECT SUB
WHERE S.USN = IA.USN AND
SS.SSID = IA.SSID AND
SUB.SUBCODE = IA.SUBCODE AND
SUB.SEM = 8;
OUTPUT:
Queries:
1.Make a list of all project numbers for projects that involve an employee whose last
name is ‘Scott’, either as a worker or as a manager of the department that controls the
project.
OUTPUT:
PNO
-----
P1
P3
P4
P8
2.Show the resulting salaries if every employee working on the ‘IoT’ project is given a
10 percent raise.
OUTPUT:
NAME INCREASED_SALARY
---------- ----------------
John 726000
Smith 660000
Vani 880000
3.Find the sum of the salaries of all employees of the ‘Accounts’ department,as well as
the maximum salary, the minimum salary, and the average salary in this department.
OUTPUT:
TOTAL_SALARY MAX_SALARY MIN_SALARY AVG_SALARY
------------ ---------- ---------- ----------
4246000 800000 606000 707666.667
4. Retrieve the name of each employee who works on all the projects controlled by
department number 5 (use NOT EXISTS operator).
SELECT E.Name
FROM EMPLOYEE E
WHERE NOT EXISTS((SELECT PNo FROM PROJECT WHERE DNo='D5')
MINUS (SELECT W.PNo FROM WORKS_ON W WHERE E.SSN=W.SSN));
OUTPUT:
NAME
----------
Gopal
5.For each department that has more than five employees, retrieve the department
number and the number of its employees who are making more than Rs. 6,00,000.
SELECT D.DNo,COUNT(*)
FROM EMPLOYEE E, DEPARTMENT D
WHERE E.DNo= D.DNo AND E.Salary>600000
GROUP BY D.DNo
HAVING COUNT(*)>=5;
OUTPUT:
DNO COUNT(*)
------ ----------
D1 6