You are on page 1of 38

SRINIVAS INSTITUTE OF TECHNOLOGY

(Affiliated to Visvesvaraya Technological University, Belagavi)


(Accredited by NAAC)
Valachil, Mangaluru-574143

Department of Artificial Intelligence and Machine Learning

LAB MANUAL
DBMS LABORATORY WITH MINI PROJECT
18CSL58

5TH SEMESTER B.E.


2022 - 2023

Name:

USN:

Section:

Batch:

Compiled By:Mr Parvathraj K M M, Assistant Professor


Mrs. Sowmya , Assistant Professor
Srinivas Institute of Technology
Vision

To be a premier institute of professional education and research, responsive to


the needs of industry and society.

Mission

To achieve academic excellence through innovative teaching- learning


practice, by providing conductive research environment, industry-institute
interaction and skill development, leading to professionals with ethical values
and social responsibilities.

Department of Artificial Intelligence and Machine Learning

Vision

To be a centre of excellence in Information Science and Engineering with


quality education and research, responsive to the needs of industry and society.

Mission

 To achieve academic excellence through innovative teaching-learning


practice.
 To inculcate the spirit of innovation, creativity and research.
 To enhance employability through skill development and industry-institute
interaction.
 To develop professionals with ethical values and social responsibilities.

Program Educational Objectives (PEOs)


Graduates will be

PEO1: Competent professionals with knowledge of Information Science &


Engineering to pursue variety of careers and higher education.
PEO2: Proficient in designing innovative solutions to real life problems that
are technically sound, economically viable and socially acceptable.
PEO3: Capable of working in teams, adapting to new technologies and
upgrading skills required to serve the society with ethical values.
DBMS LABORATORY WITH MINI PROJECT
[As per Choice Based Credit System (CBCS) scheme]
(Effective from the academic year 2017 -2018)
SEMESTER – V

Subject Code 18CSL58


17CSL58 IA Marks 40
Number of Lecture Hours/Week 01I + 02P Exam Marks 60
Total Number of Lecture Hours 40 Exam Hours 03

CREDITS – 02

Course objectives: This course will enable students to

• Foundation knowledge in database concepts, technology and practice to groom students intowell
informed database application developers.
• Strong practice in SQL programming through a variety of database problems.
• Develop database applications using front-end tools and back-end DBMS.

Description (If any):

PART-A: SQL Programming (Max. Exam Mks. 50)


• Design, develop, and implement the specified queries for the following problems using Oracle,
MySQL, MS SQL Server, or any other DBMS under LINUX/Windows environment.
• Create Schema and insert at least 5 records for each table. Add appropriate
database constraints.

PART-B: Mini Project (Max. Exam Mks. 30)


• Use Java, C#, PHP, Python, or any other similar front-end tool. All applications must bedemonstrated
on desktop/laptop as a stand-alone or web based application (Mobile apps on Android/IOS are not
permitted.)
Lab Experiments:

Part A: SQL Programming

Consider the following schema for a Library Database:


1 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)
Write SQL queries to
1. Retrieve details of all books in the library – id, title, name of publisher, authors, numberof
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.
3. Delete a book in BOOK table. Update the contents of other tables to reflect this data
manipulation operation.
4. Partition the BOOK table based on yearaof publication. Demonstrate its working with
simple query.
5. Create a view of all books and its number of copies that are currently available in the
Library.

Consider the following schema for Order Database:


2 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)
Write SQL queries to
1. Count the customers with grades above Bangalore’s average.
2. Find the name and numbers of all salesman who had more than one customer.
3. List all the salesman and indicate those who have and don’t have customers in theircities
(Use UNION operation.)
4. Create a view that finds the salesman who has the customer with the highest order of aday.
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders s
must also be deleted.
Consider the schema for Movie Database:
3 ACTOR(Act_id, Act_Name, Act_Gender)
DIRECTOR(Dir_id, Dir_Name, Dir_Phone)
MOVIES(Mov_id, Mov_Title, Mov_Year, Mov_Lang, Dir_id)
MOVIE_CAST(Act_id, Mov_id, Role)
RATING(Mov_id, Rev_Stars)
Write SQL queries to
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.
3. List all actors who acted in a movie before 2000 and also in a movie after 2015 (useJOIN
operation).
4. Find the title of movies and number of stars for each movie that has at least one ratingand
find the highest number of stars that movie received. Sort the result by movie title.
5. Update rating of all movies directed by ‘Steven Spielberg’ to 5.

Consider the schema for College Database:


4 STUDENT(USN, SName, Address, Phone, Gender)
SEMSEC(SSID, Sem, Sec)
CLASS(USN, SSID)
SUBJECT(Subcode, Title, Sem, Credits)
IAMARKS(USN, Subcode, SSID, Test1, Test2, Test3, FinalIA)
Write SQL queries to
1. List all the student details studying in fourth semester ‘C’ section.
2. Compute the total number of male and female students in each semester and in eachsection.
3. Create a view of Test1 marks of student USN ‘1BI15CS101’ in all subjects.
4. Calculate the FinalIA (average of best two test marks) and update the correspondingtable
for all students.
5. Categorize students based on the following criterion:
If FinalIA = 17 to 20 then CAT = ‘Outstanding’
If FinalIA = 12 to 16 then CAT = ‘Average’
If FinalIA< 12 then CAT = ‘Weak’
Give these details only for 8thsemester A, B, and C section students.
Consider the schema for Company Database:
5 EMPLOYEE(SSN, Name, Address, Sex, Salary, SuperSSN, DNo)
DEPARTMENT(DNo, DName, MgrSSN, MgrStartDate)
DLOCATION(DNo,DLoc)
PROJECT(PNo, PName, PLocation, DNo)
WORKS_ON(SSN, PNo, Hours)
Write SQL queries to
1. Make a list of all project numbers for projects that involve an employee whose last nameis
‘Scott’, either as a worker or as a manager of the department that controls theproject.
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 asthe
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 bydepartment
number 5 (use NOT EXISTS operator).
5. For each department that has more than five employees, retrieve the department numberand
the number of its employees who are making more than Rs. 6,00,000.

Part B: Mini project

• For any problem selected, write theER Diagram, apply ER-mapping rules, normalize the relations, and
follow the application development process.
• Make sure that the application should have five or more tables, at least one trigger and one stored
procedure, using suitable frontend tool.
• Indicative areas include; health care, education, industry, transport, supply chain, etc.
Course outcomes: The students should be able to:

 Create, Update and query on the database.


 Demonstrate the working of different concepts of DBMS
 Implement, analyze and evaluate the project developed for an application.
Conduction of Practical Examination:

1. All laboratory experiments from part A are to be included for practical examination.
2. Mini project has to be evaluated for 40 Marks.
3. Report should be prepared in a standard format prescribed for project work.
4. Students are allowed to pick one experiment from the lot.
5. Strictly follow the instructions as printed on the cover page of answer script.
6. Marks distribution:
a) Part A: Procedure + Conduction + Viva:09 + 42 + 9 =60 Marks
b) Part B: Demonstration + Report + Viva voce = 20 + 14 + 06 = 40 Marks
7. Change of experiment is allowed only once and marks allotted to the procedure part to be made zero.
List of Programs

Sl No Program Description Page


Number
01 LIBRARY DATABASE 01

02 ORDER DATABASE 08

03 MOVIE DATABASE 13

04 COLLEGE DATABASE 18

05 COMPANY DATABASE 25
DBMS Laboratory 18CSL58

Query1 :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)

CREATE TABLE PUBLISHER


( Pub_name VARCHAR(20) PRIMARY KEY ,
Address VARCHAR(20),
Phone INTEGER(12)
);
INSERT INTO PUBLISHER VALUES('Sidney' , 'Mangalore,Australia', '987654302');

CREATE TABLE BOOK


( Book_id INT PRIMARY KEY NOT NULL,
Title VARCHAR(20),
Publisher_name VARCHAR(20),
Pub_year INT,
CONSTRAINT book_publishername_fk FOREIGN KEY (Publisher_name)
REFERENCES PUBLISHER(Pub_name)
);
INSERT INTO BOOK VALUES ('801' , 'Database Management System','Willey', '2000');

Department of AIML,SIT, Page 1


MMMangaluru
DBMS Laboratory 18CSL58

CREATE TABLE BOOK_AUTHORS


( Book_id INT(10),
Author_name VARCHAR(20),
CONSTRAINT bookauthor_pk PRIMARY KEY(Book_id,Author_name),
CONSTRAINT bookauthor_bookid_fk FOREIGN KEY(Book_id) REFERENCES
BOOK(Book_id) ON DELETE CASCADE
);

INSERT INTO BOOK_AUTHORS VALUES ('801' , 'Krishnan');

CREATE TABLE LIBRARY_BRANCH


( Branch_id INT PRIMARY KEY,
Branch_name VARCHAR(20) NOT NULL,
Address VARCHAR(20)
);
INSERT INTO LIBRARY_BRANCH VALUES ('503','Gandhi Library','Lalbhagh,Bangalore');

Department of AIML,SIT, Page 2


MMMangaluru
DBMS Laboratory 18CSL58

CREATE TABLE BOOK_COPIES


( Book_id INT,
Branch_id INT,
No_of_copies INT,
PRIMARY KEY (Book_id,Branch_id),
CONSTRAINT bookcopies_bookid_fk FOREIGN KEY(Book_id) REFERENCES
BOOK(Book_id) ON DELETE CASCADE,
CONSTRAINT bookcopies_branchid_fk FOREIGN KEY(Branch_id) REFERENCES
LIBRARY_BRANCH(Branch_id) ON DELETE CASCADE
);
INSERT INTO BOOK_COPIES VALUES ('801' , '501', '79');

CREATE TABLE BOOK_LENDING


( Book_id INT,
Branch_id INT,
Card_no INT,
Date_out DATE,
Due_date DATE,
CONSTRAINT booklending_pk PRIMARY KEY(Book_id,Branch_id,Card_no),
CONSTRAINT bookbranchid_booklending_fk FOREIGN KEY(Book_id,Branch_id)
REFERENCES BOOK_COPIES(Book_id,Branch_id) ON DELETE CASCADE
);

INSERT INTO BOOK_LENDING VALUES ('801' , '501', '301','2017-01-01','2017-07-23');

Department of AIML,SIT, Page 3


MMMangaluru
DBMS Laboratory 18CSL58

Write SQL queries to


1. Retrieve details of all books in the library – id, title, name of publisher, authors,
number of copies in each branch, etc.
SELECT B.Book_id, B.title, B.Publisher_name, A.Author_name, L.Branch_name,
C.No_of_copies
FROM BOOK B, BOOK_AUTHORS A, BOOK_COPIES C, LIBRARY_BRANCH L
WHERE B.Book_id=A.Book_id
AND A.Book_id=C.Book_id
AND C.Branch_id=L.Branch_id;

Output:

Department of AIML,SIT, Page 4


MMMangaluru
DBMS Laboratory 18CSL58

2. Get the particulars of borrowers who have borrowed more than 3


books, but from Jan 2017 to Jun 2017
SELECT Card_no AS CARD_NUM
FROM BOOK_LENDING
WHERE Date_out BETWEEN '2017-01-01' AND '2017-06-30'
GROUP BY Card_no
HAVING COUNT(Book_id)>3;
Output:

3. Delete a book in BOOK table. Update the contents of other tables to


reflect this data manipulation operation.
DELETE
FROM BOOK
WHERE Book_id='805';
Output:

Department of AIML,SIT, Page 5


MMMangaluru
DBMS Laboratory 18CSL58

4. Partition the BOOK table based on year of publication.


Demonstrate its working with a simple query.
CREATE TABLE BOOKP
( Book_id INT NOT NULL,
Title VARCHAR(20),
Publisher_name VARCHAR(20),
Pub_year INT
)
PARTITION BY RANGE (Pub_year)
( PARTITION q0 VALUES LESS THAN (2002),
PARTITION q1 VALUES LESS THAN (2010),
PARTITION q2 VALUES LESS THAN (2015)
);
INSERT INTO BOOKP VALUES ('801' , 'Database Management System','Willey', '2000');

Department of AIML,SIT, Page 6


MMMangaluru
DBMS Laboratory 18CSL58

5. Create a view of all books and its number of copies that are
currently available in the Library.

CREATE VIEW BOOK_COPIES_VIEW AS SELECT B.Title,C.Branch_id,C.No_of_copies


FROM BOOK B,BOOK_COPIES C
WHERE B.Book_id=C.Book_id;

Department of AIML,SIT, Page 7


MMMangaluru
DBMS Laboratory 18CSL58

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)

CREATE TABLE SALESMAN


( Salesman_id INTEGER PRIMARY KEY,
Name CHAR(20) NOT NULL,
City CHAR(10),
Commission DECIMAL(10,2)
);

INSERT INTO SALESMAN VALUES (5001,'Adithi Sharma','Bangalore',0.15);

CREATE TABLE CUSTOMER


( Customer_id INTEGER PRIMARY KEY,
Cust_name CHAR(20) NOT NULL,
City CHAR(10),
Grade INTEGER,
Salesman_id INTEGER,
CONSTRAINT salesman_id_customer_fk FOREIGN KEY (Salesman_id) REFERENCES
SALESMAN(Salesman_id) ON DELETE CASCADE
);

INSERT INTO CUSTOMER VALUES (3002,'Nikhil Rao','Bangalore', 100,5001);

Department of AIML,SIT, Page 8


MMMangaluru
DBMS Laboratory 18CSL58

CREATE TABLE ORDERS


( Ord_no INTEGER PRIMARY KEY,
Purchase_amt DECIMAL(10,4),
Ord_date DATE NOT NULL,
Salesman_id INTEGER NOT NULL,
Customer_id INTEGER NOT NULL,
CONSTRAINT salesman_id_orders_fk FOREIGN KEY (Salesman_id) REFERENCES
SALESMAN(Salesman_id) ON DELETE CASCADE,
CONSTRAINT customer_id_orders_fk FOREIGN KEY (Customer_id) REFERENCES
CUSTOMER(Customer_id) ON DELETE CASCADE
);
INSERT INTO ORDERS VALUES (70006,2400.16,'2012-07-20',1000,3006);

Department of AIML,SIT, Page 9


MMMangaluru
DBMS Laboratory 18CSL58

Write SQL queries to


1. Count the customers with grades above Bangalore’s average.
SELECT Grade, COUNT(DISTINCT Customer_id)
FROM CUSTOMER
GROUP BY Grade
HAVING Grade > ( SELECT AVG(Grade)
FROM CUSTOMER
WHERE City='Bangalore');

Output:

2. Find the name and numbers of all salesman who had more than one customer.
SELECT S.Salesman_id, S.Name
FROM SALESMAN S
WHERE 1 < ( SELECT COUNT(*)
FROM CUSTOMER
WHERE Salesman_id=S.Salesman_id);

Output:

Department of AIML,SIT, Page 10


MMMangaluru
DBMS Laboratory 18CSL58

3. List all the salesman and indicate those who have and don’t have customers in their
cities (Use UNION operation.)
SELECT SALESMAN.Salesman_id, Name, Cust_name, Commission
FROM SALESMAN, CUSTOMER
WHERE SALESMAN.City = CUSTOMER.City
UNION SELECT Salesman_id, Name, 'NO MATCH', Commission
FROM SALESMAN
WHERE NOT City = ANY ( SELECT City
FROM CUSTOMER)
ORDER BY 2 DESC;
Output:

4. Create a view that finds the salesman who has the customer with the highest order of a
day.
CREATE VIEW Highest_Order_Salesman AS
SELECT O.Ord_date, S.Salesman_id, S.Name
FROM SALESMAN S, ORDERS O
WHERE S.Salesman_id = O.Salesman_id
AND O.Purchase_amt = ( SELECT MAX(C.Purchase_amt)
FROM ORDERS C
WHERE C.Ord_date = O.Ord_date);

Department of AIML,SIT, Page 11


MMMangaluru
DBMS Laboratory 18CSL58

Output:

5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders
must also be deleted.
DELETE
FROM SALESMAN
WHERE Salesman_id=1000 ;

Department of AIML,SIT, Page 12


MMMangaluru
DBMS Laboratory 18CSL58

3.Consider the schema for Movie Database:


ACTOR(Act_id, Act_Name, Act_Gender)
DIRECTOR(Dir_id, Dir_Name, Dir_Phone)
MOVIES(Mov_id, Mov_Title, Mov_Year, Mov_Lang, Dir_id)
MOVIE_CAST(Act_id, Mov_id, Role)
RATING(Mov_id, Rev_Stars)

CREATE TABLE ACTOR


( Act_id INT PRIMARY KEY,
Act_name VARCHAR(20) NOT NULL,
Act_gender CHAR
);
INSERT INTO ACTOR VALUES ('101',' James Stewart','M');

CREATE TABLE DIRECTOR


( Dir_id INT PRIMARY KEY,
Dir_name VARCHAR(20) NOT NULL,
Dir_phone INT(12)
);
INSERT INTO DIRECTOR VALUES('201','Alfred Hitchcock','987654001');

Department of AIML,SIT, Page 13


MMMangaluru
DBMS Laboratory 18CSL58

CREATE TABLE MOVIES


( Mov_id INT PRIMARY KEY,
Mov_title VARCHAR(30),
Mov_year INT,
Mov_lang VARCHAR(10),
Dir_id INT,
CONSTRAINT dir_dirid_fk FOREIGN KEY(Dir_id) REFERENCES DIRECTOR(Dir_id)
);
INSERT INTO MOVIES VALUES('901','Vertigo','1958','English','205');

CREATE TABLE MOVIE_CAST


( Act_id INT,
Mov_id INT,
Role VARCHAR(20),
CONSTRAINT mc_actid_movid_pk PRIMARY KEY(Act_id,Mov_id),
CONSTRAINT mc_actid_fk FOREIGN KEY(Act_id) REFERENCES ACTOR(Act_id),
CONSTRAINT mc_movid_fk FOREIGN KEY(Mov_id) REFERENCES MOVIES(Mov_id)
);
INSERT INTO MOVIE_CAST VALUES('101','901','John Scottie Ferguson');

Department of AIML,SIT, Page 14


MMMangaluru
DBMS Laboratory 18CSL58

CREATE TABLE RATING


( Mov_id INT,
Rev_stars INT,
CONSTRAINT r_movid_revstars_pk PRIMARY KEY(Mov_id,Rev_stars),
CONSTRAINT r_movid_fk FOREIGN KEY(Mov_id) REFERENCES MOVIES(Mov_id)
);

INSERT INTO RATING VALUES('901','4');

Write SQL queries

1..List the titles of all movies directed by ‘Hitchcock’.


SELECT Mov_title
FROM MOVIES
WHERE Dir_id IN ( SELECT Dir_id
FROM DIRECTOR
WHERE Dir_name LIKE '%Hitchcock');
Output:

Department of AIML,SIT, Page 15


MMMangaluru
DBMS Laboratory 18CSL58

2. Find the movie names where one or more actors acted in two or more movies.
SELECT DISTINCT Mov_title
FROM MOVIES
WHERE Mov_id IN ( SELECT Mov_id
FROM MOVIE_CAST
WHERE Act_id IN ( SELECT Act_id
FROM MOVIE_CAST
GROUP BY Act_id
HAVING COUNT(Mov_id )>1));
Output:

3. List all actors who acted in a movie before 2000 and also in a movie after 2015 (use JOIN
operation).
SELECT DISTINCT A.Act_name
FROM ACTOR A JOIN MOVIE_CAST C JOIN MOVIES M
ON A.Act_id =C.Act_id AND C.Mov_id= M.Mov_id
WHERE M.Mov_year<2000 OR M.Mov_year>2015;

Output:

Department of AIML,SIT, Page 16


MMMangaluru
DBMS Laboratory 18CSL58

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 MOVIES.Mov_title,RATING.Mov_id,SUM(RATING.Rev_stars)
AS Num_stars,MAX(Rev_stars) AS Max_star
FROM MOVIES RIGHT OUTER JOIN RATING
ON MOVIES.Mov_id=RATING.Mov_id
GROUP BY RATING.Mov_id ;
Output:

5. Update rating of all movies directed by ‘Steven Spielberg’ to 5.


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='Stanley Kubrick'));
Output:

Department of AIML,SIT, Page 17


MMMangaluru
DBMS Laboratory 18CSL58

4. Consider the schema for College Database:


STUDENT(USN, SName, Address, Phone, Gender)
SEMSEC(SSID, Sem, Sec)
CLASS(USN, SSID)
SUBJECT(Subcode, Title, Sem, Credits)
IAMARKS(USN, Subcode, SSID, Test1, Test2, Test3, FinalIA)
CREATE TABLE STUDENT
( Usn VARCHAR(20),
Sname VARCHAR(20) NOT NULL,
Address VARCHAR(20),
Phone INT UNIQUE,
Gender VARCHAR(2),
CONSTRAINT usn_st_pk PRIMARY KEY(Usn)
);

INSERT INTO STUDENT VALUES('1BI17CS001','Aman','Lalbhag,Mangalore','987654001','M');

CREATE TABLE SEMSEC


( Ssid INT,
Sem VARCHAR(5),
Sec VARCHAR(10),
CONSTRAINT semsec_ssid_pk PRIMARY KEY(Ssid)
);

INSERT INTO SEMSEC VALUES('101','2','A');

Department of AIML,SIT, Page 18


MMMangaluru
DBMS Laboratory 18CSL58

CREATE TABLE CLASS


( Usn VARCHAR(20),
Ssid INT,
CONSTRAINT class_usnssid_pk PRIMARY KEY(Usn,Ssid),
CONSTRAINT class_usn_fk FOREIGN KEY(Usn) REFERENCES STUDENT(Usn),
CONSTRAINT class_ssid_fk FOREIGN KEY(Ssid) REFERENCES SEMSEC(Ssid)
);
INSERT INTO CLASS VALUES('1BI16CS001','201');

Department of AIML,SIT, Page 19


MMMangaluru
DBMS Laboratory 18CSL58

CREATE TABLE SUBJECT


( Subcode VARCHAR(20),
Sub_title VARCHAR(30) NOT NULL,
Sem INT,
Credits INT,
CONSTRAINT subject_subcode_pk PRIMARY KEY(Subcode)
);

INSERT INTO SUBJECT VALUES('10CS21','C Programming','2','4');

CREATE TABLE IAMARKS


(Usn VARCHAR(20),
Subcode VARCHAR(20),
Ssid INT,
Test1 INT,
Test2 INT,
Test3 INT,
Finalia FLOAT,
CONSTRAINT ia_usnssidsubcode_pk PRIMARY KEY(Usn,Subcode,Ssid),
CONSTRAINT ssidusn_ia_fk FOREIGN KEY(Usn,Ssid) REFERENCES CLASS(Usn,Ssid),
CONSTRAINT subcode_ia_fk FOREIGN KEY(Subcode) REFERENCES SUBJECT(Subcode)
);

INSERT INTO IAMARKS VALUES('1BI15CS001','10CS61','301','10','15','17',NULL);

Department of AIML,SIT, Page 20


MMMangaluru
DBMS Laboratory 18CSL58

Write SQL queries to


1. List all the student details studying in fourth semester ‘C’ section.
SELECT S.Usn, S.Sname
FROM STUDENT S,SEMSEC M,CLASS C
WHERE M.Sem=4 AND M.Sec='c' AND S.Usn=C.Usn AND C.Ssid=M.Ssid;
Output:

2. Compute the total number of male and female students in each semester and in
each section.
SELECT C.Ssid AS Room_No, M.Sec AS Sec,M.Sem AS SEMESTER,
SUM(CASE WHEN S.Gender = 'M' THEN 1 ELSE 0 END) AS Male,
SUM(CASE WHEN S.Gender = 'F' THEN 1 ELSE 0 END) AS Female
FROM CLASS C ,STUDENT S,SEMSEC M
WHERE S.Usn=C.Usn AND M.Ssid=C.Ssid GROUP BY C.Ssid;

Department of AIML,SIT, Page 21


MMMangaluru
DBMS Laboratory 18CSL58

Output:

3. Create a view of Test1 marks of student USN ‘4SN14CS001’ in all subjects.


CREATE VIEW TEST1_MARK AS
SELECT Test1
FROM IAMARKS
WHERE Usn='4SN14CS001';
Output:

4. Calculate the FinalIA (average of best two test marks) and update the corresponding
table for all students.

UPDATE IAMARKS
SET Finalia = (GREATEST(Test1+Test2,Test2+Test3,Test3+Test1)/2);

Department of AIML,SIT, Page 22


MMMangaluru
DBMS Laboratory 18CSL58

5. Categorize students based on the following criterion:


If FinalIA = 17 to 20 then CAT = ‘Outstanding’
If FinalIA = 12 to 16 then CAT = ‘Average’
If FinalIA< 12 then CAT = ‘Weak’
Give these details only for 8 th semester A, B, and C section students.
SELECT I.Usn, I.Test1,I.Test2,I.Test3,I.Finalia,
(CASE WHEN I.Finalia>=17 AND I.Finalia<=20 THEN 'Outstanding'

WHEN I.Finalia>=12 AND I.Finalia<17 THEN 'Average'

WHEN I.Finalia<12 THEN 'Weak'

ELSE 'INVALID'

END) AS Grade

FROM IAMARKS I,CLASS C,SEMSEC S

WHERE I.Usn=C.Usn AND C.Ssid=S.Ssid AND S.Sem=8 AND S.Sec IN ('A','B','C');

Department of AIML,SIT, Page 23


MMMangaluru
DBMS Laboratory 18CSL58

Output:

Department of AIML,SIT, Page 24


MMMangaluru
DBMS Laboratory 18CSL58

5. Consider the schema for Company Database:


EMPLOYEE(SSN, Name, Address, Sex, Salary, SuperSSN, DNo)
DEPARTMENT(DNo, DName, MgrSSN, MgrStartDate)
DLOCATION(DNo,DLoc)
PROJECT(PNo, PName, PLocation, DNo)
WORKS_ON(SSN, PNo, Hours)

CREATE TABLE DEPARTMENT


( DNo INTEGER,
DName VARCHAR(20),
MgrSSN CHAR(9),
MgrStartDate DATE,
PRIMARY KEY(DNo)
);

INSERT INTO DEPARTMENT VALUES(5,'Accounts','333445555','1988-05-02');

CREATE TABLE EMPLOYEE


( SSN CHAR(9) PRIMARY KEY,
FName VARCHAR(20),
Minit CHAR,
LName VARCHAR(20),
Address VARCHAR(30),
Sex CHAR,
Salary DECIMAL(10,2),
SuperSSN CHAR(9),
DNo INTEGER,

Department of AIML,SIT, Page 25


MMMangaluru
DBMS Laboratory 18CSL58

CONSTRAINT SuperSSN_employee_fk FOREIGN KEY (SuperSSN) REFERENCES


EMPLOYEE(SSN),
CONSTRAINT dno_employee_fk FOREIGN KEY(DNo) REFERENCES
DEPARTMENT(DNo) ON DELETE CASCADE
);
INSERT INTO EMPLOYEE
VALUES('888665555','James','E','Borg','Mangaluru','M',655000,NULL,1);

CREATE TABLE DLOCATION


( DNo INTEGER,
DLoc VARCHAR(20),
PRIMARY KEY(DNo, DLoc),
CONSTRAINT dno_dlocation_fk FOREIGN KEY (DNo) REFERENCES
DEPARTMENT(DNo) ON DELETE CASCADE
);

INSERT INTO DLOCATION VALUES(1,'Houston');

Department of AIML,SIT, Page 26


MMMangaluru
DBMS Laboratory 18CSL58

CREATE TABLE PROJECT


( PNo INTEGER,
PName VARCHAR(20) UNIQUE,
PLocation VARCHAR(20),
DNo INTEGER,
PRIMARY KEY(PNo),
CONSTRAINT dno_project_fk FOREIGN KEY (DNo) REFERENCES
DEPARTMENT(DNo) ON DELETE CASCADE
);

INSERT INTO PROJECT VALUES(1,'ProductX','Bellaire',5);

CREATE TABLE WORKS_ON


( SSN CHAR(9),
PNo INTEGER,
Hours DECIMAL(3,1),
PRIMARY KEY(SSN,PNo),
CONSTRAINT ssn_workson_fk FOREIGN KEY (SSN) REFERENCES EMPLOYEE(SSN)
ON DELETE CASCADE,
CONSTRAINT pno_workson_fk FOREIGN KEY (PNo) REFERENCES PROJECT(PNo)
ON DELETE CASCADE
);

INSERT INTO WORKS_ON VALUES('123456789',1,32.5);

Department of AIML,SIT, Page 27


MMMangaluru
DBMS Laboratory 18CSL58

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 theproject.
( SELECT DISTINCT P.PNo
FROM PROJECT P, DEPARTMENT D, EMPLOYEE E
WHERE E.DNo = D.DNo
AND D.MgrSSN = E.SSN
AND E.LName='Scott')
UNION
( SELECT DISTINCT P.PNo
FROM PROJECT P, WORKS_ON W, EMPLOYEE E
WHERE P.PNo = W.PNo AND E.SSN = W.SSN AND E.LName= 'Scott');

Department of AIML,SIT, Page 28


MMMangaluru
DBMS Laboratory

Output:

2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10
percent raise.

UPDATE EMPLOYEE

SET Salary= Salary * 1.1

WHERE SSN IN ( SELECT SSN


FROM PROJECT P, WORKS_ON W
WHERE P.PNo= W.PNo
AND P.PName=‘IoT’);

Output:

3. Find the sum of the salaries of all employees of the ‘Accounts’ department, as well asthe
maximum salary, the minimum salary, and the average salary in this department

SELECT SUM(E.Salary),MAX(E.Salary),MIN(E.Salary),AVG(E.Salary)
FROM EMPLOYEE E JOIN DEPARTMENT D ON E.DNo=D.DNo
WHERE D.Dname='Accounts';

CSE Department,SIT, Mangaluru Page 29


DBMS Laboratory

Output:

4. Retrieve the name of each employee who works on all the projects controlled by
department number 5 (use NOT EXISTS operator).

SELECT E.FName,E.LName
FROM EMPLOYEE E
WHERE NOT EXISTS ( SELECT P.PNo
FROM PROJECT P
WHERE P.DNo=5
AND P.PNo NOT IN ( SELECT W.PNo
FROM WORKS_ON W
WHERE W.SSN=E.SSN ));
Output:

5. For each department that has more than five employees, retrieve thedepartment
number and the number of its employees who are making more than Rs. 6,00,000.

SELECT E.DNo, COUNT(*)


FROM DEPARTMENT D,EMPLOYEE E
WHERE D.DNo=E.DNo
AND Salary> 600000
AND E.DNo IN ( SELECT DNo
FROM EMPLOYEE
GROUP BY DNo
HAVING COUNT(*)>5)
GROUP BY E.DNo

Output:

CSE Department,SIT, Mangaluru Page 30


Srinivas Institute of Technology
Department of Information Science and Engineering
Program Outcomes ( POs)
1. Engineering Knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering
problems.
2. Problem Analysis: Identify, formulate, review research literature, and analyse complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences and engineering sciences.
3. Design/Development of Solutions: Design solutions for complex engineering
problems and design system components or processes that meet the specified needs with
appropriate consideration for the public health and safety, and the cultural, societal, and
environmental considerations.
4. Conduct Investigations of Complex Problems: Use research-based knowledge
and research methods including design of experiments, analysis and interpretation of data,
and synthesis of the information to provide valid conclusions for complex problems.
5. Modern Tool Usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modelling to complex
engineering activities with an understanding of the limitations.
6. The Engineer and Society: Apply reasoning informed by the contextual knowledge
to assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
7. Environment and Sustainability: Understand the impact of the professional
engineering solutions in societal and environmental contexts, and demonstrate the
knowledge of, and need for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
9. Individual and Team Work: Function effectively as an individual, and as a member
or leader in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and
write effective reports and design documentation, make effective presentations, and give
and receive clear instructions.
11. Project Management and Finance: Demonstrate knowledge and understanding of
the engineering and management principles and apply these to one’s own work, as a
member and leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long Learning: Recognize the need for, and have the preparation and ability to
engage in independent and lifelong learning in the broadest context of technological
change.
Program Specific Outcomes (PSOs)
PSO1:Programming and software development skills: Ability to employ modern computer
languages, computing environments and standard practices for analysing, designing and
developing optimal solutions to deliver quality software products.
PSO2: Domain specific skills: Ability to apply techniques to develop computer based solutions
in various domains like Artificial Intelligence, Machine Learning, Network Engineering, Image
Processing, Web Technologies and Data Sciences.

You might also like