0% found this document useful (0 votes)
30 views62 pages

Dbms Lab Manual 18scheme

The document describes laboratory experiments involving SQL programming on various databases. Part A includes 5 SQL queries on schemas for a Library Database, Order Database, Movie Database, College Database, and Company Database. Part B describes a mini project. The document provides the schemas and sample queries to demonstrate basic SQL operations like retrieving data, joins, aggregates, and views.

Uploaded by

heliumargon2
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
30 views62 pages

Dbms Lab Manual 18scheme

The document describes laboratory experiments involving SQL programming on various databases. Part A includes 5 SQL queries on schemas for a Library Database, Order Database, Movie Database, College Database, and Company Database. Part B describes a mini project. The document provides the schemas and sample queries to demonstrate basic SQL operations like retrieving data, joins, aggregates, and views.

Uploaded by

heliumargon2
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 62

Laboratory Experiments

Part A: SQL Programming

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)

CARD(Card_No)

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.

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 1


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

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 2


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)

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.

5 Consider the schema for Company Database:

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 3


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
Controlled by 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

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 4


LAB EXPERIMENTS

PART A: SQL PROGRAMMING

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

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 5


Schema Diagram

Card
Card_no

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 6


Table Creation

CREATE TABLE PUBLISHER


(PNAME VARCHAR (20) PRIMARY KEY,
PHONE NUMBER(38),
ADDRESS VARCHAR (20));

CREATE TABLE BOOK


(BOOKID NUMBER(38) PRIMARY KEY,
TITLE VARCHAR (20),
PUBYEAR VARCHAR (20),
PNAME REFERENCES PUBLISHER (PNAME) ON DELETE
CASCADE);

CREATE TABLE BOOKAUTHORS


(AUTHORNAME VARCHAR(20) PRIMARY KEY,
BOOKID REFERENCES BOOK (BOOKID) ON DELETE CASCADE);

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 7


CREATE TABLE LIBRARYBRANCH (
BRANCHID NUMBER(10) PRIMARY KEY,
BRANCHNAME VARCHAR(50),
ADDRESS VARCHAR(50));

CREATE TABLE BOOKCOPIES (NOOFCOPIES NUMBER(5),


BOOKID REFERENCES BOOK (BOOKID) ON DELETE CASCADE,
BRANCHID REFERENCES LIBRARYBRANCH (BRANCHID) ON DELETE CASCADE);

CREATE TABLE CARD (CARDNO NUMBER(5) PRIMARY KEY);

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 8


CREATE TABLE BOOKLENDING (

DATEOUT DATE,

DUEDATE DATE,

BOOKID REFERENCES BOOK (BOOKID) ON DELETE CASCADE,

BRANCHID REFERENCES LIBRARYBRANCH (BRANCHID) ON DELETE


CASCADE,

CARDNO REFERENCES CARD (CARDNO) ON DELETE CASCADE);

Insertion of Values to Tables


INSERT INTO PUBLISHER VALUES (‘MCGRAW-HILL’, 9989076587, ‘BANGALORE’);
INSERT INTO PUBLISHER VALUES (‘PEARSON’, 9889076565, ‘NEWDELHI’);

INSERT INTO PUBLISHER VALUES (‘PRENTICE HALL’, 7455679345, ‘HYDERABAD’);


INSERT INTO PUBLISHER VALUES (‘WILEY’, 8970862340, ‘CHENNAI’);

INSERT INTO PUBLISHER VALUES (‘MIT’, 7756120238, ‘BANGALORE’);

Select * from PUBLISHER;

NAME PHONE ADDRESS


MCGRAW-HILL 9989076587 BANGALORE
PEARSON 9889076565 NEWDELHI

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 9


PRENTICE HALL 7455679345 HYDERABAD
WILEY 8970862340 CHENNAI
MIT 7756120238 BANGALORE
INSERT INTO BOOK VALUES (1,’DBMS’,’JAN-2017’, ‘MCGRAW-HILL’);

INSERT INTO BOOK VALUES (2,’ADBMS’,’JUN-2016’, ‘MCGRAW-HILL’);


INSERT INTO BOOK VALUES (3,’CD’,’SEP-2016’, ‘PEARSON’);

INSERT INTO BOOK VALUES (4,’ALGORITHMS’,’SEP-2015’, ‘MIT’);

INSERT INTO BOOK VALUES (5,’OS’,’MAY-2016’, ‘PEARSON’);

Select * from BOOK;

BOOK_ID TITLE PUB_YEAR PUBLISHER_NAME


1 DBMS Jan-2017 MCGRAW-HILL
2 ADBMS Jun-2017 MCGRAW-HILL
3 CD Sep-2016 PEARSON
4 ALGORITHMS Sep-2015 MIT
5 OS May-2016 PEARSON

INSERT INTO BOOKAUTHORS VALUES (’NAVATHE’, 1);

INSERT INTO BOOKAUTHORS VALUES (’NAVATHE’, 2);

INSERT INTO BOOKAUTHORS VALUES (’ULLMAN’, 3);

INSERT INTO BOOKAUTHORS VALUES (’CHARLES’, 4);

INSERT INTO BOOKAUTHORS VALUES (’GALVIN’, 5);

Select * from BOOKAUTHORS;

AUTHORNAME BOOKID
NAVATHE 1
NAVATHE 2

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 10


ULLMAN 3
CHARLES 4
GALVIN 5

INSERT INTO LIBRARYBRANCH VALUES (10,’VIJAY NAGAR’,’BANGALORE’);

INSERT INTO LIBRARYBRANCH VALUES (11,’GAT’,’BANGALORE’);

INSERT INTO LIBRARYBRANCH VALUES (12,’RAJAJI NAGAR’, ’BANGALORE’);

INSERT INTO LIBRARYBRANCH VALUES (13,’NITTE’,’MANGALORE’);

INSERT INTO LIBRARYBRANCH VALUES (14,’MANIPAL’,’UDUPI’);

Select * from LIBRARY_BRANCH;

BRANCHID BRANCHNAME ADRESS


10 VIJAY NAGAR BANGALORE
11 GAT BANGALORE
12 RAJAJI NAGAR BANGALORE
13 NITTE MANGALORE
14 MANIPAL UDUPI
INSERT INTO BOOKCOPIES VALUES (10, 1, 10);
INSERT INTO BOOKCOPIES VALUES (5, 1, 11);
INSERT INTO BOOKCOPIES VALUES (2, 2, 12);
INSERT INTO BOOKCOPIES VALUES (5, 2, 13);
INSERT INTO BOOKCOPIES VALUES (7, 3, 14);
INSERT INTO BOOKCOPIES VALUES (1, 5, 10);
INSERT INTO BOOKCOPIES VALUES (3, 4, 11);
Select * from BOOKCOPIES;

NOOFCOPIES BOOKID BRANCHID


10 1 10
5 1 11
2 2 12

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 11


5 2 13
7 3 14
1 5 10
3 4 11
INSERT INTO CARD VALUES (101);

INSERT INTO CARD VALUES (102);

INSERT INTO CARD VALUES (103);

INSERT INTO CARD VALUES (104);

INSERT INTO CARD VALUES (105);

Select * from CARD;

CARDNO
101
102
103
104
105

INSERT INTO BOOKLENDING VALUES (’01-JAN-17’,’01-JUN-17’, 1, 10, 101);

INSERT INTO BOOKLENDING VALUES (’11-JAN-17’,’11-MAR-17’, 3, 14, 101);

INSERT INTO BOOKLENDING VALUES (’21-FEB-17’,’21-APR-17’, 2, 13, 101);

INSERT INTO BOOKLENDING VALUES (’15-MAR-17’,’15-JUL-17’, 4, 11, 101);

INSERT INTO BOOKLENDING VALUES (‘12-APR-17’,’12-MAY-17’, 1, 11, 104);

Select * from BOOKLENDING;

DATEOUT DUEDATE BOOKID BRANCHID CARDNO


1-Jan-17 1-Jun-17 1 10 101
11-Jan-17 11-Mar-17 3 4 101
21-Feb-17 21-Apr-17 2 13 101
15-Mar-17 15-Jul-17 4 11 101

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 12


12-Apr-17 12-May-17 1 11 104

1.Retrieve details of all books in the library – id, title, name of publisher, authors, number of
copies in each branch, etc.
SELECT LB.BRANCHNAME, B.BOOKID,TITLE, PNAME,AUTHORNAME, NOOFCOPIES
FROM BOOK B, BOOKAUTHORS BA, BOOKCOPIES BC, LIBRARYBRANCH LB
WHERE B.BOOKID = BA.BOOKID AND
BA.BOOKID = BC.BOOKID AND
BC.BRANCHID = LB.BRANCHID
GROUP BY LB.BRANCHNAME, B.BOOKID, TITLE, PNAME,AUTHORNAME, NOOFCOPIES;

BOOK_ID TITLE PNAME AUTHORNAME NOOFCOPIES BRANCHID


1 DBMS MCGRAW-HILL NAVATHE 10 10
1 DBMS MCGRAW-HILL NAVATHE 5 11
2 ADBMS MCGRAW-HILL NAVATHE 2 12
2 ADBMS MCGRAW-HILL NAVATHE 5 13
3 CD PEARSON ULLMAN 7 14
5 OS PEARSON GALVIN 1 10
ALGORITH
4 MS MIT CHARLES 3 11

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

SELECT CARDNO
FROM BOOKLENDING
WHERE DATEOUT BETWEEN ’01-JAN-2017’ AND ’01-JUN-2017’
GROUP BY CARDNO HAVING
COUNT(*)>3;

CARDNO

101

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 13


3.
1. Delete a book in BOOK table. Update the contents of other tables to reflect this data
manipulation operation.
DELETE FROM BOOK
WHERE BOOKID=3;

BOOKID TITLE PUBYEAR PNAME


1 DBMS Jan-17 MCGRAW-HILL
2 ADBMS Jun-16 MCGRAW-HILL
4 ALGORITHMS Sep-15 MIT
5 OS May-16 PEARSON

4.Create a view of all books and its number of copies that are currently available in the Library.
CREATE VIEW BOOKSAVAILABLE AS

SELECT B.BOOKID, B.TITLE, C.NOOFCOPIES

FROM BOOK B, BOOKCOPIES C, LIBRARYBRANCH L

WHERE B.BOOKID=C.BOOKID AND C.BRANCHID=L.BRANCHID;

View Created

Select * from BOOKSAVAILABLE

BOOKID TITLE NOOFCOPIES


1 DBMS 10
1 DBMS 5
2 ADBMS 2
2 ADBMS 5
5 OS 1
4 ALGORITHMS 3

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 14


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

SELECT BOOKID, TITLE, PNAME, PUBYEAR


FROM BOOK
GROUP BY PUBYEAR, BOOKID, TITLE, PNAME;

BOOK_ID TITLE PUB_YEAR PUBLISHER_NAME


1 DBMS Jan-2017 MCGRAW-HILL
2 ADBMS Jun-2017 MCGRAW-HILL
3 CD Sep-2016 PEARSON
4 ALGORITHMS Sep-2015 MIT
5 OS May-2016 PEARSON

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 15


2. Consider the following schema for Order Database:
SALESMAN (Salesmanid, Name, City, Commission)
CUSTOMER (Customerid, Cust_Name, City, Grade, Salesman_id)
ORDERS (OrdNo, Purchase_Amt, Ord_Date, Customerid, Salesmanid)

Write SQL queries to


1. Count the customers with grades above Bangalore’s average.
2. Find the name and numbers of all salesmen who had more than one customer.
3. List all salesmen 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.

Schema Diagram

Table Creation

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 16


CREATE TABLE SALESMAN (

SALESMANID NUMBER (4) PRIMARY KEY,

NAME VARCHAR (20),

CITY VARCHAR (20),

COMMISSION VARCHAR (20));

DESC SALESMAN;

Name Null? Type

SALESMANID NOT NULL NUMBER(4)

NAME VARCHAR(20)

CITY VARCHAR(20)

COMMISSION NUMBER(3)

CREATE TABLE CUSTOMER1


(CUSTOMERID NUMBER (4) PRIMARY KEY,
CUSTNAME VARCHAR(20),
CITY VARCHAR (20),
GRADE NUMBER (3),
SALESMAN_ID REFERENCES SALESMAN (SALESMAN_ID) ON
DELETE CASCADE);

DESC CUSTOMER1;

Name Null? Type

CUSTOMERID NOT NULL NUMBER(4)

CUSTNAME VARCHAR(20)

CITY VARCHAR(20)

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 17


GRADE NUMBER(3)

SALESMANID NUMBER(4)

CREATE TABLE ORDERS (


ORDNO NUMBER (5) PRIMARY KEY,
PURCHASEAMT NUMBER (10, 2),
ORDDATE DATE,
CUSTOMERID REFERENCES CUSTOMER1 (CUSTOMER_ID) ON DELETE CASCADE,
SALESMANID REFERENCES SALESMAN (SALESMAN_ID) ON DELETE CASCADE);

DESC ORDERS;

Name Null? Type

ORDNO NOT NULL NUMBER(5)

PURCHASEsAMT NUMBER(10,2)

ORDDATE DATE

CUSTOMERID NUMBER(4)

SALESMANID NUMBER(4)

Insertion of Values to Tables

INSERT INTO SALESMAN VALUES(101,'RICHARD','LOS ANGELES','18%');


INSERT INTO SALESMAN VALUES(103,''GEORGE','NEWYORK','32%');
INSERT INTO SALESMAN VALUES(110,'CHARLES','BANGALORE','54%');
INSERT INTO SALESMAN VALUES(122,'ROWLING','PHILADELPHIA','46%');
INSERT INTO SALESMAN VALUES(126,'KURT','CHICAGO','52%');
INSERT INTO SALESMAN VALUES(132,'EDWIN','PHOENIX','41%');

SELECT * FROM SALESMAN;

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 18


SALESMANID NAME CITY COMMISSION
101 RICHARD LOS ANGELES 18%
1000 GEORGE NEWYORK 32%
110 CHARLES HOUSTON 54%
122 ROWLING PHILADELPHIA 46%
126 KURT CHICAGO 52%
132 EDWIN PHOENIX 41%

INSERT INTO CUSTOMER1 VALUES(501,'SMITH','LOS ANGELES',10,103);


INSERT INTO CUSTOMER1 VALUES(510,'BROWN','ATLANTA',14,122);
INSERT INTO CUSTOMER1 VALUES(522,'LEWIS','BANGALORE',10,132);
INSERT INTO CUSTOMER1 VALUES(534,'PHILIPS','BOSTON',17,103);
INSERT INTO CUSTOMER1 VALUES(543,'EDWARD','BANGALORE',14,110);
INSERT INTO CUSTOMER1 VALUES(550,'PARKER','ATLANTA',19,126);

SELECT * FROM CUSTOMER1

CUSTOMERID CUSTNAME CITY GRADE SALESMANID


501 SMITH LOS ANGELES 10 103
510 BROWN ATLANTA 14 122
522 LEWIS BANGALORE 10 132
534 PHILIPS BOSTON 17 103
543 EDWARD BANGALORE 14 110
550 PARKER ATLANTA 19 126

INSERT INTO ORDERS VALUES(001,1000,'05-APR-08',501,103);

INSERT INTO ORDERS VALUES(002,4000,'12-JUL-03',522,132);

INSERT INTO ORDERS VALUES(003,2500,'04-JUL-06',550,126);

INSERT INTO ORDERS VALUES(005,6000,'09-MAY-70',522,103);

INSERT INTO ORDERS VALUES(006,7000,'30-AUG-98',550,126);

INSERT INTO ORDERS VALUES (007,3400,'20-FEB-17',501,122);

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 19


SELECT * FROM ORDERS;

ORDNO PURCHASEAMT ORDDATE CUSTOMERID SALESMANID


1 1000 05-APR-08 501 1000
2 4000 12-JUL-03 522 132
3 2500 04-JUL-06 550 126
5 6000 09-MAY-70 522 103
6 7000 30-AUG-98 550 126
7 3400 20-FEB-17 501 122
8 3500 20-FEB-17 501 1000

Queries:

1. Count the customers with grades above Bangalore’s average.

SELECT GRADE,COUNT (DISTINCT CUSTOMERID)


FROM CUSTOMER
GROUP BY GRADE
HAVING GRADE > (SELECT AVG(GRADE)
FROM CUSTOMER
WHERE CITY='BANGALORE');

GRADE COUNT(DISTINCT CUSTOMERID)


14 2
17 1
19 1

2. Find the name and numbers of all salesmen who had more than one customer.

SELECT NAME, COUNT(CUSTOMERID)


FROM SALESMAN S, CUSTOMER C
WHERE S.SALESMANID=C.SALESMANID
GROUP BY NAME
HAVING COUNT(CUSTOMERID)>1;

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 20


SALESMANID NAME
------------------- -----------
1000 GEORGE

3. List all salesmen and indicate those who have and don’t have customers in their cities
(Use UNION operation.)

(SELECT NAME FROM SALESMAN S, CUSTOMER C


WHERE S.SALESMANID=C.SALESMANID AND S.CITY=C.CITY)
UNION
(SELECT NAME FROM SALESMAN
WHERE SALESMANID NOT IN(SELECT S1.SALESMANID
FROM SALESMAN S1, CUSTOMER C1
WHERE S1.SALESMANID=C1.SALESMANID AND
S1.CITY=C1.CITY));
NAME
ROWLING
RICHARD
SMITH
KURT
GEORGE
EDWIN
CHARLES
4) Create a view that finds the salesman who has the customer with the highest order of a
day.

CREATE VIEW ELITSALESMAN AS


SELECT B.ORDDATE, A.SALESMANID, A.NAME
FROM SALESMAN A, ORDERS B
WHERE A.SALESMANID = B.SALESMANID
AND B.PURCHASEAMT = (SELECT MAX (PURCHASEAMT)
FROM ORDERS C
WHERE C.ORDDATE = B.ORDDATE);

SELECT * FROM ELITSALESMAN;

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 21


ORD_DATE SALESMAN_ID NAME
05-APR-08 1000 GEORGE
12-JUL-03 132 EDWIN
04-JUL-06 126 KURT
09-MAY-70 1000 GEORGE
30-AUG-98 126 KURT
20-FEB-17 1000 GEORGE

5) Demonstrate the DELETE operation by removing salesman with id 1000. All his orders
must also be deleted.
Use ON DELETE CASCADE at the end of foreign key definitions while creating child table orders
and then execute the following:
Use ON DELETE SET NULL at the end of foreign key definitions while creating child table
customers and then executes the following:

DELETE FROM SALESMAN


WHERE SALESMANID=1000;

SALESMAN_ID NAME CITY COMMISSION


------------------- -------- ------- -------------------
101 RICHARD LOS ANGELES 18%
110 CHARLES HOUSTON 54%
122 ROWLING PHILADELPHIA 46%
126 KURT CHICAGO 52%
132 EDWIN PHOENIX 41%

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 22


3) Consider the schema for Movie Database:
ACTOR (Actid, ActName, ActGender)
DIRECTOR (Dirid, DirName, DirPhone)
MOVIES (Movid, MovTitle, Mov_Year, MovLang, Dirid)
MOVIE_CAST (Actid,Movid, Role)
RATING (Movid, RevStars)

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.

Schema Diagram

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 23


Table Creation

CREATE TABLE ACTOR (

ACTID NUMBER (3),

ACTNAME VARCHAR (20),

ACTGENDER CHAR (1),

PRIMARY KEY (ACTID));

DESC ACTOR;

Name Null? Type


ACTID NOT NULL NUMBER(3)
ACTNAME VARCHAR(20)
ACTGENDER CHAR(1)

CREATE TABLE DIRECTOR (

DIRID NUMBER (3),

DIRNAME VARCHAR (20),

DIRPHONE NUMBER (10),

PRIMARY KEY (DIRID));

DESC DIRECTOR;

Name Null? Type


DIRID NOT NULL NUMBER(3)
DIRNAME VARCHAR(20)
DIRPHONE NUMBER(10)

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 24


CREATE TABLE MOVIES (

MOVID NUMBER(4) PRIMARY KEY,

MOVTITLE VARCHAR(25),

MOVYEAR NUMBER(4),

MOVLANG VARCHAR(12),

DIRID NUMBER(3),REFERENCES DIRECTOR(DIR_ID));

DESC MOVIES;

Name Null? Type


MOVID NOT NULL NUMBER(4)
MOVTITLE VARCHAR(25)
MOVYEAR NUMBER(4)
MOVLANG VARCHAR(12)
DIRID NUMBER(3)

CREATE TABLE MOVIECAST (


ACTID REFERENCES ACTOR (ACTID),
MOVID REFERENCES MOVIES (MOVID),
ROLE VARCHAR (10));
DESC MOVIE_CAST

Name Null? Type


ACTID NOT NULL NUMBER(3)
MOVID NOT NULL NUMBER(4)
ROLE VARCHAR(10)

CREATE TABLE RATING (

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 25


MOVID NUMBER (4) REFERENCES MOVIES (MOV_ID),

REVSTARS VARCHAR (25));

DESC RATING;

Name Null? Type


MOVID NUMBER(4)
REVSTARS VARCHAR(25)

Insertion of Values to Tables

INSERT INTO ACTOR VALUES (001,’MADHURI DIXIT’,’F’);

INSERT INTO ACTOR VALUES (002,’AAMIR KHAN’,’M’);

INSERT INTO ACTOR VALUES (003,’JUHI CHAWLA’,’F’);

INSERT INTO ACTOR VALUES (004,’SRIDEVI’,’F’);

SELECT * FROM ACTOR

ACTID ACTNAME ACT

1 MADHURI DIXIT F

2 AAMIR KHAN M

3 JUHI CHAWLA F

4 SRIDEVI F

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 26


INSERT INTO DIRECTOR VALUES (100,’SUBHASH KAPOOR’, 56340015);
INSERT INTO DIRECTOR VALUES(102,'ALAN TAYLOR',719600310);
INSERT INTO DIRECTOR VALUES (103,’SANTHOSH ANANDDRAM’, 99346111);
INSERT INTO DIRECTOR VALUES (104,’IMTIAZ ALI’, 85399209);
INSERT INTO DIRECTOR VALUES (105,'HITCHCOCK',7766138911);
INSERT INTO DIRECTOR VALUES (106,'STEVEN SPIELBERG',9966138934);

SELECT * FROM DIRECTOR;

DIRID DIRNAME DIRPHONE

100 SUBHASH KAPOOR 56340015

102 ALAN TAYLOR 719600310

103 SANTHOSH ANANDDRAM 99346111

104 IMTIAZ ALI 85399209

105 HITCHCOCK 7766138911

106 STEVEN SPIELBERG 9966138934

INSERT INTO MOVIES VALUES (501,’JAB HARRY MET SEJAL’, 2017, ‘HINDI’, 104); INSERT
INTO MOVIES VALUES (502,’RAJAKUMARA’, 2017, ‘KANNADA’, 103);
INSERT INTO MOVIES VALUES (503,’JOLLY LLB 2’, 2013, ‘HINDI’, 100);
INSERT INTO MOVIES VALUES (504,’TERMINATOR GENESYS, 2015, ‘ENGLISH’, 102);
INSERT INTO MOVIES VALUES (505,’JAWS, 1975, ‘ENGLISH’, 106);
INSERT INTO MOVIES VALUES (506,’BRIDGE OF SPIES’,2015,’ENGLISH’, 106);
INSERT INTO MOVIES VALUES (507,’VERTIGO, 1943, ‘ENGLISH’, 105);
INSERT INTO MOVIES VALUES (508,’SHADOW OF A DOUBT, 1943, ‘ENGLISH’, 105);

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 27


SELECT * FROM MOVIES

MOVID MOVTITLE MOVYEAR MOVLANG DIRID

501 JAB HARRY MET SEJAL 2017 HINDI 104

502 RAJAKUMARA 2017 KANNADA 103

503 JOLLY LLB 2 2013 HINDI 100

504 TERMINATOR GENESYS 2015 ENGLISH 102

505 JAWS 1975 ENGLISH 106

506 BRIDGE OF SPIES 2015 ENGLISH 106

507 VERTIGO 1958 ENGLISH 105

508 SHADOW OF A DOUBT 1943 ENGLISH 105

INSERT INTO MOVIECAST VALUES (001, 501, ‘HEROINE’);

INSERT INTO MOVIECAST VALUES (001, 502, ‘HEROINE’);

INSERT INTO MOVIECAST VALUES (003, 503, ‘COMEDIAN’);

INSERT INTO MOVIECAST VALUES (004, 504, ‘GUEST’);

INSERT INTO MOVIECAST VALUES (004, 501, ‘HERO’);

SELECT * FROM MOVIE_CAST

ACT_ID MOV_ID ROLE

1 501 HEROINE

1 502 HEROINE

3 503 COMEDIAN

4 504 GUEST

4 501 HERO

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 28


INSERT INTO RATING VALUES (501, 4);
INSERT INTO RATING VALUES (502, 2);
INSERT INTO RATING VALUES (503, 5);
INSERT INTO RATING VALUES (504, 4);
INSERT INTO RATING VALUES (505, 3);
INSERT INTO RATING VALUES (506, 2);

SELECT * FROM RATING

MOVID REVSTARS

501 4

502 2

503 5

504 4

505 3

506 2

507 2

508 4

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

SELECT MOVTITLE

FROM MOVIES

WHERE DIRID IN ( SELECT DIRID

FROM DIRECTOR

WHERE DIRNAME='HITCHCOCK');

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 29


MOV_TITLE
----------------
VERTIGO
SHADOW OF A DOUBT

2. Find the movie names where one or more actors acted in two or more movies.

SELECT MOVTITLE FROM MOVIES M, MOVIECAST MC


WHERE M.MOVID=MC.MOVID AND MC.ACTID IN (SELECT ACTID
FROM MOVIECAST GROUP BY ACT_ID
HAVING COUNT(MOVID)>=2);
MOV_TITLE
--------------------
JAB HARRY MET SEJAL

3. List all actors who acted in a movie before 2000 and also in a movie after 2015 (use JOIN
operation).
SELECT ACTNAME, MOVTITLE, MOVYEAR

FROM ACTOR A JOIN MOVIECAST C ON A.ACTID=C.ACTID

JOIN MOVIES M ON C.MOVID=M.MOVID

WHERE M.MOVYEAR NOT BETWEEN 2000 AND 2015;

OR
SELECT A.ACTNAME, A.ACTNAME, C.MOVTITLE, C.MOVYEAR FROM

ACTOR A, MOVIECAST B, MOVIES C

WHERE A.ACTID=B.ACTIDAND B.MOVID=C.MOVID

AND C.MOVYEAR NOT BETWEEN 2000 AND 2015;

ACTNAME MOVTITLE MOVYEAR


-----------------------------------------------------------------------------------------------------------
MADHURI DIXIT JAB HARRY MET SEGAL 2017
SRIDEVI JAB HARRY MET SEGAL 2017
MADHURI DIXIT RAJAKUMARA 2017

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 30


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 MOVTITLE, MAX (REVSTARS)


FROM MOVIES INNER JOIN RATING USING (MOVID)
GROUP BY MOVTITLE
HAVING MAX (REVSTARS)>0
ORDER BY MOVTITLE;

MOVTITLE MAX(REVSTARS)
---------------------------------------------------------------------------------
BRIDGE OF SPIES 5
JAB HARRY MET SEJAL 4
JAWS 5
JOLLY LLB2 5
RAJAKUMARA 2
SHADOW OF A DOUBT 4
TERMINATOR GENESYS 4
VERTIGO 2

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

UPDATE RATING
SET REVSTARS=5
WHERE MOVID IN (SELECT MOVID
FROM MOVIES
WHERE DIRID IN (SELECT DIRID
FROM DIRECTOR

WHERE DIRNAME='STEVEN SPIELBERG'));

1 ROWS UPDATED

SELECT * FROM RATING

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 31


MOV_ID REV_STARS

501 4

502 2

503 5

504 4

505 5

506 5

507 2

508 4

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 32


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 ‘1GA15CS101’ 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 stude
Schema Diagram

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 33


Table Creation

STUDENT
CREATE TABLE STUDENT
(USN VARCHAR(10) PRIMARY KEY,
SNAME VARCHAR(25),
ADDRESS VARCHAR(25),
PHONE VARCHAR(10),
GENDER CHAR(1));
Table created.
SEMSEC
CREATE TABLE SEMSEC
SSID VARCHAR(5) PRIMARY KEY,
SEM NUMBER(2),
SEC CHAR(1));
Table created.
CLASS
CREATE TABLE CLASS
(USN REFERENCES STUDENT(USN),
SSID REFERENCES SEMSEC(SSID));
Table created.
SUBJECT
CREATE TABLE SUBJECT
(SUBCODE VARCHAR(8) PRIMARY KEY,

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 34


TITLE VARCHAR(20),
SEM NUMBER(2), CREDITS NUMBER(2));
Table created.
IAMARKS
CREATE TABLE IAMARKS
(USN REFERENCES STUDENT(USN),
SUBCODE REFERENCES SUBJECT(SUBCODE),
SSID REFERENCES SEMSEC(SSID),
TEST1 NUMBER(2),
TEST2 NUMBER(2),

TEST3 NUMBER(2),
FINALIA NUMBER(3));
Table created.
Values for tables:
STUDENT:
select * from student;
USN SNAME ADDRESS PHONE G
---------- ------------------------- ------------- ------------ - ---
1cg15cs001 Abhi tumkur 9875698410 M
1cg15cs002 amulya gubbi 8896557412 F
1cg16me063 chethan nittur 7894759522 M
1cg14ec055 raghavi sspuram 9485675521 F
1cg15ee065 sanjay bangalore 9538444404 M
SEMSEC:
select * from semsec;
SSID SEM S
----- --------- -
5A 5 A
3B 3 B

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 35


7A 7 A
2C 2 C
4B 4 B
4c 4 c

CLASS:
select * from class;
USN SSID
---------- -----
1cg15cs001 5A

1cg15cs002 5A
1cg16me063 3B
1cg14ec055 7A
1cg15ee065 3B
1cg15ee065 4c
1cg15cs002 4c
SUBJECT:
select * from subject;
SUBCODE TITLE SEM CREDITS
-------- -------------------- --------- ---------
15cs53 dbms 5 4
15cs33 ds 3 4
15cs34 co 3 4
15csl58 dba 52
10cs71 oomd 7 4
IAMARKS:
select * from iamarks;
USN SUBCODE SSID TEST1 TEST2 TEST3 FINALIA
---------- -------- ----- --------- --------- --------- ---------

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 36


1cg15cs001 15cs53 5A 18 19 15 19
1cg15cs002 15cs53 5A 15 16 14 16
1cg16me063 15cs33 3B 10 15 16 16
1cg14ec055 10cs71 7A 18 20 21 21
1cg15ee065 15cs33 3B 16 20 17 19
1cg15ee065 15cs53 4c 19 20 18 20
Queries:
1. List all the student details studying in fourth semester ‘C’ section.
select
s.usn,sname,address,phone,gender from
student s, class c, semsec ss where
sem=4 and
sec='c' and
ss.ssid=c.ssid and
c.usn=s.usn;

USN SNAME ADDRESS PHONE G


---------- -------------- ---------------- ------------ -----
1cg15ee065 Sanjay bangalore 9538444404 M
1cg15cs002 Amulya gubbi 8896557412 F
2. Compute the total number of male and female students in each semester and in
each section.
SELECT SEM,SEC,GENDER,COUNT(*)
FROM STUDENT S, SEMSEC SS,CLASS C
WHERE S.USN=C.USN AND
C.SSID=SS.SSID
GROUP BY SEM,SEC,GENDER
ORDER BY SEM;

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 37


SEM S G COUNT(*)
---- - - ---------
3BM2
4cF1
4cM1
5AF1
5AM1
7AF1

3. Create a view of Test1 marks of student USN ‘1BI15CS101’ in all subjects.


CREATE VIEW TEST1 AS
SELECT SUBCODE,TEST1
FROM IAMARKS
WHERE USN='1cg15ee065';
View created.
SQL> select * from test1;
SUBCODE TEST1
-------- ---------
15cs33 16
15cs53 19
4. Calculate the FinalIA (average of best two test marks) and update the
corresponding table for all students.
CREATE OR REPLACE PROCEDURE AVG
IS
CURSOR C_IAMARKS IS
SELECT GREATEST(TEST1,TEST2) AS A,GREATEST(TEST1,TEST3) AS B,

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 38


GREATEST(TEST3,TEST2) AS C
FROM IAMARKS
WHERE FINALIA IS NULL
FOR UPDATE;
C_A NUMBER;
C_B NUMBER;
C_C NUMBER;
C_SM NUMBER;
C_AV NUMBER;
BEGIN
OPEN C_IAMARKS;
LOOP
FETCH C_IAMARKS INTO C_A,C_B,C_C;
EXIT WHEN C_IAMARKS%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(C_A||' '||C_B||' '||C_C);
IF(C_A!=C_B) THEN
C_SM:=C_A+C_B;
ELSE
C_SM:=C_A+C_C;
END IF;
C_AV:=C_SM/2;
DBMS_OUTPUT.PUT_LINE('SUM='||C_SM);
DBMS_OUTPUT.PUT_LINE('AVERAGE='||C_AV);
UPDATE IAMARKS
SET FINALIA=C_AV
WHERE CURRENT OF C_IAMARKS;
END LOOP;

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 39


CLOSE C_IAMARKS;
END AVG;
Procedure created.
SQL> BEGIN
2 AVG;
3 END;
PL/SQL procedure successfully completed.
SQL> SELECT * FROM IAMARKS;
USN SUBCODE SSID TEST1 TEST2 TEST3 FINALIA
---------- -------- ----- --------- --------- --------- -----
1cg15cs001 15cs53 5A 18 19 15 19
1cg15cs002 15cs53 5A 15 16 14 16
1cg16me063 15cs33 3B 10 15 16 16
1cg14ec055 10cs71 7A 18 20 21 21
1cg15ee065 15cs33 3B 16 20 17 19
1cg15ee065 15cs53 4c 19 20 18 20
6 rows selected.

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.
SELECT S.USN,S.SNAME,S.ADDRESS,S.PHONE,S.GENDER,
CASE WHEN IA.FINALIA BETWEEN 17 AND 20 THEN 'OUTSTANDING'
WHEN IA.FINALIA BETWEEN 12 AND 16 THEN 'AVERAGE'
ELSE 'WEAK'

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 40


END AS CAT
FROM STUDENT S,SEMSEC SS,IAMARKS IA,SUBJECT SUB
WHERE S.USN=IA.USN AND
SS.SSID=IA.SSID AND
SUB.SUBCODE=IA.SUBCODE AND
SUB.SEM=7
USN SNAME ADDRESS PHONE G CAT
---------- ----------- ------------ ---------- - -----
1cg14ec055 raghavi sspuram 9485675521 F WEAK

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 ‘KUMAR’, 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 5 (use NOT EXISTS operator).

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 41


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.

Schema Diagram

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 42


Table Creation

CREATE TABLE DEPARTMENT


(DNO VARCHAR2 (20) PRIMARY KEY,
DNAME VARCHAR2 (20),
MGRSTARTDATE DATE);

CREATE TABLE EMPLOYEE


(SSN VARCHAR2 (20) PRIMARY KEY,
FNAME VARCHAR (20),
LNAME VARCHAR (20),
ADDRESS VARCHAR (20),
SEX CHAR (1),
SALARY INTEGER,
SUPERSSN REFERENCES EMPLOYEE (SSN),
DNO REFERENCES DEPARTMENT (DNO));

NOTE: Once DEPARTMENT and EMPLOYEE tables are created we must alter
Department table to add foreign constraint MGRSSN using sql command.

ALTER TABLE DEPARTMENT


ADD MGRSSN REFERENCES EMPLOYEE (SSN);

CREATE TABLE DLOCATION


(DLOC VARCHAR2 (20),
DNO REFERENCES DEPARTMENT(DNO), PRIMARY

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 43


KEY (DNO, DLOC));

CREATE TABLE PROJECT


(PNO INTEGER PRIMARY KEY,
PNAME VARCHAR2 (20),
PLOCATION VARCHAR2 (20),
DNO REFERENCES DEPARTMENT (DNO));

CREATE TABLE WORKS_ON


(HOURS NUMBER (2),
SSN REFERENCES EMPLOYEE (SSN),
PNO REFERENCES PROJECT(PNO),
PRIMARY KEY (SSN, PNO));

Insertion of values to tables

INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‘GATECE01’,’MOHAN’,’KUMAR’,’BANGALORE’,’M’, 450000);

INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‘GATCSE01’,’JAGAN’,’RAO’,’BANGALORE’,’M’, 500000);

SELECT * FROM DEPARTMENT;

DNO DNAME MGRSTARTDATE MGRSSN


---------------------------------------------------------------------
1 ACCOUNTS 01-JAN-17 GATACC02
2 IT 01-AUG-16 GATIT01
3 ECE 01-JUN-08 GATECE01
4 ISE 01-AUG-15 GATISE01
5 CSE 01-JUN-02 GATCSE05

Note: update entries of employee table to fill missing fields SUPERSSN and DNO

UPDATE EMPLOYEE
SETSUPERSSN=NULL, DNO=’3’
WHERE SSN=’GATECE01’;

UPDATE EMPLOYEE
SETSUPERSSN=’GATCSE02’, DNO=’5’
WHERE SSN=’GATCSE01’;

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 44


UPDATE EMPLOYEE
SETSUPERSSN=’GATCSE03’, DNO=’5’
WHERE SSN=’GATCSE02’;

UPDATE EMPLOYEE
SETSUPERSSN=’GATCSE04’, DNO=’5’
WHERE SSN=’GATCSE03’;

UPDATE EMPLOYEE
SETDNO=’5’, SUPERSSN=’GATCSE05’
WHERE SSN=’GATCSE04’;

UPDATE EMPLOYEE
SETDNO=’5’, SUPERSSN=’GATCSE06’
WHERE SSN=’GATCSE05

UPDATE EMPLOYEE
SETDNO=’5’, SUPERSSN=NULL
WHERE SSN=’GATCSE06’;

UPDATE EMPLOYEE
SETDNO=’1’, SUPERSSN=’GATACC02’
WHERE SSN=’GATACC01’;

UPDATE EMPLOYEE
SETDNO=’1’, SUPERSSN=NULL
WHERE SSN=’GATACC02’;

UPDATE EMPLOYEE
SETDNO=’4’, SUPERSSN=NULL
WHERE SSN=’GATISE01’;

UPDATE EMPLOYEE
SETDNO=’2’, SUPERSSN=NULL
WHERE SSN=’GATIT01’;

SELECT * FROM EMPLOYEE;

SSN FNAME LNAME ADDRESS SEX SALARY SUPERSSN DNO


--------------------------------------------------------------------------------------------------------------------
GATECE01 MOHAN KUMAR BANGALORE M 450000 3
GATCSE01 JAGAN RAO BANGALORE M 500000 GATCSE02 5
GATCSE02 HEMANTH BHATT BANGALORE M 700000 GATCSE03 5
GATCSE03 ESHWAR KUMAR MYSORE M 500000 GATCSE04 5
GATCSE04 PAVAN HEGDE MANGALORE M 650000 GATCSE05 5

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 45


GATCSE05 GIRISH MALYA MYSORE M 450000 GATCSE06 5
GATCSE06 NEHA SN BANGALORE F 800000 5
GATACC01 AHANA K MANGALORE F 350000 1
GATACC02 SANTOSH KUMAR MANGALORE M 300000 1
GATISE01 VEENA M BANGALORE F 600000 4
GATIT01 NAGESH HR BANGALORE M 500000 2

INSERT INTO DLOCATION VALUES (’BANGALORE’, ‘1’);


INSERT INTO DLOCATION VALUES (’BANGALORE’, ‘2’);
SELECT * FROM DLOCATION;
DLOC DNO
---------------------------------------
BANGALORE 1
BANGALORE 2
BANGALORE 3
MANGALORE 4
MANGALORE 5

INSERT INTO PROJECT VALUES (100,’IOT’,’BANGALORE’,’5’); INSERT


INTO PROJECT VALUES (101,’CLOUD’,’BANGALORE’,’5’);
SELECT * FROM PROJECT;

PNO PNAME PLOCATION DNO


-----------------------------------------------------------------------------------
100 IOT BANGALORE 5
101 CLOUD BANGALORE 5
102 BIGDATA BANGALORE 5
103 SENSORS BANGALORE 3
104 BANK MANAGEMENT BANGALORE 1
105 SALARY MANAGEMENT BANGALORE 1
106 OPEN STACK BANGALORE 4
107 SMART CITY BANGALORE 2

INSERT INTO WORKS_ON VALUES (4, ‘GATCSE01’, 100);


INSERT INTO WORKS_ON VALUES (6, ‘GATCSE01’, 101);

SELECT * FROM WORKS_ON;

HOURS SSN PNO


-------------------------------------------

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 46


4 GATCSE01 100
6 GATCSE01 101
8 GATCSE01 102
10 GATCSE02 100
3 GATCSE04 100
4 GATCSE05 101
5 GATCSE06 102
6 GATCSE03 102
7 GATECE01 103
5 GATACC01 104
6 GATACC02 105
4 GATISE01 106
10 GATIT01 107

Queries:

1. Make a list of all project numbers for projects that involve an employee whose last name is
‘KUMAR’, either as a worker or as a manager of the department that controls the project.

(SELECT DISTINCT P.PNO


FROM PROJECT P, DEPARTMENT D, EMPLOYEE E
WHERE P.DNO=D.DNO AND D.MGRSSN=E.SSN AND E.LNAME=’KUMAR’)
UNION
(SELECT DISTINCT P1.PNO
FROM PROJECT P1, WORKS_ON W, EMPLOYEE E1
WHERE P1.PNO=W.PNO AND E1.SSN=W.SSN AND E1.LNAME=’KUMAR’);

PNO
-----------
102
103
104
105

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

SELECT E.FNAME, E.LNAME, 1.1*E.SALARY AS INCR_SAL


FROM EMPLOYEE E, WORKS_ON W, PROJECT P

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 47


WHERE E.SSN=W.SSN AND W.PNO=P.PNO AND P.PNAME=’IOT’;

FNAME LANME INCR_SAL


-------------------------------------------------------------------------------
JAGAN RAO 550000
HEMANTH BHATT 770000
PAVAN HEGDE 715000

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

SELECT SUM (E.SALARY), MAX (E.SALARY), MIN (E.SALARY), AVG (E.SALARY)


FROM EMPLOYEE E, DEPARTMENT D
WHERE E.DNO=D.DNO AND D.DNAME=’ACCOUNTS’;
SUM MAX MIN AVG
---------------------------------------------------------------
650000 350000 300000 325000

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 PNO
FROM PROJECT
WHERE DNO=’5’)
MINUS (SELECT PNO
FROM WORKS_ON
WHERE E.SSN=SSN));

FNAME LNAME
------------------------------------
JAGAN RAO

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

SELECT D.DNO, COUNT (*)


FROM DEPARTMENT D, EMPLOYEE E

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 48


WHERE D.DNO=E.DNO AND E.SALARY>600000 AND D.DNO IN (SELECT E1.DNO
FROM EMPLOYEE E1
GROUP BY E1.DNO
HAVING COUNT (*)>5)
GROUP BY D.DNO;

DNO COUNT(*)
---------------------------------------
5 3

Viva Questions
1. What is SQL?
Structured Query Language
2. What is database?
A database is a logically coherent collection of data with some inherent meaning, representing some
aspect of real world and which is designed, built and populated with data for a specific purpose.

3. What is DBMS?
It is a collection of programs that enables user to create and maintain a database. In other words it is
general-purpose software that provides the users with the processes of defining, constructing and
manipulating the database for various applications.

4. What is a Database system?


The database and DBMS software together is called as Database system.

5. Advantages of DBMS?

o Redundancy is controlled.

o Unauthorized access is restricted.

o Providing multiple user interfaces.

o Enforcing integrity constraints.

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 49


o Providing backup and recovery.

6. Disadvantage in File Processing System?


o Data redundancy & inconsistency.

o Difficult in accessing data.

o Data isolation.

o Data integrity.

o Concurrent access is not possible.


o Security Problems.

7. Describe the three levels of data abstraction?


There are three levels of abstraction:
 Physical level: The lowest level of abstraction describes how data are stored.
 Logical level:The next higher level of abstraction, describes what data are stored in database and
what relationship among those data.
 View level:The highest level of abstraction describes only part of entire database.

8. Define the "integrity rules"


There are two Integrity rules.
o Entity Integrity:States that “Primary key cannot have NULL value”
o Referential Integrity:States that “Foreign Key can be either a NULL value or should be
Primary Key value of other relation.
9. What is extension and intension?
Extension - It is the number of tuples present in a table at any instance. This is time dependent.
Intension -It is a constant value that gives the name, structure of table and the constraints laid on it.

10. What is Data Independence?


Data independence means that “the application is independent of the storage structure and access
strategy of data”. In other words, The ability to modify the schema definition in one level should not
affect the schema definition in the next higher level.

Two types of Data Independence:

 Physical Data Independence: Modification in physical level should not affect the logical level.

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 50


 Logical Data Independence: Modification in logical level should affect the view level.

NOTE: Logical Data Independence is more difficult to achieve

11. What is a view? How it is related to data independence?


A view may be thought of as a virtual table, that is, a table that does not really exist in its own right but
is instead derived from one or more underlying base table. In other words, there is no stored file that
direct represents the view instead a definition of view is stored in data dictionary.

Growth and restructuring of base tables is not reflected in views. Thus the view can insulate users
from the effects of restructuring and growth in the database. Hence accounts for logical data
independence.

12. What is Data Model?


A collection of conceptual tools for describing data, data relationships data semantics and constraints.

13. What is E-R model?


This data model is based on real world that consists of basic objects called entities and of relationship
among these objects. Entities are described in a database by a set of attributes.

14. What is Object Oriented model?


This model is based on collection of objects. An object contains values stored in instance variables
within the object. An object also contains bodies of code that operate on the object. These bodies of
code are called methods. Objects that contain same types of values and the same methods are grouped
together into classes.

15. What is an Entity?


It is an 'object' in the real world with an independent existence.

16. What is an Entity type?


It is a collection (set) of entities that have same attributes.

17. What is an Entity set?


It is a collection of all entities of particular entity type in the database.

18. What is an Extension of entity type?

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 51


The collections of entities of a particular entity type are grouped together into an entity set.

19. What is an attribute?

It is a particular property, which describes the entity.

20. What is a Relation Schema and a Relation?


A relation Schema denoted by R(A1, A2, …, An) is made up of the relation

R and the list of attributes Ai that it contains. A relation is defined as

be the relation which contains set tuples (t1, t2, t3, ...,tn). Each tuple

values t=(v1,v2, ..., vn).

21. What is degree of a Relation?

It is the number of attribute of its relation schema.

22. What is Relationship?

It is an association among two or more entities.

23. What is Relationship set?


The collection (or set) of similar relationships.
24. What is Relationship type?
Relationship type defines a set of associations or a relationship set among a given set of entity types.
25. What is degree of Relationship type?
It is the number of entity type participating.
26. What is DDL (Data Definition Language)?
A data base schema is specified by a set of definitions expressed by a special language called DDL.
27. What is VDL (View Definition Language)?
It specifies user views and their mappings to the conceptual schema.
28. What is SDL (Storage Definition Language)?
This language is to specify the internal schema. This language may specify the mapping between
two schemas.
29. What is Data Storage - Definition Language?

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 52


The storage structures and access methods used by database system are specified by aset of definition
in a special type of DDL called data storage-definition language.

30. What is DML (Data Manipulation Language)?


This language that enable user to access or manipulate data as organized by appropriate
Datamodel.Procedural DML or Low level: DML requires a user to specify what data are
needed and how to get those data. Non-Procedural DML or High level: DML requires a
user to specify what data are needed without specifying how to get those data.
31. What is DML Compiler?
It translates DML statements in a query language into low-level instruction that the query
evaluation engine can understand.
32. What is Relational Algebra?
It is a procedural query language. It consists of a set of operations that take one or two relations
asinput and produce a new relation.

33. What is Relational Calculus?


It is an applied predicate calculus specifically tailored for relational databases proposed by E.F.
Codd.
E.g. of languages based on it are DSL, ALPHA, QUEL.
34. What is normalization?
It is a process of analyzing the given relation schemas based on their Functional Dependencies
(FDs)and primary key to achieve the properties Minimizing redundancy, Minimizing insertion,
deletion and update anomalies.

35. What is Functional Dependency?


A Functional dependency is denoted by X Y between two sets of attributes X and Y that are subsets of
R specifies a constraint on the possible tuple that can form a relation state r of R. The constraint is for
any two tuples t1 and t2 in r if t1[X] = t2[X] then they have t1[Y] = t2[Y]. This means the value of X
component of a tuple uniquely determines the value of component Y.

36. When is a functional dependency F said to be minimal?

Every dependency in F has a single attribute for its right hand side.

We cannot replace any dependency X A in F with a dependency Y A where Y is a proper subset of X


and still have a set of dependency that is equivalent to F.

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 53


We cannot remove any dependency from F and still have set of dependency that is equivalent to F.

37. What is Multivalued dependency?

Multivalued dependency denoted by X Y specified on relation schema R, where X and Y are


bothsubsets of R, specifies the following constraint on any relation r of R: if two tuples t1 and t2 exist
in r such that t1[X] = t2[X] then t3 and t4 should also exist in r with the following properties

t3[x] = t4[X] = t1[X] = t2[X]


t3[Y] = t1[Y] and t4[Y] = t2[Y]
t3[Z] = t2[Z] and t4[Z] = t1[Z]
where [Z = (R-(X U Y)) ]
38. What is Lossless join property?
It guarantees that the spurious tuple generation does not occur with respect to relation
schemas after decomposition.
39. What is 1 NF (Normal Form)?
The domain of attribute must include only atomic (simple, indivisible) values.
40. What is Fully Functional dependency?
It is based on concept of full functional dependency. A functional dependency X Y is fully
functional dependency if removal of any attribute A from X means that the dependency
does not hold any more.

41. What is 2NF?


A relation schema R is in 2NF if it is in 1NF and every non-prime attribute A in R is fully
functionally dependent on primary key.
42. What is 3NF?
A relation schema R is in 3NF if it is in 2NF and for every FD X A either of the following
is true
X is a Super-key of R.
A is a prime attribute of R.
In other words, if every non prime attribute is non-transitively dependent on primary key.
43. What is BCNF (Boyce-Codd Normal Form)?
A relation schema R is in BCNF if it is in 3NF and satisfies additional constraints that for
every FD X A, X must be a candidate key.

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 54


44. What is 4NF?
A relation schema R is said to be in 4NF if for every Multivalued dependency X Y that
holds over R, one of following is true
X is subset or equal to (or) XY = R.
X is a super key.
45. What is 5NF?
A Relation schema R is said to be 5NF if for every join dependency {R1, R2, ...,Rn} that
holds R, one the following is true
Ri = R for some i.
The join dependency is implied by the set of FD, over R in which the left side is key of R.
46. What is Domain-Key Normal Form?
A relation is said to be in DKNF if all constraints and dependencies that should hold on the
constraint can be enforced by simply enforcing the domain constraint and key constraint on
the relation.
47. What are partial, alternate, artificial, compound and natural key?
Partial Key:
It is a set of attributes that can uniquely identify weak entities and that are related to same
owner entity. It is sometime called as Discriminator.
Alternate Key:
All Candidate Keys excluding the Primary Key are known as Alternate Keys.
ArtificialKey:
If no obvious key, either stand alone or compound is available, then the last resort is to
simply create a key, by assigning a unique number to each record or occurrence. Then this
is known as developing an artificial key.
CompoundKey:
If no single data element uniquely identifies occurrences within a construct, then combining
multiple elements to create a unique identifier for the construct is known as creating a compound key.
NaturalKey:
When one of the data elements stored within a construct is utilized as the primary key, then it is
called the natural key.
48. What is indexing and what are the different kinds of indexing?
Indexing is a technique for determining how quickly specific data can be found.
Binary search style indexing

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 55


B-Tree indexing
Inverted list indexing
Memory resident table
Table indexing
49. What is system catalog or catalog relation? How is better known as?
A RDBMS maintains a description of all the data that it contains, information about every
relation and index that it contains. This information is stored in a collection of relations
maintained by the system called metadata. It is also called data dictionary.
50. What is meant by query optimization?
The phase that identifies an efficient execution plan for evaluating a query that has the least
estimated cost is referred to as query optimization.
51. What is join dependency and inclusion dependency?
JoinDependency:
A Join dependency is generalization of Multivalued dependency.A JD {R1, R2, ...,Rn} is
said to hold over a relation R if R1, R2, R3, ..., Rn is a lossless-join decomposition of R .
There is no set of sound and complete inference rules for JD.
InclusionDependency:
An Inclusion Dependency is a statement of the form that some columns of a relation are
contained in other columns. A foreign key constraint is an example of inclusion dependency.
52. What is durability in DBMS?
Once the DBMS informs the user that a transaction has successfully completed, its effects
should persist even if the system crashes before all its changes are reflected on disk. This
property is called durability.
53. What do you mean by atomicity and aggregation?
Atomicity:
Either all actions are carried out or none are. Users should not have to worry about the effectof
incomplete transactions. DBMS ensures this by undoing the actions of incomplete
transactions.
Aggregation:
A concept which is used to model a relationship between a collection of entities and
relationships. It is used when we need to express a relationship among relationships.
54. What is a Phantom Deadlock?
In distributed deadlock detection, the delay in propagating local information might cause the

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 56


deadlock detection algorithms to identify deadlocks that do not really exist. Such situations
are called phantom deadlocks and they lead to unnecessary aborts.
55. What is a checkpoint and when does it occur?
A Checkpoint is like a snapshot of the DBMS state. By taking checkpoints, the DBMS can
reduce the amount of work to be done during restart in the event of subsequent crashes.
56. What are the different phases of transaction?
Different phases are
 Analysis phase
 Redo Phase
 Undo phase
57. What do you mean by flat file database?
It is a database in which there are no programs or user access languages. It has no cross-file
capabilities but is user-friendly and provides user-interface management.
58. What is "transparent DBMS"?
It is one, which keeps its Physical Structure hidden from user.
59. Brief theory of Network, Hierarchical schemas and their properties
Network schema uses a graph data structure to organize records example for such a
database management system is CTCG while a hierarchical schema uses a tree data
structure example for such a system is IMS.
60. What is a query?
A query with respect to DBMS relates to user commands that are used to interact with a
data base. The query language can be classified into data definition language and data
manipulation language.
61. What do you mean by Correlated subquery?
Subqueries, or nested queries, are used to bring back a set of rows to be used by the parent
query. Depending on how the subquery is written, it can be executed once for the parent
query or it can be executed once for each row returned by the parent query. If the subquery
is executed for each row of the parent, this is called a correlated subquery.
A correlated subquery can be easily identified if it contains any references to the parent
subquery columns in its WHERE clause. Columns from the subquery cannot be referenced
anywhere else in the parent query. The following example demonstrates a non-correlated
subquery.
E.g. Select * From CUST Where '10/03/1990' IN (Select ODATE From ORDER Where

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 57


CUST.CNUM = ORDER.CNUM)
62. What are the primitive operations common to all record management systems?
Addition, deletion and modification.
63. Name the buffer in which all the commands that are typed in are stored
‘Edit’ Buffer
64. What are the unary operations in Relational Algebra?
PROJECTION and SELECTION.
65. Are the resulting relations of PRODUCT and JOIN operation the same?
No.
PRODUCT: Concatenation of every row in one relation with every row in another.
JOIN: Concatenation of rows from one relation and related rows from another.
66. What is RDBMS KERNEL?
Two important pieces of RDBMS architecture are the kernel, which is the software, and the
data dictionary, which consists of the system-level data structures used by the kernel to
manage the database
You might think of an RDBMS as an operating system (or set of subsystems), designed
specifically for controlling data access; its primary functions are storing, retrieving, and
securing data. An RDBMS maintains its own list of authorized users and their associated
privileges; manages memory caches and paging; controls locking for concurrent resource
usage; dispatches and schedules user requests; and manages space usage within its table-
space structures.
67. Name the sub-systems of a RDBMS
I/O, Security, Language Processing, Process Control, Storage Management, Logging and
Recovery, Distribution Control, Transaction Control, Memory Management, Lock
Management
68. Which part of the RDBMS takes care of the data dictionary? How
Data dictionary is a set of tables and database objects that is stored in a special area of the
database and maintained exclusively by the kernel.
69. What is the job of the information stored in data-dictionary?
The information in the data dictionary validates the existence of the objects, provides access
to them, and maps the actual physical storage location.

Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 58


Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 59
Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 60
Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 61
Dept of CSE DBMS LABORATORY WITH MINI PROJECT– 18CSL58 62

You might also like