0% found this document useful (0 votes)
126 views

Lab Programs

This document contains the details of the DBMS Laboratory program for semester 5 students at Gopalan College of Engineering and Management. It includes 5 SQL programs to be completed as part of Part A and the guidelines for a mini database project as Part B. The objectives of the course are to provide foundational knowledge of databases, practice SQL programming skills, and develop database applications using front-end and back-end tools. Marks will be awarded for the procedure, execution, and viva of both the SQL programs and mini project.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

Lab Programs

This document contains the details of the DBMS Laboratory program for semester 5 students at Gopalan College of Engineering and Management. It includes 5 SQL programs to be completed as part of Part A and the guidelines for a mini database project as Part B. The objectives of the course are to provide foundational knowledge of databases, practice SQL programming skills, and develop database applications using front-end and back-end tools. Marks will be awarded for the procedure, execution, and viva of both the SQL programs and mini project.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

GOPALAN COLLEGE OF ENGINEERING AND MANAGEMENT

[ISO Certified 9000:2008, Affiliated to VTU, Belgaum, Approved by AICTE, New Delhi]
181/1, Hoodi Village, Sonnenahalli, K.R.Puram, Bangalore – 560 048

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CERTIFICATE

This is to Certify that Mr. / Ms. _______________________________________________

bearing USN _______________________ of the V Semester B.E completed the experiments in

the ____________________Laboratory prescribed by the Visvesvaraya Technological University ,

Belgaum , for the semester of the academic year 2020-21.

Signature of the Guide Signature of the HOD Signature of the Principal


Mrs.Suparna.K Dr. J. Somasekar Dr. N. Sengottaiyan

Name of the Examiners: External Viva Signature with Date

1.

2.
DBMS LABORATORY WITH MINI PROJECT
(Effective from the academic year 2018 -2019)
SEMESTER – V
Course Code 18CSL58 CIE Marks 40
Number of Contact Hours/Week 0:2:2 SEE Marks 60
Total Number of Lab Contact Hours 36 Exam Hours 03
Credits – 2
Course Learning Objectives: This course (18CSL58) will enable students to:
• Foundation knowledge in database concepts, technology and practice to groom students
into well-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.
Descriptions (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
be demonstrated on desktop/laptop as a stand-alone or web based application (Mobile
apps on Android/IOS are not permitted.)
Installation procedure of the required software must be demonstrated, carried out in
groups and documented in the journal.
Programs List:
PART A
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, Programme_id, No-of_Copies)
BOOK_LENDING(Book_id, Programme_id, Card_No, Date_Out, Due_Date)
LIBRARY_PROGRAMME(Programme_id, Programme_Name, Address)
Write SQL queries to
1. Retrieve details of all books in the library – id, title, name of publisher,
authors, number of copies in each Programme, 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 year of publication. Demonstrate its
working with a simple query.
5. Create a view of all books and its number of copies that are currently
available in the Library.
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)
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 their cities (Use UNION operation.)
4. Create a view that finds the salesman who has the customer with the highest
order of a day.
5. Demonstrate the DELETE operation by removing salesman with id 1000. All
his orders must also be deleted.
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)
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 (use JOIN operation).
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.
5. Update rating of all movies directed by ‘Steven Spielberg’ to 5.
4. Consider the schema for College Database:
STUDENT(USN, SName, Address, Phone, Gender)
SEMSEC(SSID, Sem, Sec)
CLASS(USN, SSID)
COURSE(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
each section.
3. Create a view of Test1 marks of student USN ‘1BI15CS101’ in all Courses.
4. Calculate the FinalIA (average of best two test marks) and update the
corresponding table 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 8th semester A, B, and C section students.
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)
Write SQL queries to
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
controlledby department number 5 (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.
PART B: Mini Project
• For any problem selected
• Make sure that the application should have five or more tables
• Indicative areas include; health care
Laboratory Outcomes: The student 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.
Conduct of Practical Examination:
• Experiment distribution o For laboratories having only one part: Students are allowed
to pick one experiment from the lot with equal opportunity.
o For laboratories having PART A and PART B: Students are allowed to pick one
experiment from PART A and one experiment from PART B, with equal
opportunity.
• Change of experiment is allowed only once and marks allotted for procedure to be
made zero of the changed part only. • Marks Distribution (Courseed to change in
accoradance with university regulations)
k) For laboratories having only one part – Procedure + Execution + Viva-Voce:
15+70+15 =
100 Marks
l) For laboratories having PART A and PART B
i. Part A – Procedure + Execution + Viva = 6 + 28 + 6 = 40
Marks ii. Part B – Procedure + Execution + Viva = 9 + 42 + 9 =
60 Marks
DBMS LAB PROGRAMS (15CSL58)

Lab Program 1

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)

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.
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 year of publication. Demonstrate its
working with a simple query.
5. Create a view of all books and its number of copies that are currently
available in the Library.

AIM: Create table, querying the Library database and perform all the operations using sql.

ER DIAGRAM
SCHEMA DIAGRAM

TABLE CREATION:

mysql> CREATE TABLE BOOK(BOOK_ID INT(5),TITLE


VARCHAR(20),PUBLISHER_NAME VARCHAR(20),PUB_YEAR INT(5));
Query OK, 0 rows affected (0.08 sec)

mysql> ALTER TABLE BOOK ADD CONSTRAINT PRIMARY KEY(BOOK_ID,PUB_YEAR);


Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> CREATE TABLE BOOK_AUTHORS(BOOK_ID INT(5),AUTHOR_NAME


VARCHAR(20),FOREIGN KEY(BOOK_ID) REFERENCES BOOK(BOOK_ID) ON DELETE
CASCADE);
Query OK, 0 rows affected (0.06 sec)

mysql> CREATE TABLE PUBLISHER(NAME VARCHAR(20) PRIMARY KEY,ADDRESS


VARCHAR(20),PHONE INT(10));
Query OK, 0 rows affected (0.07 sec)

mysql> CREATE TABLE LIBRARY_BRANCH(BRANCH_ID INT(5) PRIMARY


KEY,BRANCH_NAME VARCHAR(20), ADDRESS VARCHAR(20));
Query OK, 0 rows affected (0.06 sec)

mysql> CREATE TABLE BOOK_COPIES(BOOK_ID INT(5),BRANCH_ID


INT(5),N0_OF_COPIES INT(10),PRIMARY KEY(BOOK_ID,BRANCH_ID),FOREIGN
KEY(BOOK_ID) REFERENCES BOOK(BOOK_ID) ON DELETE CASCADE,FOREIGN
KEY(BRANCH_ID) REFERENCES LIBRARY_BRANCH(BRANCH_ID) ON DELETE
CASCADE);
Query OK, 0 rows affected (0.12 sec)

mysql> CREATE TABLE BOOK_LENDING(BOOK_ID INT(5) ,BRANCH_ID


INT(5),CARD_NO INT(5),DATE_OUT DATE,DUE_DATE DATE,PRIMARY
KEY(CARD_NO,BOOK_ID,BRANCH_ID),FOREIGN KEY(BOOK_ID) REFERENCES
BOOK(BOOK_ID) ON DELETE CASCADE,FOREIGN KEY(BRANCH_ID) REFERENCES
LIBRARY_BRANCH(BRANCH_ID) ON DELETE CASCADE);
Query OK, 0 rows affected (0.11 sec)

mysql> INSERT INTO BOOK VALUES(1,'CG','MCGRAW',2000);


Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO BOOK VALUES(2,'CN','MCGRAW',1998);


Query OK, 1 row affected (0.04 sec)

mysql> INSERT INTO BOOK VALUES(3,'CPP','NAVATHE',2017);


Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO BOOK_AUTHORS VALUES(1,'GALVIN');


Query OK, 1 row affected (0.02 sec)

mysql> INSERT INTO BOOK_AUTHORS VALUES(2,'FOROUZAN');


Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO BOOK_AUTHORS VALUES(3,'SUMITA');


Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO PUBLISHER VALUES('MK','BANGALORE',999876544);


Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO PUBLISHER VALUES('JK','BANGALORE',999876548);


Query OK, 1 row affected (0.02 sec)

mysql> INSERT INTO PUBLISHER VALUES('SR','BANGALORE',966876548);


Query OK, 1 row affected (0.04 sec)

mysql> INSERT INTO LIBRARY_BRANCH VALUES(11,'HOODI','BANGALORE');


Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO LIBRARY_BRANCH VALUES(12,'WHITEFIELD','BANGALORE');


Query OK, 1 row affected (0.04 sec)

mysql> INSERT INTO LIBRARY_BRANCH


VALUES(13,'KORAMANGALA','BANGALORE');
Query OK, 1 row affected (0.04 sec)

mysql> INSERT INTO BOOK_COPIES VALUES(1,11,100);


Query OK, 1 row affected (0.04 sec)

mysql> INSERT INTO BOOK_COPIES VALUES(2,12,200);


Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO BOOK_COPIES VALUES(3,13,300);


Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO BOOK_LENDING VALUES(1,11,101,20001101,20000130);


Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO BOOK_LENDING VALUES(2,12,102,20170122,20170613);


Query OK, 1 row affected (0.04 sec)

mysql> INSERT INTO BOOK_LENDING VALUES(3,13,103,20170622,20170713);


Query OK, 1 row affected (0.03 sec)

QUERY1: Retrieve details of all books in the library – id, title, name of publisher,
authors, number of copies in each branch, etc.

mysql> SELECT
C.BRANCH_ID,B.BOOK_ID,B.TITLE,B.PUBLISHER_NAME,A.AUTHOR_NAME,C.N0_OF_
COPIES FROM BOOK B,BOOK_AUTHORS A,BOOK_COPIES C WHERE
B.BOOK_ID=A.BOOK_ID AND B.BOOK_ID=C.BOOK_ID ORDER BY C.BRANCH_ID;

+-----------+---------+-------+----------------+-------------+--------------+
| BRANCH_ID | BOOK_ID | TITLE | PUBLISHER_NAME | AUTHOR_NAME |
N0_OF_COPIES |
+-----------+---------+-------+----------------+-------------+--------------+
| 11 | 1 | CG | MCGRAW | GALVIN | 100 |
| 12 | 2 | CN | MCGRAW | FOROUZAN | 200 |
| 13 | 3 | CPP | NAVATHE | SUMITA | 300 |
+-----------+---------+-------+----------------+-------------+--------------+
3 rows in set (0.00 sec)

QUERY2: Get the particulars of borrowers who have borrowed more than 3 books, but
from Jan 2017 to Jun 2017.

mysql> SELECT CARD_NO FROM BOOK_LENDING WHERE DATE_OUT>='2017-01-01'


AND DATE_OUT<='2017-06-30' GROUP BY CARD_NO HAVING COUNT(*)>3;
Empty set (0.00 sec)

QUERY3: Delete a book in BOOK table. Update the contents of other tables to reflect
this data manipulation operation.

mysql> DELETE FROM BOOK WHERE BOOK_ID=1;


Query OK, 1 row affected (0.03 sec)

QUERY4: Partition the BOOK table based on year of publication. Demonstrate its
working with a simple query.

mysql> CREATE TABLE BOOK1(BOOK_ID INT(5),TITLE


VARCHAR(20),PUBLISHER_NAME VARCHAR(20),PUB_YEAR INT(5));
Query OK, 0 rows affected (0.07 sec)

mysql> ALTER TABLE BOOK1 ADD CONSTRAINT PRIMARY


KEY(BOOK_ID,PUB_YEAR);
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE BOOK1 PARTITION BY RANGE(PUB_YEAR)(PARTITION P0
VALUES LESS THAN(2000),PARTITION P1 VALUES LESS THAN MAXVALUE);
Query OK, 0 rows affected (0.30 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> DESC BOOK1;


+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| BOOK_ID | int(5) | NO | PRI | 0 | |
| TITLE | varchar(20) | YES | | NULL | |
| PUBLISHER_NAME | varchar(20) | YES | | NULL | |
| PUB_YEAR | int(5) | NO | PRI | 0 | |
+----------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> INSERT INTO BOOK1 VALUES(01,'VB','MCGRAW',1999);


Query OK, 1 row affected (0.04 sec)

mysql> INSERT INTO BOOK1 VALUES(02,'C','KANETKAR',2002);


Query OK, 1 row affected (0.04 sec)

mysql> INSERT INTO BOOK1 VALUES(03,'C++','BALGURU',2018);


Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM BOOK1;


+---------+-------+----------------+----------+
| BOOK_ID | TITLE | PUBLISHER_NAME | PUB_YEAR |
+---------+-------+----------------+----------+
| 1 | VB | MCGRAW | 1999 |
| 2 | C | KANETKAR | 2002 |
| 3 | C++ | BALGURU | 2018 |
+---------+-------+----------------+----------+
3 rows in set (0.01 sec)

mysql> SELECT PARTITION_NAME,TABLE_ROWS FROM


INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME='BOOK1';
+----------------+------------+
| PARTITION_NAME | TABLE_ROWS |
+----------------+------------+
P0 | 1|
| P1 | 2|
+----------------+------------+
2 rows in set (0.01 sec)

mysql> SELECT * FROM BOOK1 WHERE PUB_YEAR<2000;


+---------+-------+----------------+----------+
| BOOK_ID | TITLE | PUBLISHER_NAME | PUB_YEAR |
+---------+-------+----------------+----------+
| 1 | VB | MCGRAW | 1999 |
+---------+-------+----------------+----------+
1 row in set (0.00 sec)

mysql> SELECT * FROM BOOK1 WHERE PUB_YEAR>2000;


+---------+-------+----------------+----------+
| BOOK_ID | TITLE | PUBLISHER_NAME | PUB_YEAR |
+---------+-------+----------------+----------+
| 2 | C | KANETKAR | 2002 |
| 3 | C++ | BALGURU | 2018 |
+---------+-------+----------------+----------+
2 rows in set (0.00 sec)

mysql> ALTER TABLE BOOK1 TRUNCATE PARTITION P0;


Query OK, 0 rows affected (0.04 sec)

mysql> SELECT * FROM BOOK1;


+---------+-------+----------------+----------+
| BOOK_ID | TITLE | PUBLISHER_NAME | PUB_YEAR |
+---------+-------+----------------+----------+
| 2 | C | KANETKAR | 2002 |
| 3 | C++ | BALGURU | 2018 |
+---------+-------+----------------+----------+
2 rows in set (0.00 sec)

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

mysql> CREATE VIEW TOTAL_BOOKS AS SELECT BOOK_ID,SUM(N0_OF_COPIES) AS


SUM FROM BOOK_COPIES GROUP BY BOOK_ID;
Query OK, 0 rows affected (0.05 sec)

mysql> CREATE VIEW BOOK_BORROWED AS SELECT BOOK_ID,COUNT(* )AS SUM


FROM BOOK_LENDING GROUP BY BOOK_ID;
Query OK, 0 rows affected (0.05 sec)

mysql> CREATE VIEW BOOKS_AVAILABLE AS SELECT A.BOOK_ID,IFNULL(A.SUM-


B.SUM,A.SUM) AS BOOKS_AVAILABLE FROM TOTAL_BOOKS A LEFT OUTER JOIN
BOOK_BORROWED B ON A.BOOK_ID=B.BOOK_ID;
Query OK, 0 rows affected (0.04 sec)

mysql> SELECT * FROM BOOKS_AVAILABLE;


+---------+-----------------+
| BOOK_ID | BOOKS_AVAILABLE |
+---------+-----------------+
| 1| 99 |
| 2| 199 |
| 3| 299 |
+---------+-----------------+
3 rows in set (0.00 sec)

CONCLUSION: Tables are created and the values have been inserted accordingly and all the
mentioned queries have been executed.
Lab Program 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)

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
their cities (Use UNION operation.)
4. Create a view that finds the salesman who has the customer with the highest
order of a day.
5. Demonstrate the DELETE operation by removing salesman with id 1000. All
his orders must also be deleted.

AIM: Create table, querying the Order database and perform all the operations using sql.

ER DIAGRAM:

SCHEMA DIAGRAM
TABLE CREATION

create table salesman(salesman_id int(5) not null primary key, name varchar(10), city varchar(10),
commission int(8));
Query OK, 0 rows affected (0.09 sec)

mysql> desc salesman;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| salesman_id | int(5) | NO | PRI | NULL | |
| name | varchar(10) | YES | | NULL | |
| city | varchar(10) | YES | | NULL | |
| commission | int(8) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

create table customer(customer_id int(5) not null primary key, cust_name varchar(10), city
varchar(10), grade int(8),salesman_id int (5), foreign key(salesman_id) references
salesman(salesman_id) on delete cascade);
Query OK, 0 rows affected (0.07 sec)

mysql> desc customer;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| customer_id | int(5) | NO | PRI | NULL | |
| cust_name | varchar(10) | YES | | NULL | |
| city | varchar(10) | YES | | NULL | |
| grade | int(8) | YES | | NULL | |
| salesman_id | int(5) | YES | MUL | NULL | |
+-------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

mysql> create table order1(ord_no int(5) primary key,purchase_amt int(7), ord_date


date,customer_id int(5), salesman_id int(5), foreign key(customer_id) references
customer(customer_id) on delete cascade,foreign key(salesman_id) references
salesman(salesman_id) on delete cascade); c
Query OK, 0 rows affected (0.12 sec)

mysql> desc order1;


+--------------+--------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------+------+-----+---------+-------+
| ord_no | int(5) | NO | PRI | NULL | |
| purchase_amt | int(7) | YES | | NULL | |
| ord_date | date | YES | | NULL | |
| customer_id | int(5) | YES | MUL | NULL | |
| salesman_id | int(5) | YES | MUL | NULL | |
+--------------+--------+------+-----+---------+-------+
5 rows in set (0.01 sec)

mysql> insert into salesman values(1,'abc','delhi', 2000);


Query OK, 1 row affected (0.04 sec)

mysql> insert into salesman values(2,'abc','kolkatha', 1000);


Query OK, 1 row affected (0.04 sec)

mysql> insert into salesman values(3,'xyz','chennai', 1000);


Query OK, 1 row affected (0.03 sec)

mysql> insert into salesman values(4,'pqr','hyderabad', 3000);


Query OK, 1 row affected (0.03 sec)

mysql> select * from salesman;


+-------------+------+-----------+------------+
| salesman_id | name | city | commission |
+-------------+------+-----------+------------+
| 1 | abc | delhi | 2000 |
| 2 | abc | kolkatha | 1000 |
| 3 | xyz | chennai | 1000 |
| 4 | pqr | hyderabad | 3000 |
+-------------+------+-----------+------------+
4 rows in set (0.00 sec)

mysql> insert into customer values (101,'abc','delhi',1,1);


Query OK, 1 row affected (0.04 sec)

mysql> insert into customer values (102,'xyz','chennai',2,3);


Query OK, 1 row affected (0.04 sec)

mysql> insert into customer values (103,'pqr','hyderabad',3,4);


Query OK, 1 row affected (0.04 sec)

mysql> select * from customer;


+-------------+-----------+-----------+-------+-------------+
| customer_id | cust_name | city | grade | salesman_id |
+-------------+-----------+-----------+-------+-------------+
| 101 | abc | delhi | 1 | 1|
| 102 | xyz | chennai | 2 | 3|
| 103 | pqr | hyderabad | 3 | 4|
+-------------+-----------+-----------+-------+-------------+
3 rows in set (0.00 sec)

mysql> insert into order1 values(01,50000,20001005,101,1);


Query OK, 1 row affected (0.04 sec)

mysql> insert into order1 values(02,52000,20101005,102,2);


Query OK, 1 row affected (0.03 sec)

mysql> insert into order1 values(03,32000,20101106,103,4);


Query OK, 1 row affected (0.04 sec)

mysql> insert into order1 values(04,62000,20111104,102,3);


Query OK, 1 row affected (0.03 sec)

mysql> select * from order1;


+--------+--------------+------------+-------------+-------------+
| ord_no | purchase_amt | ord_date | customer_id | salesman_id |
+--------+--------------+------------+-------------+-------------+
| 1| 50000 | 2000-10-05 | 101 | 1|
| 2| 52000 | 2010-10-05 | 102 | 2|
| 3| 32000 | 2010-11-06 | 103 | 4|
| 4| 62000 | 2011-11-04 | 102 | 3|
+--------+--------------+------------+-------------+-------------+
4 rows in set (0.00 sec)

Query1: Count the customers with grades above Bangalore’s average.

mysql> select count(*) from customer where grade>(select avg(grade)from customer where
city='bangalore');
+----------+
| count(*) |
+----------+
| 2|
+----------+
1 row in set (0.00 sec)

Query2: Find the name and numbers of all salesman who had more than one customer

mysql> select name,count(customer_id) from salesman s,customer c where s.salesman_id =


c.salesman_id group by name having count(customer_id)>1;
+------+--------------------+
| name | count(customer_id) |
+------+--------------------+
| abc | 2|
+------+--------------------+
1 row in set (0.00 sec)

mysql> select * from customer;


+-------------+-----------+-----------+-------+-------------+
| customer_id | cust_name | city | grade | salesman_id |
+-------------+-----------+-----------+-------+-------------+
| 3 | bc | chennai | 4 | 2|
| 4 | ar | hyderabad | 5 | 1|
| 7 | gh | delhi | 3 | 3|
| 9 | yq | bangalore | 3 | 4|
+-------------+-----------+-----------+-------+-------------+
4 rows in set (0.00 sec)

mysql> select * from salesman;


+-------------+------+-----------+------------+
| salesman_id | name | city | commission |
+-------------+------+-----------+------------+
| 1 | abc | delhi | 2000 |
| 2 | abc | kolkatha | 1000 |
| 3 | xyz | chennai | 1000 |
| 4 | pqr | hyderabad | 3000 |
+-------------+------+-----------+------------+
4 rows in set (0.00 sec)

Query3: List all the salesman and indicate those who have and don’t have customers in
their cities (Use UNION operation.)
mysql> (select distinct s.salesman_id,s.name,'same city' from customer c,salesman s where
c.salesman_id=s.salesman_id and c.city=s.city) union (select distinct
s.salesman_id,s.name,'different city' from customer c,salesman s where
c.salesman_id=s.salesman_id and c.city != s.city) union (select distinct s.salesman_id,s.name,'no
customer' from salesman s where salesman_id not in (select salesman_id from customer));
+-------------+------+-------------+
| salesman_id | name | same city |
+-------------+------+-------------+
| 1 | abc | same city |
| 3 | xyz | same city |
| 4 | pqr | same city |
| 2 | abc | no customer |
+-------------+------+-------------+
4 rows in set (0.00 sec)

Query4: Create a view that finds the salesman who has the customer with the highest
order of a day.

mysql> create view sales1 as select o.ord_date,s.salesman_id,s.name from salesman s,order1 o


where s.salesman_id = o.salesman_id and o.purchase_amt =(select max(purchase_amt) from order1
od where od.ord_date = o.ord_date);
Query OK, 0 rows affected (0.03 sec)

mysql> select * from sales1;


+------------+-------------+------+
| ord_date | salesman_id | name |
+------------+-------------+------+
| 2010-10-05 | 2 | abc |
| 2011-11-04 | 3 | xyz |
| 2000-10-05 | 3 | xyz |
| 2010-11-06 | 4 | pqr |
+------------+-------------+------+
4 rows in set (0.00 sec)

Query5: Demonstrate the DELETE operation by removing salesman with id 1000. All
his orders must also be deleted.

mysql> delete from salesman where salesman_id=1;

Note: Insert tuple with salesman id=1000

CONCLUSION: Tables are created and the values have been inserted accordingly and all the
mentioned queries have been executed.
Lab Program 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)

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 (use JOIN operation).
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.
5. Update rating of all movies directed by ‘Steven Spielberg’ to 5.

AIM: Create table, querying the Movie database and perform all the operations using sql.

ER DIAGRAM:
SCHEMA DIAGRAM:

TABLE CREATION

mysql> create table actor(act_id int(5) primary key,act_name varchar(20),act_gender varchar(5));


Query OK, 0 rows affected (0.09 sec)

mysql> create table director(director_id int(5) primary key,director_name


varchar(20),director_phone double);
Query OK, 0 rows affected (0.09 sec)

mysql> create table movies(mov_id int(5) primary key,mov_title varchar(20),mov_year


int,mov_language varchar(20),director_id int(5),foreign key(director_id) references
director(director_id) on delete cascade);
Query OK, 0 rows affected (0.08 sec)

mysql> create table movie_cast(act_id int(5),mov_id int(5),role varchar(20),primary


key(act_id,mov_id),foreign key(act_id) references actor(act_id) on delete cascade,foreign
key(mov_id) references movies(mov_id) on delete cascade);
Query OK, 0 rows affected (0.10 sec)

mysql> create table rating(mov_id int(5) primary key,rev_stars decimal(2,1),foreign key(mov_id)


references movies(mov_id) on delete cascade);
Query OK, 0 rows affected (0.07 sec)

mysql> insert into actor values(01,'rajesh','M');


Query OK, 1 row affected (0.06 sec)

mysql> insert into actor values(02,'akshay','M');


Query OK, 1 row affected (0.04 sec)

mysql> insert into actor values(03,'aishwarya','F');


Query OK, 1 row affected (0.04 sec)
mysql> insert into actor values(04,'deepika','F');
Query OK, 1 row affected (0.05 sec)

mysql> insert into director values(100,'rakesh',89526358536);


Query OK, 1 row affected (0.04 sec)

mysql> insert into director values(101,'karan',8952635567);


Query OK, 1 row affected (0.04 sec)

mysql> insert into director values(103,'aktar',8952635577);


Query OK, 1 row affected (0.05 sec)

mysql> insert into director values(104,'bansali',9952635577);


Query OK, 1 row affected (0.04 sec)

mysql> insert into movies values(200,'knph',2000,'hindi',100);


Query OK, 1 row affected (0.04 sec)

mysql> insert into movies values(201,'k3g',2001,'hindi',101);


Query OK, 1 row affected (0.04 sec)

mysql> insert into movies values(203,'kirik party',2016,'kannada',103);


Query OK, 1 row affected (0.05 sec)

mysql> insert into movies values(204,'khamoshi',1994,'hindi',104);


Query OK, 1 row affected (0.04 sec)

mysql> insert into movie_cast values(01,200,'hero');


Query OK, 1 row affected (0.03 sec)

mysql> insert into movie_cast values(02,201,'hero');


Query OK, 1 row affected (0.04 sec)

mysql> insert into movie_cast values(03,203,'heroine');


Query OK, 1 row affected (0.04 sec)

mysql> insert into movie_cast values(04,204,'heroine');


Query OK, 1 row affected (0.04 sec)

mysql> insert into movie_cast values(03,201,'heroine');


Query OK, 1 row affected (0.04 sec)

mysql> insert into movie_cast values(01,204,'hero');


Query OK, 1 row affected (0.04 sec)

mysql> insert into rating values(200,3.5);


Query OK, 1 row affected (0.05 sec)

mysql> insert into rating values(201,1.5);


Query OK, 1 row affected (0.03 sec)

mysql> insert into rating values(203,4);


Query OK, 1 row affected (0.05 sec)

mysql> insert into rating values(204,4.5);


Query OK, 1 row affected (0.04 sec)

mysql> select * from actor;


+--------+-----------+------------+
| act_id | act_name | act_gender |
+--------+-----------+------------+
| 1 | rajesh | M |
| 2 | akshay | M |
| 3 | aishwarya | F |
| 4 | deepika | F |
+--------+-----------+------------+
4 rows in set (0.00 sec)

mysql> select * from director;


+-------------+---------------+----------------+
| director_id | director_name | director_phone |
+-------------+---------------+----------------+
| 100 | rakesh | 8952635853 |
| 101 | karan | 8952635567 |
| 103 | aktar | 8952635577 |
| 104 | bansali | 9952635577 |
+-------------+---------------+----------------+
4 rows in set (0.00 sec)

mysql> select * from movies;


+--------+-------------+----------+--------------+-------------+
| mov_id | mov_title | mov_year | mov_language | director_id |
+--------+-------------+----------+--------------+-------------+
| 200 | knph | 2000 | hindi | 100 |
| 201 | k3g | 2001 | hindi | 101 |
| 203 | kirik party | 2016 | kannada | 103 |
| 204 | khamoshi | 1994 | hindi | 104 |
+--------+-------------+----------+--------------+-------------+
4 rows in set (0.00 sec)

mysql> select * from movie_cast;


+--------+--------+---------+
| act_id | mov_id | role |
+--------+--------+---------+
| 1 | 200 | hero |
| 1 | 204 | hero |
| 2 | 201 | hero |
| 3 | 201 | heroine |
| 3 | 203 | heroine |
| 4 | 204 | heroine |
+--------+--------+---------+
6 rows in set (0.01 sec)

mysql> select * from rating;


+--------+-----------+
| mov_id | rev_stars |
+--------+-----------+
| 200 | 3.5 |
| 201 | 1.5 |
| 203 | 4.0 |
| 204 | 4.5 |
+--------+-----------+
4 rows in set (0.00 sec)

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

mysql> select mov_title from movies m,director d where m.director_id=d.director_id and


d.director_name ='bansali';
+-----------+
| mov_title |
+-----------+
| khamoshi |
+-----------+
1 row in set (0.00 sec)

QUERY 2:

mysql> select distinct m.mov_title,c.act_id from movies m,movie_cast c where


m.mov_id=c.mov_id and c.act_id in(select act_id from movie_cast group by act_id having
count(mov_id)>1);
+-------------+--------+
| mov_title | act_id |
+-------------+--------+
| knph | 1|
| k3g | 3|
| kirik party | 3|
| khamoshi | 1|
+-------------+--------+
4 rows in set (0.00 sec)

QUERY 3: Find the movie names where one or more actors acted in two or more movies.

mysql> select act_name,mov_title,mov_year 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;
+-----------+-------------+----------+
| act_name | mov_title | mov_year |
+-----------+-------------+----------+
| aishwarya | kirik party | 2016 |
| rajesh | khamoshi | 1994 |
| deepika | khamoshi | 1994 |
+-----------+-------------+----------+
3 rows in set (0.00 sec)

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

mysql> select m.mov_title,rev_stars from movies m,rating r where m.mov_id=r.mov_id and


r.rev_stars>0 order by mov_title;
+-------------+-----------+
| mov_title | rev_stars |
+-------------+-----------+
| k3g | 1.5 |
| k3g | 1.5 |
| khamoshi | 4.5 |
| khamoshi | 4.5 |
| kirik party | 4.0 |
| knph | 3.5 |
+-------------+-----------+
6 rows in set (0.02 sec)

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

mysql> update rating set rev_stars= 5 where mov_id in(select mov_id from movies m ,director d
where d.director_id=m.director_id and director_name='rakesh');
Query OK, 1 row affected (0.09 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from rating;


+--------+-----------+
| mov_id | rev_stars |
+--------+-----------+
| 200 | 5.0 |
| 201 | 1.5 |
| 203 | 4.0 |
| 204 | 4.5 |
+--------+-----------+
4 rows in set (0.00 sec)

PS: Insert a tuple with director name as Steven Spielberg

CONCLUSION: Tables are created and the values have been inserted accordingly and all the
mentioned queries have been executed.
Lab Program 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)

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
each section.
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
corresponding table 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 8th semester A, B, and C section students.

AIM: Create table, querying the College database and perform all the operations using sql.

ER DIAGRAM
SCHEMA DIAGRAM

TABLE CREATION:
mysql> create table student(usn varchar(10) primary key,name varchar(20),address
varchar(20),phone double,gender varchar(5));
Query OK, 0 rows affected (0.25 sec)

mysql> create table semsec(ssid int(5) primary key,sem int(5),sec varchar(5));


Query OK, 0 rows affected (0.07 sec)

mysql> create table class(usn varchar(10) primary key,ssid int(5),foreign key(usn) references
student(usn) on delete cascade,foreign key(ssid) references semsec(ssid));
Query OK, 0 rows affected (0.17 sec)

mysql> create table subject(sub_code varchar(10) primary key,title varchar(10),sem int(5),credits


int(5));Query OK, 0 rows affected (0.11 sec)

mysql> create table IA_marks1(usn varchar(10),sub_code varchar(10),ssid int(5),test1 int,test2


int,test3 int,final_IA int,primary key(usn,sub_code,ssid),foreign key(usn) references
student(usn),foreign key(sub_code) references subject(sub_code),foreign key(ssid) references
semsec(ssid));Query OK, 0 rows affected (0.15 sec)

mysql> insert into student values('1GD16CS001','akanksha','bangalore',9090786756,'F');


Query OK, 1 row affected (0.08 sec)

mysql> insert into student values('1GD16CS002','rohit','pune',9090786766,'M');


Query OK, 1 row affected (0.03 sec)

mysql> insert into student values('1GD16CS003','kirti','delhi',8890786766,'F');


Query OK, 1 row affected (0.03 sec)

mysql> insert into student values('1GD16CS004','tarun','bangalore',8890786796,'M');


Query OK, 1 row affected (0.07 sec)
mysql> insert into semsec values(01,4,'C');
Query OK, 1 row affected (0.07 sec)

mysql> insert into semsec values(02,7,'A');


Query OK, 1 row affected (0.04 sec)

mysql> insert into semsec values(03,1,'B');


Query OK, 1 row affected (0.06 sec)

mysql> insert into semsec values(04,4,'C');


Query OK, 1 row affected (0.05 sec)

mysql> insert into class values('1GD16CS001',04);


Query OK, 1 row affected (0.08 sec)

mysql> insert into class values('1GD16CS002',01);


Query OK, 1 row affected (0.04 sec)

mysql> insert into class values('1GD16CS003',02);


Query OK, 1 row affected (0.03 sec)

mysql> insert into class values('1GD16CS004',03);


Query OK, 1 row affected (0.04 sec)

mysql> insert into subject values('15CS32','DS',3,4);


Query OK, 1 row affected (0.07 sec)

mysql> insert into subject values('15CS33','DBMS',5,4);


Query OK, 1 row affected (0.04 sec)

mysql> insert into subject values('15CS34','CO',7,3);


Query OK, 1 row affected (0.05 sec)

mysql> insert into subject values('15CS13','PCD',1,5);


Query OK, 1 row affected (0.06 sec)

mysql> insert into IA_marks1 values('1GD16CS001','15CS13',01,25,15,20,22);


Query OK, 1 row affected (0.09 sec)

mysql> insert into IA_marks1 values('1GD16CS002','15CS34',02,12,14,16,15);


Query OK, 1 row affected (0.06 sec)

mysql> insert into IA_marks1 values('1GD16CS003','15CS33',03,18,19,14,16);


Query OK, 1 row affected (0.04 sec)

mysql> insert into IA_marks1 values('1GD16CS004','15CS32',04,10,9,8,9);


Query OK, 1 row affected (0.03 sec)

mysql> insert into IA_marks1 values('1GD16CS001','15CS33',01,30,18,10,25);


Query OK, 1 row affected (0.03 sec)

mysql> insert into IA_marks1 values('1GD16CS001','15CS34',01,30,18,10,30);


Query OK, 1 row affected (0.04 sec)

mysql> select * from student;


+------------+----------+-----------+------------+--------+
| usn | name | address | phone | gender |
+------------+----------+-----------+------------+--------+
| 1GD16CS001 | akanksha | bangalore | 9090786756 | F |
| 1GD16CS002 | rohit | pune | 9090786766 | M |
| 1GD16CS003 | kirti | delhi | 8890786766 | F |
| 1GD16CS004 | tarun | bangalore | 8890786796 | M |
+------------+----------+-----------+------------+--------+
4 rows in set (0.00 sec)

mysql> select * from semsec;


+------+------+------+
| ssid | sem | sec |
+------+------+------+
| 1| 4|C |
| 2| 7|A |
| 3| 1|B |
| 4| 4|C |
+------+------+------+
4 rows in set (0.00 sec)

mysql> select * from class;


+------------+------+
| usn | ssid |
+------------+------+
| 1GD16CS002 | 1 |
| 1GD16CS003 | 2 |
| 1GD16CS004 | 3 |
| 1GD16CS001 | 4 |
+------------+------+
4 rows in set (0.00 sec)

mysql> select * from subject;


+----------+-------+------+---------+
| sub_code | title | sem | credits |
+----------+-------+------+---------+
| 15CS13 | PCD | 1 | 5|
| 15CS32 | DS | 3 | 4|
| 15CS33 | DBMS | 5 | 4|
| 15CS34 | CO | 7 | 3|
+----------+-------+------+---------+
4 rows in set (0.00 sec)

mysql> select * from IA_marks1;


+------------+----------+------+-------+-------+-------+----------+
| usn | sub_code | ssid | test1 | test2 | test3 | final_IA |
+------------+----------+------+-------+-------+-------+----------+
| 1GD16CS001 | 15CS13 | 1 | 25 | 15 | 20 | 22 |
| 1GD16CS001 | 15CS33 | 1 | 30 | 18 | 10 | 25 |
| 1GD16CS001 | 15CS34 | 1 | 30 | 18 | 10 | 30 |
| 1GD16CS002 | 15CS34 | 2 | 12 | 14 | 16 | 15 |
| 1GD16CS003 | 15CS33 | 3 | 18 | 19 | 14 | 16 |
| 1GD16CS004 | 15CS32 | 4 | 10 | 9 | 8 | 9|
+------------+----------+------+-------+-------+-------+----------+
6 rows in set (0.00 sec)
QUERY1: List all the student details studying in fourth semester ‘C’ section.

mysql> select s.* 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';
+------------+----------+-----------+------------+--------+
| usn | name | address | phone | gender |
+------------+----------+-----------+------------+--------+
| 1GD16CS002 | rohit | pune | 9090786766 | M |
| 1GD16CS001 | akanksha | bangalore | 9090786756 | F |
+------------+----------+-----------+------------+--------+
2 rows in set (0.02 sec)

QUERY 2: Compute the total number of male and female students in each semester and each
section.
mysql> select sem,sec,gender ,count(gender) from student natural join class natural join semsec
group by sem,sec,gender;
+------+------+--------+---------------+
| sem | sec | gender | count(gender) |
+------+------+--------+---------------+
| 1|B |M | 1|
| 4|C |F | 1|
| 4|C |M | 1|
| 7|A |F | 1|
+------+------+--------+---------------+
4 rows in set (0.03 sec)

mysql> insert into student values('1GD16CS005','akash','bangalore',9870786756,'M');


Query OK, 1 row affected (0.06 sec)

mysql> insert into class values('1GD16CS005',01);


Query OK, 1 row affected (0.05 sec)

mysql> select sem,sec,gender ,count(gender) from student natural join class natural join semsec
group by sem,sec,gender;
+------+------+--------+---------------+
| sem | sec | gender | count(gender) |
+------+------+--------+---------------+
| 1|B |M | 1|
| 4|C |F | 1|
| 4|C |M | 2|
| 7|A |F | 1|
+------+------+--------+---------------+ 4 rows in set (0.03 sec)

QUERY 3: Create a view of Test1 marks of student USN ‘1GD16CS001’ in all subjects

mysql> create view testmrk as select usn,sub_code,test1 from IA_marks1 where


usn='1GD16CS001';
Query OK, 0 rows affected (0.04 sec)

mysql> select * from testmrk;


+------------+----------+-------+
| usn | sub_code | test1 |
+------------+----------+-------+
| 1GD16CS001 | 15CS13 | 25 |
| 1GD16CS001 | 15CS33 | 30 |
| 1GD16CS001 | 15CS34 | 30 |
+------------+----------+-------+
3 rows in set (0.00 sec)

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

mysql> update IA_marks1 set final_IA =(test1+test2+test3-least(test1,test2,test3))/2;


Query OK, 5 rows affected (0.04 sec)
Rows matched: 6 Changed: 5 Warnings: 0

mysql> select * from IA_marks1;


+------------+----------+------+-------+-------+-------+----------+
| usn | sub_code | ssid | test1 | test2 | test3 | final_IA |
+------------+----------+------+-------+-------+-------+----------+
| 1GD16CS001 | 15CS13 | 1 | 25 | 15 | 20 | 23 |
| 1GD16CS001 | 15CS33 | 1 | 30 | 18 | 10 | 24 |
| 1GD16CS001 | 15CS34 | 1 | 30 | 18 | 10 | 24 |
| 1GD16CS002 | 15CS34 | 2 | 12 | 14 | 16 | 15 |
| 1GD16CS003 | 15CS33 | 3 | 18 | 19 | 14 | 19 |
| 1GD16CS004 | 15CS32 | 4 | 10 | 9 | 8 | 10 |
+------------+----------+------+-------+-------+-------+----------+
6 rows in set (0.00 sec)

QUERY5: 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 8th semester A, B, and C section students.

mysql> select s.usn,s.name,i.final_IA,(case when i.final_IA between 17 and 30 then 'outstanding'


when i.final_IA between 12 and 16 then 'average' else 'weak' end) as cat from student s,semsec
ss,IA_marks1 i,subject b where s.usn=i.usn and ss.ssid=i.ssid and b.sub_code = i.sub_code and
ss.sem=7;
+------------+-------+----------+---------+
| usn | name | final_IA | cat |
+------------+-------+----------+---------+
| 1GD16CS002 | rohit | 15 | average |
+------------+-------+----------+---------+
1 row in set (0.00 sec)

(P.S : put any row with semester 8 and maximum marks should be 20)

CONCLUSION: Tables are created and the values have been inserted accordingly and all the
mentioned queries have been executed.
Lab Program 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)

Write SQL queries to


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
controlledby department number 5 (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.

AIM: Create table, querying the Company database and perform all the operations using sql.

ER DIAGRAM
SCHEMA DIAGRAM:

TABLE CREATION:
mysql> create table department(dno int(5) primary key, dname varchar(10), mgrssn varchar(10),
mgrstartdate date);
Query OK, 0 rows affected (0.07 sec)

mysql> create table employee(ssn varchar(10) primary key, name varchar(10), address varchar(15),
sex varchar(4), salary double, superssn varchar(10), dno int(5),foreign key(superssn) references
employee(ssn),foreign key(dno) references department(dno));
Query OK, 0 rows affected (0.09 sec)

mysql> alter table department add foreign key(mgrssn) references employee(ssn);


Query OK, 0 rows affected (0.18 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> create table dlocation(dno int(5), dloc varchar(15), primary key(dno,dloc), foreign key(dno)
references department(dno));
Query OK, 0 rows affected (0.08 sec)

mysql> create table project(pno int(5) primary key, pname varchar(10), ploc varchar(10), dno
int(5), foreign key(dno) references department(dno) on delete cascade);
Query OK, 0 rows affected (0.13 sec)

mysql> create table works_on(ssn varchar(10), pno int(5), hours int, primary key(ssn,pno), foreign
key(ssn) references employee(ssn) on delete cascade, foreign key(pno) references project(pno) on
delete cascade);
Query OK, 0 rows affected (0.12 sec)

mysql> desc employee;


+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| ssn | varchar(10) | NO | PRI | NULL | |
| name | varchar(10) | YES | | NULL | |
| address | varchar(15) | YES | | NULL | |
| sex | varchar(4) | YES | | NULL | |
| salary | double | YES | | NULL | |
| superssn | varchar(10) | YES | MUL | NULL | |
| dno | int(5) | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+-------+
7 rows in set (0.01 sec)

mysql> desc department;


+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| dno | int(5) | NO | PRI | NULL | |
| dname | varchar(10) | YES | | NULL | |
| mgrssn | varchar(10) | YES | MUL | NULL | |
| mgrstartdate | date | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
4 rows in set (0.02 sec)

mysql> desc dlocation;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| dno | int(5) | NO | PRI | 0 | |
| dloc | varchar(15) | NO | PRI | | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)

mysql> desc project;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| pno | int(5) | NO | PRI | NULL | |
| pname | varchar(10) | YES | | NULL | |
| ploc | varchar(10) | YES | | NULL | |
| dno | int(5) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.02 sec)

mysql> desc works_on;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ssn | varchar(10) | NO | PRI | | |
| pno | int(5) | NO | PRI | 0 | |
| hours | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> insert into employee(ssn,name,address,sex,salary)


values('W101','scott','bangalore','M',25000);
Query OK, 1 row affected (0.03 sec)

mysql> insert into employee(ssn,name,address,sex,salary)


values('W102','smith','mysore','M',15000);
Query OK, 1 row affected (0.04 sec)

mysql> insert into employee(ssn,name,address,sex,salary)


values('M102','john','mangalore','M',600000);
Query OK, 1 row affected (0.03 sec)

mysql> insert into employee(ssn,name,address,sex,salary)


values('M101','kate','mangalore','F',650000);
Query OK, 1 row affected (0.04 sec)

mysql> insert into department values(01,'sales','W101',20001002);


Query OK, 1 row affected (0.04 sec)

mysql> insert into department values(02,'account','W102',19901002);


Query OK, 1 row affected (0.04 sec)

mysql> insert into department values(03,'account','M102',19900503);


Query OK, 1 row affected (0.04 sec)

mysql> insert into department values(04,'sales','M101',19990503);


Query OK, 1 row affected (0.04 sec)

mysql> insert into dlocation values(01, 'whitefiled');


Query OK, 1 row affected (0.04 sec)

mysql> insert into dlocation values(02, 'hoodi');


Query OK, 1 row affected (0.04 sec)

mysql> insert into dlocation values(03, 'rajajinagar');


Query OK, 1 row affected (0.03 sec)

mysql> insert into dlocation values(04, 'kormangala');


Query OK, 1 row affected (0.04 sec)

mysql> insert into project values(301,'IOT', 'USA', 01);


Query OK, 1 row affected (0.04 sec)

mysql> insert into project values(302,'IOT', 'USA', 02);


Query OK, 1 row affected (0.04 sec)

mysql> insert into project values(303,'CC', 'Uk', 03);


Query OK, 1 row affected (0.04 sec)

mysql> insert into project values(304,'ML', 'Uk', 04);


Query OK, 1 row affected (0.05 sec)

mysql> insert into works_on values('W101',301,12);


Query OK, 1 row affected (0.05 sec)

mysql> insert into works_on values('W102',302,10);


Query OK, 1 row affected (0.04 sec)

mysql> insert into works_on values('M101',303,17);


Query OK, 1 row affected (0.03 sec)

mysql> insert into works_on values('M102',304,14);


Query OK, 1 row affected (0.04 sec)

mysql> select * from employee;


+------+-------+-----------+------+--------+----------+------+
| ssn | name | address | sex | salary | superssn | dno |
+------+-------+-----------+------+--------+----------+------+
| M101 | kate | mangalore | F | 650000 | NULL | NULL |
| M102 | john | mangalore | M | 600000 | NULL | NULL |
| W101 | scott | bangalore | M | 25000 | NULL | NULL |
| W102 | smith | mysore | M | 15000 | NULL | NULL |
+------+-------+-----------+------+--------+----------+------+
4 rows in set (0.00 sec)

mysql> select * from department;


+-----+---------+--------+--------------+
| dno | dname | mgrssn | mgrstartdate |
+-----+---------+--------+--------------+
| 1 | sales | W101 | 2000-10-02 |
| 2 | account | W102 | 1990-10-02 |
| 3 | account | M102 | 1990-05-03 |
| 4 | sales | M101 | 1999-05-03 |
+-----+---------+--------+--------------+
4 rows in set (0.00 sec)

mysql> select * from dlocation;


+-----+-------------+
| dno | dloc |
+-----+-------------+
| 1 | whitefiled |
| 2 | hoodi |
| 3 | rajajinagar |
| 4 | kormangala |
+-----+-------------+
4 rows in set (0.00 sec)

mysql> select * from project;


+-----+-------+------+------+
| pno | pname | ploc | dno |
+-----+-------+------+------+
| 301 | IOT | USA | 1 |
| 302 | IOT | USA | 2 |
| 303 | CC | Uk | 3 |
| 304 | ML | Uk | 4 |
+-----+-------+------+------+
4 rows in set (0.00 sec)

mysql> select * from works_on;


+------+-----+-------+
| ssn | pno | hours |
+------+-----+-------+
| M101 | 303 | 17 |
| M102 | 304 | 14 |
| W101 | 301 | 12 |
| W102 | 302 | 10 |
+------+-----+-------+
4 rows in set (0.01 sec)

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

mysql> (select pno from project p,department d,employee e where p.dno=d.dno and e.ssn=d.mgrssn
and e.name='scott')
union
(select pno from employee e,works_on w where e.ssn=w.ssn and e.name='scott');
+-----+
| pno |
+-----+
| 301 |
+-----+
1 row in set (0.00 sec)

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

mysql> select e.ssn,salary*1.1 from employee e, works_on w, project p where e.ssn=w.ssn and
w.pno=p.pno and pname='IOT';
+------+--------------------+
| ssn | salary*1.1 |
+------+--------------------+
| W101 | 27500.000000000004 |
| W102 | 16500 |
+------+--------------------+
2 rows in set (0.01 sec)

Query3: 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

mysql> select dname, max(salary), min(salary), avg(salary) from employee e, department d where
e.dno=d.dno and d.dname='account';
+---------+-------------+-------------+-------------+
| dname | max(salary) | min(salary) | avg(salary) |
+---------+-------------+-------------+-------------+
| account | 600000 | 15000 | 307500 |
+---------+-------------+-------------+-------------+
1 row in set (0.00 sec)

Query4: Retrieve the name of each employee who works on all the projects
controlledby department number 5 (use NOT EXISTS operator).

mysql> select e.name from employee e where not exists (select pno from project where dno=4 and
pno not in (select pno from works_on w where w.ssn=e.ssn));
+------+
| name |
+------+
| john |
+------+
1 row in set (0.00 sec)

Query5: 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.

mysql> select dno, count(ssn) from employee where salary> 600000 and dno in (select dno from
employee group by dno having count(ssn)>0);
+------+------------+
| dno | count(ssn) |
+------+------------+
| 4| 1|
+------+------------+
1 row in set (0.00 sec)

CONCLUSION: Tables are created and the values have been inserted accordingly and all the
mentioned queries have been executed.

You might also like