DBMS_Lab_Program-2
DBMS_Lab_Program-2
MANUAL
Program – 1
Objective
- To create relational database tables with appropriate constraints.
- To insert sample data into the tables.
- To write SQL queries to retrieve and manipulate data effectively.
Database Schema
EMPLOYEE (Ename: string, Ssn: integer, Bdate: date, Sex: string, Address: string, Salary:
real, Mgrssn: integer, Dno: integer)
DEPARTMENT (Dname: string, Dnumber: integer, Mgrssn: integer, Mgr_start_date: date)
PROJECT (Pname: string, Pnumber: integer, Plocation: string, Dnum: integer)
WORKS_ON (Essn: integer, Pno: integer, Hours: real)
DEPENDENT (Essn: integer, Dependent_name: string, Sex: string)
Plocation VARCHAR(100),
Dnum INT,
);
Essn INT,
Pno INT,
Hours REAL,
);
Essn INT,
Dependent_name VARCHAR(50),
Sex VARCHAR(10),
INSERT INTO EMPLOYEE VALUES ('John Doe', 101, '1990-01-01', 'M', '123 Main St', 50000,
NULL, 10);
INSERT INTO EMPLOYEE VALUES ('Jane Smith', 102, '1992-05-15', 'F', '456 Elm St', 60000,
NULL, 20);
INSERT INTO EMPLOYEE VALUES ('Alice Johnson', 103, '1985-03-10', 'F', '789 Oak St',
70000, NULL, 10);
INSERT INTO EMPLOYEE VALUES ('Bob Brown', 104, '1993-07-25', 'M', '321 Pine St',
55000, NULL, 30);
INSERT INTO EMPLOYEE VALUES ('Charlie Green', 105, '1988-11-30', 'M', '654 Cedar St',
45000, NULL, 20);
SQL Queries
1. Retrieve the name and address of all employees who work for the "ISE" department:
2. For each employee, retrieve the employee's name and the name of his or her immediate
supervisor:
4. For each department, retrieve the department name and the average salary of employees
in that department:
Evaluation Criteria
- Correct table creation syntax and constraints.
- Successful insertion of sample data.
- Correct and optimized SQL queries for the given tasks.
- Clear documentation of output.
Program – 2
Consider the following relation schema:
Create above tables by specifying primary key, foreign key and other suitable constraints.
Design a database to satisfy the above requirements and answer following queries
Objective
- To create relational database tables with appropriate constraints.
- To insert sample data into the tables.
- To write SQL queries to retrieve and manipulate data effectively.
Database Schema
SAILORS (Sid: integer, Sname: string, Rating: integer, Age: real)
BOATS (Bid: integer, Bname: string, Color: string)
RESERVES (Sid: integer, Bid: integer, Day: date)
SQL Queries
1. Find all sailors with a rating above 7:
SELECT Sname
FROM SAILORS
WHERE Rating > 7;
2. Find the names of sailors who have reserved boat number 103:
SELECT Sname
FROM SAILORS
WHERE Sid IN (
SELECT Sid
FROM RESERVES
WHERE Bid = 103
);
SELECT Sname
FROM SAILORS S
JOIN RESERVES R ON S.Sid = R.Sid
JOIN BOATS B ON R.Bid = B.Bid
WHERE B.Color = 'Red';
4. Find the names of sailors who have reserved a red or a green boat:
SELECT Sname
FROM SAILORS S
JOIN RESERVES R ON S.Sid = R.Sid
JOIN BOATS B ON R.Bid = B.Bid
WHERE B.Color IN ('Red', 'Green');
Evaluation Criteria
• Correct table creation syntax and constraints.
• Successful insertion of sample data.
• Correct and optimized SQL queries for the given tasks.
• Clear documentation of output.
• Answering Viva Questions
Viva Questions
1. What is a foreign key, and how does it ensure referential integrity?
2. Explain the purpose of the IN clause in SQL.
3. How does a JOIN clause help in combining multiple tables?
4. What is the difference between INNER JOIN and OUTER JOIN?
5. What is the significance of using WHERE clauses for filtering results?
Program – 3
Consider the following relation schema:
STUDENT (Snum: integer, Sname: string, Major: string, Level: string, Age: integer)
CLASS (Cname: string, Meets at: string, Room: string, Fid: integer)
The meaning of these relations is straightforward; for example, enrolled has one record per
student-class pair such that the student is enrolled in the class. Level is a two-character code
with 4 different values (example: Junior: JR etc)
Write the following queries in SQL. No duplicates should be printed in any of the answers.
i. Find the names of all Juniors (level = JR) who are enrolled in a class taught by Prof.
Harshith
ii. Find the names of all classes that either meet in room R128 or have five or more
Students enrolled.
iii. Find the names of all students who are enrolled in two classes that meet at the
same time.
iv. Find the names of faculty members who teach in every room in which some class
is taught.
Objective
1. To create relational database tables with appropriate constraints.
2. To insert sample data into the tables.
3. To write SQL queries to retrieve and manipulate data effectively.
Major VARCHAR(50),
Level VARCHAR(2),
Age INT
);
CREATE TABLE FACULTY (
Deptid INT
);
Meets_at VARCHAR(20),
Room VARCHAR(20),
Fid INT,
);
Snum INT,
Cname VARCHAR(50),
);
-- Student
-- Class
-- Enrolled
FROM STUDENT S
2. Find the names of all classes that either meet in room R128 or have five or
more students enrolled
FROM CLASS
UNION
SELECT Cname
FROM ENROLLED
GROUP BY Cname
FROM STUDENT S1
4. Find the names of faculty members who teach in every room in which some
class is taught
SELECT F.Fname
FROM FACULTY F
SELECT Room
FROM CLASS
EXCEPT
FROM CLASS C
);
Program – 4
Consider the relation schema for book dealer database:
AUTHOR (Author-id:int, Name:string, City:string, Country:string)
PUBLISHER (Publisher-id:int, Name:string, City:string, Country:string)
CATALOG (Book-id:int, Title:string, Author-id:int, Publisher-id:int, Category-id:int,
Year:int, Price:int)
CATEGORY (Category-id:int, Description:string)
ORDER-DETAILS (Order-no:int, Book-id:int, Quantity:int)
Create the above tables by properly specifying the primary keys and the foreign keys.
Enter at least five tuples for each relation.
i. Give the details of the authors who have 2 or more books in the catalog and the
price of the books is greater than the average price of the books in the catalog and
the year of publication is after 2000.
ii. Find the author of the book which has maximum sales.
iii. Demonstrate how you increase the price of books published by a specific publisher
by 10%
iv. List any department that has all its adopted books published by a specific publisher
Objective
Database Schema
Name VARCHAR(50),
City VARCHAR(50),
Country VARCHAR(50)
);
Name VARCHAR(50),
City VARCHAR(50),
Country VARCHAR(50)
);
Description VARCHAR(100)
);
Title VARCHAR(100),
Author_id INT,
Publisher_id INT,
Category_id INT,
Year INT,
Price INT,
);
CREATE TABLE ORDER_DETAILS (
Order_no INT,
Book_id INT,
Quantity INT,
);
INSERT INTO CATALOG VALUES (103, 'The Adventures of Tom Sawyer', 3, 2, 3, 1995, 250);
INSERT INTO CATALOG VALUES (104, 'Pride and Prejudice', 4, 2, 4, 2001, 350);
SQL Queries
1. Give the details of authors who have 2 or more books in the catalog, and the price of
the books is greater than the average price of the books in the catalog and the year
of publication is after 2000.
SELECT A.*
FROM AUTHOR A
AND A.Author_id IN (
SELECT Author_id
FROM CATALOG
GROUP BY Author_id
);
2. Find the author of the book which has maximum sales.
SELECT A.Name
FROM AUTHOR A
WHERE O.Quantity = (
);
3. Demonstrate how you increase the price of books published by a specific publisher
by 10%.
UPDATE CATALOG
4. List any department (category) that has all its adopted books published by a specific
publisher.
SELECT Category_id
FROM CATALOG
GROUP BY Category_id
Expected Output
• List of authors matching all filtering conditions.
• Name of the author whose book had maximum sales.
• All prices of books are updated for a specific publisher.
• Category IDs where all books were published by the same publisher.
Evaluation Criteria
• Proper creation of tables with all constraints.
• Correct data insertion.
• Accurate results for complex SQL queries.
• Neatly presented output and documentation.
Viva Questions
1. How do you implement aggregation in SQL?
2. What is a GROUP BY clause and when do we use HAVING?
3. How can we perform an update on multiple rows?
4. Explain how foreign keys help maintain integrity across tables.
5. How do subqueries help in filtering or calculating intermediate results?