DBMS LAB MANUAL Modified-1
DBMS LAB MANUAL Modified-1
5 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.
2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10
percent raise.
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
4. Retrieve the name of each employee who works on all the projects controlled by
department number. (use NOT EXISTS operator).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.
Consider the schema of the call detail table to partitioned primary index:
Demonstrate the query against this table be optimized by partitioning its primary index
using partitioning techniques.
1. Consider the following schema for a Library Database:
BOOK (Book_id, Title, Publisher_Name, Pub_Year)
BOOK_AUTHORS (Book_id, Author_Name)
PUBLISHER (Name, Address, Phone)
BOOK_COPIES (Book_id, Branch_id, No-of_Copies)
BOOK_LENDING (Book_id, Branch_id, Card_No, Date_Out, Due_Date)
LIBRARY_BRANCH (Branch_id, Branch_Name, Address)
PUBLISHER
Table created.
BOOK
SQL> CREATE TABLE BOOK (
BOOK_ID INTEGER PRIMARY KEY,
TITLE VARCHAR (20),
PUBLISHER_NAME VARCHAR (20) REFERENCES PUBLISHER(NAME)ON DELETE
CASADE,
PUB_YEAR INT (4));
Table created.
BOOK_AUTHORS
Table created.
BOOK_COPIES
Table created
BOOK_LENDING
Table created
Values for tables:
PUBLISHER
BOOK
BOOK_AUTHORS
LIBRARY_BRANCH
BOOK_LENDING
4 rows selected.
SQL> SELECT * FROM BOOK_AUTHORS;
BOOK_ID AUTHOR_NAME
1111 SOMMERVILLE
2222 NAVATHE
3333 HENRY GRAY
4444 THOMAS
4 rows selected.
SQL>SELE * FROM
CT PUBLISHER;
PHONE
NAME ADDRESS
PEARSON BANGALORE 987546253
0
MCGRAW NEWDELHI 784569123
4
SAPNA BANGALORE 784596321
0
3 rows selected.
1111 11 5
3333 22 6
4444 33 10
2222 11 12
4444 55 3
BOOK_ID BRANCH_ID NO_OF_COPIES
5 rows selected.
SQL> SELECT * FROM BOOK_LENDING;
7 rows selected.
SQL> SELECT * FROM LIBRARY_BRANCH;
5 rows selected.
Queries:
1) Retrieve details of all books in the library – id, title, name of publisher, authors, number of copies in each
branch, etc.
2) Get the particulars of borrowers who have borrowed more than 3 books, but from Jan 2017 to Jun 2017.
CARD_NO
1
3) Delete a book in BOOK table. Update the contents of other tables to reflect this data manipulation
operation.
11
4) Create the view for BOOK table based on year of publication and demonstrate its working
with a simple query.
View created.
View created.
1111 SE
5
3333 ANOTOMY
6
4444 ENCYCLOPEDIA
2) Consider the following schema for Order Database:
SALESMAN (Salesman_id, Name, City, Commission)
CUSTOMER (Customer_id, Cust_Name, City, Grade,Salesman_id)
ORDERS (Ord_No, Purchase_Amt, Ord_Date, Customer_id, Salesman_id)
Table Creation:
SALESMAN
COMMISSION INT(5));
Table created.
CUSTOMER
Table created.
ORDERS
Table created.
Queries:
1. Count the customers with grades above Bangalore’s average.
COUNT(CUSTOMER_ID)
2. Find the name and numbers of all salesmen who had more than one customer.
ASHWIN 2
RAJ 2
3. List all salesmen and indicate those who have and don’t have customers in their cities (Use UNION
operation.)
NAME
ASHWIN
BINDU
LAVANYARA
J
ROHIT
4. Create a view that finds the salesman who has the customer with the highest order of a day.
2000 300000
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders must also be
deleted.
1 row deleted.
SELECT * FROM SALESMAN;
11 INFOSYS BENGALURU 5
22 TCS BENGALURU 4 2000
33 WIPRO MYSORE 7
44 TCS MYSORE 6 2000
55 ORACLE TUMKUR 3 3000
Table Creation:
ACTOR
CREATE TABLE ACTOR (
ACT_ID INT PRIMARY KEY,
ACT_NAME VARCHAR(20) NOT NULL,
ACT_GENDER CHAR(1) NOT NULL);
Table created.
DIRECTOR
Table created.
MOVIES
Table created.
MOVIE_CAST
RATING
Table created.
Description of Schema:
VALUES(111,'DEEPA','F'),
(222,'SUDEEP','M'),
(333,'PUNEETH','M'),
(444,'DHIGANTH','M'),
(555,'RANI','F');
111 DEEPA F
222 SUDEEP M
333 PUNEETH M
444 DHIGANTH M
555 RANI F
MOV_ID REV_STARS
1111 3
2222 4
3333 3
5555 4
4444 5
Queries
1. List the titles of all movies directed by ‘Hitchcock’.
2. Find the movie names where one or more actors acted in two or more movies.
SELECT MOV_TITLE
FROM MOVIES M,MOVIE_CAST MC
WHERE M.MOV_ID=MC.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;
MOV_TITLE
MALE
MANASARE
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 M ON A.ACT_ID=M.ACT_ID
JOIN MOVIES M1 ON M.MOV_ID=M1.MOV_ID
WHERE M1.MOV_YEAR<2000 )
INTERSECT
(SELECT A.ACT_NAME
FROM ACTOR A JOIN MOVIE_CAST M ON A.ACT_ID=M.ACT_ID
JOIN MOVIES M1 ON M.MOV_ID=M1.MOV_ID
WHERE M1.MOV_YEAR>2015 );
ACT_NAME
DHIGANTH
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.
SELECT MOV_TITLE,MAX(REV_STARS)
FROM MOVIES
INNER JOIN RATING USING (MOV_ID)
GROUP BY MOV_TITLE
HAVING MAX(REV_STARS)>0
ORDER BY MOV_TITLE;
MOV_TITLE REV_STARS
EEGA 4
LASTWORLD 3
MALE 3
MANASARE 4
PARAMATHM 5
A
UPDATE RATING
SET REV_STARS=5
WHERE MOV_ID IN (SELECT MOV_ID FROM MOVIES
WHERE DIR_ID IN (SELECT DIR_ID
FROM DIRECTOR
WHERE DIR_NAME='STEVEN SPIELBERG'));
1. List all the student details studying in fifth semester ‘B’ section.
2. Compute the total number of male and female students in each semester and in each section.
3. Create a view of Event 1 marks of student USN ‘01JST IS ’ in all subjects.
4. Calculate the Final IA (average of best two test marks) and update the corresponding table for all students.
5. Categorize students based on the following criterion:
If Final IA = 17 to 20 then CAT =‘Outstanding’
If Final IA = 12 to 16 then CAT = ‘Average’
If Final IA< 12 then CAT = ‘Weak’
Give these details only for 8th semester A, B, and C section students.
Table Creation:
STUDENT
Table created.
SEMSEC
Table created.
CLASS
CREATE TABLE CLASS (
USN VARCHAR(10),
SSID VARCHAR(5), FOREIGN KEY(USN) REFERENCES STUDENT(USN), FOREIGN KEY(SSID)
REFERENCES SEMSEC(SSID));
Table created.
SUBJECT
Table created.
IAMARKS
Table created.
Values for tables:
STUDENT:
INSERTION TO BE DONE
SELECT * FROM STUDENT;
SEMSEC:
INSERTION TO BE DONE
SSID SEM S
-
5A 5
A
3B 3
B
7A 7
A
2C 2 C
4B 4
B
4C 4 C
CLASS:
INSERTION TO BE DONE.
USN SSID
1cg15cs001 5A
1cg15cs002 5A
1cg16me063 3B
1cg14ec055 7A
1cg15ee065 3B
1cg15ee065 4c
1cg15cs002 4c
SUBJECT:
INSERTION TO BE DONE
SELECT * FROM
SUBJECT;
SUBCODE TITLE SEM CREDITS
15cs53 dbms 5 4
15cs33 3 4
ds
15cs34 3 4
co
15csl58 dba 52
10cs71 oomd 7 4
IAMARKS:
INSERTION TO BE DONE
Queries:
1. List all the student details studying in fourth semester ‘C’ section.
SELECT S.*,SS.SEM,SS.SEC
-> FROM STUDENT S,SEMSEC SS,CLASS C WHERE S.USN=C.USN
-> AND SS.SSID=C.SSID
-> AND SS.SEM=4 AND SS.SEC='C';
2. Compute the total number of male and female students in each semester and in each section.
mysql> SELECT SS.SEM,SS.SEC,S.GENDER,COUNT(S.GENDER) AS COUNT FROM
STUDENT S,SEMSEC SS,CLASS C WHERE S.USN=C.USN
-> AND SS.SSID=C.SSID GROUP BY SS.SEM,SS.SEC,S.GENDER ORDER BY SEM;
SEM S G COUNT(*)
15cs33 16
15cs53 19
4. Calculate the FinalIA (average of best two test marks) and update the corresponding table for all
students.
mysql> DELIMITER //
mysql> CREATE PROCEDURE AVG_MARKS()
-> BEGIN
-> DECLARE C_A INTEGER;
-> DECLARE C_B INTEGER;
-> DECLARE C_C INTEGER;
-> DECLARE C_SUM INTEGER;
-> DECLARE C_AVG INTEGER;
-> DECLARE C_USN VARCHAR(10);
-> DECLARE C_SUBCODE VARCHAR(8);
-> DECLARE C_SSID VARCHAR(5);
->
-> DECLARE C_IAMARKS CURSOR FOR
-> SELECT GREATEST(TEST1,TEST2) AS A, GREATEST(TEST1,TEST3) AS B,
GREATEST(TEST3,TEST2) AS C, USN, SUBCODE, SSID
-> FROM IAMARKS
-> WHERE FINALIA IS NULL
-> FOR UPDATE;
->
-> OPEN C_IAMARKS;
-> LOOP
->
-> FETCH C_IAMARKS INTO C_A, C_B, C_C, C_USN, C_SUBCODE, C_SSID;
->
-> IF (C_A != C_B) THEN
-> SET C_SUM=C_A+C_B;
-> ELSE
-> SET C_SUM=C_A+C_C;
-> END IF;
->
-> SET C_AVG=C_SUM/2;
->
-> UPDATE IAMARKS SET FINALIA = C_AVG
-> WHERE USN = C_USN AND SUBCODE = C_SUBCODE AND SSID = C_SSID;
->
-> END LOOP;
-> CLOSE C_IAMARKS;
-> END;
-> //
Query OK, 0 rows affected (0.01 sec)
DELIMITER;
Give these details only for 8th semester A, B, and C section students.
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.
2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10 percent raise.
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
4. Retrieve the name of each employee who works on all the projects controlled by department number(use NOT
EXISTS operator).
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.
Table Creation:
DEPARTMENT
DESC DEPARTMENT;
EMPLOYEE
DESC EMPLOYEE;
ALTER TABLE DEPARTMENT
ADD FOREIGN KEY (MGR_SSN) REFERENCES
EMPLOYEE(SSN);
Table altered.
DLOCATION
DESC DLOCATION;
PROJECT
DESC PROJECT;
WORKS_ON
DESC WORKS_ON;
DEPARTMENT
5 rows selected.
EMPLOYEE
INSERT INTO EMPLOYEE VALUES('SSN','NAME','ADDRESS','SEX',SALARY,
'SUPERSSN' ,‘ ‘ DNO’);
10 rows selected.
DLOCATION
PROJECT
INSERT INTO PROJECT VALUES(PNO,'PNAME','PLOCATION',’DNO’);
SELECT * FROM PROJECT;
111 IOT
GUBBI
222
TEXTSPEECH GUBBI
333 4
IPSECURITY DELHI
444 TRAFICANAL 5
BENGALURU
555 1
CLOUDSEC DELHI
5 rows selected.
WORKS_ON
8 rows selected.
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.
PNO
111
333
444
2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10 percent raise.
10 rows selected.
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.
4
0
0
0
0
440000 200000 320000
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='5' AND PNO NOT
IN (SELECT
PNO FROM WORKS_ON
WHERE E.SSN=SSN));
NAME
SCOTT
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.
DNO COUNT(SSN)
3 4
6) Consider the schema of the call detail table to partitioned primary index:
CREATE TABLE calldetail (phone_number DECIMAL(10) NOT NULL, call_start TIMESTAMP, call_duration
INTEGER, call_description VARCHAR(30)) PRIMARY INDEX (phone_number, call_start);
Demonstrate the query against this table be optimized by partitioning its primary index using partitioning
techniques.