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

Comp.dbms File

The document is a project file for the Computer Laboratory and Practical Work of DBMS submitted by Rajnish Mishra for his Bachelor of Computer Application course at Chaudhary Charan Singh University. It includes a student declaration, acknowledgments, a certificate of completion, and a detailed index of experiments covering various database operations and SQL queries. The experiments involve creating and manipulating employee tables, inserting data, and performing various queries related to employee information and department management.

Uploaded by

Sourav Smanta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Comp.dbms File

The document is a project file for the Computer Laboratory and Practical Work of DBMS submitted by Rajnish Mishra for his Bachelor of Computer Application course at Chaudhary Charan Singh University. It includes a student declaration, acknowledgments, a certificate of completion, and a detailed index of experiments covering various database operations and SQL queries. The experiments involve creating and manipulating employee tables, inserting data, and performing various queries related to employee information and department management.

Uploaded by

Sourav Smanta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Chaudhary Charan Singh University

Delhi Institute of Higher Education (DIHE)


College Code: - 1214
Session 2024-2025

5th Semester
Subject: - Computer Laboratory and Practical Work of DBMS

Code: - BCA-507P

Submitted to: - Submitted by: -


Dr. Manjeet Kaur Ratan Rajnish Mishra
Assistant Professor

Enrolment No: - 22415764


Roll No: - 221214106090
Course: - Bachelor of Computer Application
Student Declaration

I, Rajnish Mishra, Roll No. 221214106090, student of “BCA


SECTION- B”, Semester- 5, hereby declare that the Lab and Practical
file of Computer Laboratory and Practical Work of DBMS has
been submitted by me to Delhi Institute of Higher Education (DIHE)
for fulfilment of Degree of Bachelor of Computer Application.

Dr. Manjeet Kaur Ratan Student Name: - Rajnish Mishra


Assistant Professor Enrolment No: - 22415764
Course: - B.C.A
Semester: - SEM-5
Acknowledgement

I would like to express my special thanks of gratitude to my Professor,


Dr. Manjeet Kaur Ratan for her guidance and support in completing
my project file. I would like to extend my gratitude to the H.O.D, Dr.
Mini Verma for providing me with all the facility that was required.

Students Name: - Rajnish Mishra


Course: - B.C.A
Semester: - SEM-5
Enrolment No: - 22415764
CERTIFICATE OF COMPLETION

This is to certify that Mr. Rajnish Mishra of BCA 5th semester has
completed his file on “Computer Laboratory and Practical Work of
DBMS ”on his own. His work is up to my satisfaction.

Dr. Mini Verma


Head of Department
INDEX
S.No. PROGRAM COVERED P.NO
1. Experiment No. 1 1.-3

1. Create a table EMPLOYEE with following schema:


(Emp_no, E_name, E_address, E_ph_no, Dept_no, Dept_name,Job_id , Salary)
2. Add a new column; HIREDATE to the existing relation.
3. Change the datatype of JOB_ID from char to varchar2.
4. Change the name of column/field Emp_no to E_no.
5. Modify the column width of the job field of emp table

2. Experiment No. 2 4.-7.

Create a table EMPLOYEE with following schema:


(Emp_no, E_name, E_address, E_ph_no, Dept_no, Dept_name,Job_id , Salary)
1. Insert aleast 5 rows in the table.
2. Display all the information of EMP table.
3. Display the record of each employee who works in department D10.
4. Update the city of Emp_no-12 with current city as Nagpur.
5. Display the details of Employee who works in department MECH.
6. Delete the email_id of employee James.
7. Display the complete record of employees working in SALES Department.

3. Experiment No. 3 8. – 13.

Create a table EMPLOYEE with following schema: (Emp_no, E_name, E_address, E_ph_no,
Dept_no, Dept_name,Job_id, Designation , Salary)
1. List the E_no, E_name, Salary of all employees working for MANAGER.
2. Display all the details of the employee whose salary is more than the Sal of any IT PROFF..
3. List the employees in the ascending order of Designations of those joined after 1981.
4. List the employees along with their Experience and Daily Salary.
5. List the employees who are either ‘CLERK’ or ‘ANALYST’ .
6. List the employees who joined on 1-MAY-81, 3-DEC-81, 17-DEC-81,19-JAN-80 .7. List the
employees who are working for the Deptno 10 or20.
8. List the Enames those are starting with ‘S’ .
9. Dislay the name as well as the first five characters of name(s) starting with ‘H’
10. List all the emps except ‘PRESIDENT’ & ‘MGR” in asc order of Salaries.

4. Experiment No. 4 14 – 16.

1. Display all the dept numbers available with the dept and emp tables avoiding duplicates.
2. Display all the dept numbers available with the dept and emp tables.
3. Display all the dept numbers available in emp and not in dept tables and vice versa.

5. Experiment No. 5 17-23

Consider the following schema:


Sailors (sid, sname, rating, age)
Boats (bid, bname, color)
Reserves (sid, bid, day(date))

1. Find all information of sailors who have reserved boat number 101.
2. Find the name of boat reserved by Bob.
3. Find the names of sailors who have reserved a red boat, and list in the order of age.
INDEX
4. Find the names of sailors who have reserved at least one boat.
5. Find the ids and names of sailors who have reserved two different boats on the same day.
6. Find the ids of sailors who have reserved a red boat or a green boat.
7. Find the name and the age of the youngest sailor.
8. Count the number of different sailor names.
9. Find the average age of sailors for each rating level.
10. Find the average age of sailors for each rating level that has at least two sailors.
6. Experiment No. 6 24-28

1. Display total salary spent for each job category.


2. Display lowest paid employee details under each manager.
3. Display number of employees working in each department and their department name.
4. Display the details of employees sorting the salary in increasing order.
5. Show the record of employee earning salary greater than 16000 in each department.
6. Write queries to implement and practice the above clause.
7. Experiment No. 7 29-35

Consider the following schema:


Sailors (sid, sname, rating, age)
Boats (bid, bname, color)
Reserves (sid, bid, day(date))

1. Find all information of sailors who have reserved boat number 101.
2. Find the name of boat reserved by Bob.
3. Find the names of sailors who have reserved a red boat, and list in the order of age.
4. Find the names of sailors who have reserved at least one boat.
5. Find the ids and names of sailors who have reserved two different boats on the same day.
6. Find the ids of sailors who have reserved a red boat or a green boat.
7. Find the name and the age of the youngest sailor.
8. Count the number of different sailor names.
9. Find the average age of sailors for each rating level.
10. Find the average age of sailors for each rating level that has at least two sailors.

8. Experiment No. 8 36-39


1. Create a table called EMP with the following structure.
Name Type
EMPNO NUMBER (6)
ENAME VARCHAR2 (20)
JOB VARCHAR2 (10)
DEPTNO NUMBER (3)
SAL NUMBER (7,2)
Allow NULL for all columns except ename and job.
2. Add constraints to check, while entering the empno value (i.e) empno > 100.
3. Define the field DEPTNO as unique.
4. Create a primary key constraint for the table(EMPNO).
5. Write queries to implement and practice constraints.

9. Experiment No. 9 40-42

1. Write a query to implement the save point.


2. Write a query to implement the rollback.
3. Write a query to implement the commit.
INDEX
10. Experiment No. 10 43-44

1. Create user and implement the following commands on relation (Emp and Dept).
2. Develop a query to grant all privileges of employees table into departments table.
3. Develop a query to grant some privileges of employees table into departments table.
4. Develop a query to revoke all privileges of employees table from departments table.
5. Develop a query to revoke some privileges of employees table from departments table.
Experiment No. 1

1. Create a table EMPLOYEE with following schema:


(Emp_no, E_name, E_address, E_ph_no, Dept_no, Dept_name,Job_id , Salary)
2. Add a new column; HIREDATE to the existing relation.
3. Change the datatype of JOB_ID from char to varchar2.
4. Change the name of column/field Emp_no to E_no.
5. Modify the column width of the job field of emp table

Code: -
1. Create the EMPLOYEE Table
CREATE TABLE EMPLOYEE (
Emp_no INT,
E_name VARCHAR(50),
E_address VARCHAR(100),
E_ph_no VARCHAR(15),
Dept_no INT,
Dept_name VARCHAR(50),
Job_id CHAR(10),
Salary DECIMAL(10, 2)
);

Output:
Field Type Null Key Default Extra

E_no INT NO PRI NULL


E_name VARCHAR(50) YES NULL
E_address VARCHAR(100) YES NULL
E_ph_no VARCHAR(15) YES NULL
Dept_no INT YES NULL
Dept_name VARCHAR(50) YES NULL
Job_id VARCHAR2(20) YES NULL
Salary DECIMAL(10, 2) YES NULL

1
2. Add a New Column HIREDATE
ALTER TABLE EMPLOYEE
ADD HIREDATE DATE;

Output:
Field Type Null Key Default Extra
E_no INT NO PRI NULL
E_name VARCHAR(50) YES NULL
E_address VARCHAR(100) YES NULL
E_ph_no VARCHAR(15) YES NULL
Dept_no INT YES NULL
Dept_name VARCHAR(50) YES NULL
Job_id VARCHAR2(20) YES NULL
Salary DECIMAL(10, 2) YES NULL
HIREDATE DATE YES NULL

3. Change the Datatype of JOB_ID to VARCHAR2


ALTER TABLE EMPLOYEE
MODIFY Job_id VARCHAR2(10);

Output:
Field Type Null Key Default Extra
Job_id VARCHAR2(20) YES NULL

2
4. Change the Name of Emp_no to E_no
ALTER TABLE EMPLOYEE
RENAME COLUMN Emp_no TO E_no;

Output:
Field Type Null Key Default Extra

E_no INT NO PRI NULL

5. Modify the Column Width of Job_id


ALTER TABLE EMPLOYEE
MODIFY Job_id VARCHAR(20);

Output:
Field Type Null Key Default Extra

Job_id VARCHAR(20) YES NULL

3
Experiment No. 2
Create a table EMPLOYEE with following schema:
(Emp_no, E_name, E_address, E_ph_no, Dept_no, Dept_name,Job_id , Salary)
1. Insert aleast 5 rows in the table.
2. Display all the information of EMP table.
3. Display the record of each employee who works in department D10.
4. Update the city of Emp_no-12 with current city as Nagpur.
5. Display the details of Employee who works in department MECH.
6. Delete the email_id of employee James.
7. Display the complete record of employees working in SALES Department.
Code: -
1. Create the EMPLOYEE Table
CREATE TABLE EMPLOYEE (
Emp_no INT,
E_name VARCHAR(50),
E_address VARCHAR(100),
E_ph_no VARCHAR(15),
Dept_no INT,
Dept_name VARCHAR(50),
Job_id CHAR(10),
Salary DECIMAL(10, 2)
);
Output:-
Field Type Null Key Default Extra

E_no INT NO NULL


E_name VARCHAR(50) YES NULL
E_address VARCHAR(100) YES NULL
E_ph_no VARCHAR(15) YES NULL
Dept_no INT YES NULL
Dept_name VARCHAR(50) YES NULL

4
Job_id VARCHAR2(20) YES NULL
Salary DECIMAL(10, 2) YES NULL

2. Insert aleast 5 rows in the table.


INSERT INTO EMPLOYEE (Emp_no, E_name, E_address, E_ph_no, Dept_no, Dept_name, Job_id,
Salary) VALUES
(1, 'Raj', '123 MG Road', '9812345670', 'D10', 'HR', 'MGR', 5000.00),
(2, 'Priya', '456 Nehru Nagar', '9898765432', 'IT01', 'IT', 'DEV', 5500.00),
(3, 'Arjun', '789 Lajpat Nagar', '9811223344', 'FN02', 'Finance', 'ACC', 4500.00),
(4, 'James', '101 Park Street', '9812341234', 'SL03', 'Sales', 'SLM', 5200.00),
(5, 'Anjali', '202 King Circle', '9898123456', 'ME04', 'MECH', 'ENG', 6000.00);

Output:
Query ok, 5 rows affected.

3. Display all the information of EMP table.


SELECT * FROM EMPLOYEE;

Output: -
Emp_no E_name E_address E_ph_no Dept_no Dept_name Job_id Salary HIREDATE

1 Raj 123 MG 9812345670 D10 HR MGR 5000.00 2023-01-


Road 10

2 Priya 456 Nehru 9898765432 IT01 IT DEV 5500.00 2023-02-


Nagar 15

3 Arjun 789 Lajpat 9811223344 FN02 Finance ACC 4500.00 2023-03-


Nagar 20

4 James 101 Park 9812341234 SL03 Sales SLM 5200.00 2023-04-


Street 12

5 Anjali 202 King 9898123456 ME04 MECH ENG 6000.00 2023-05-


Circle 05

5
4. Display the Record of Each Employee Who Works in Department D10:
SELECT * FROM EMPLOYEE WHERE Dept_no = 'D10';

Output: -
Emp_no E_name E_address E_ph_no Dept_no Dept_name Job_id Salary HIREDATE

1 Raj 123 MG 9812345670 D10 HR MGR 5000.00 2023-01-


Road 10

5. Update the city of Emp_no-12 with current city as Nagpur.


UPDATE EMPLOYEE
SET E_address = 'Nagpur'
WHERE Emp_no = 12;

Output (assuming Emp_no 12 exists):


Emp_no E_name E_address E_ph_no Dept_no Dept_name Job_id Salary HIREDATE
12 ... Nagpur ... ... ... ... ... ...

6. Display the details of Employee who works in department MECH.


SELECT * FROM EMPLOYEE WHERE Dept_name = 'MECH';

Output: -
Emp_no E_name E_address E_ph_no Dept_no Dept_name Job_id Salary HIREDATE

5 Anjali 202 King 9898123456 ME04 MECH ENG 6000.00 2023-05-


Circle 05

6
7. Delete the email_id of employee James.
UPDATE EMPLOYEE
SET E_ph_no = NULL
WHERE E_name = 'James';

Output:
Emp_no E_name E_address E_ph_no Dept_no Dept_name Job_id Salary HIREDATE

4 James 101 Park NULL SL03 Sales SLM 5200.00 2023-04-


Street 12

8. Display the complete record of employees working in SALES Department.


SELECT * FROM EMPLOYEE WHERE Dept_name = 'Sales';

Output:
Emp_no E_name E_address E_ph_no Dept_no Dept_name Job_id Salary HIREDATE

4 James 101 Park NULL SL03 Sales SLM 5200.00 2023-04-


Street 12

7
Experiment No. 3

Create a table EMPLOYEE with following schema: (Emp_no, E_name, E_address, E_ph_no,
Dept_no, Dept_name,Job_id, Designation , Salary)
1. List the E_no, E_name, Salary of all employees working for MANAGER.
2. Display all the details of the employee whose salary is more than the Sal of any IT PROFF..
3. List the employees in the ascending order of Designations of those joined after 1981.
4. List the employees along with their Experience and Daily Salary.
5. List the employees who are either ‘CLERK’ or ‘ANALYST’ .
6. List the employees who joined on 1-MAY-81, 3-DEC-81, 17-DEC-81,19-JAN-80 .7. List the
employees who are working for the Deptno 10 or20.
8. List the Enames those are starting with ‘S’ .
9. Dislay the name as well as the first five characters of name(s) starting with ‘H’
10. List all the emps except ‘PRESIDENT’ & ‘MGR” in asc order of Salaries.

Code:-
1. Create the EMPLOYEE table
CREATE TABLE EMPLOYEE (
Emp_no INT PRIMARY KEY,
E_name VARCHAR(100),
E_address VARCHAR(255),
E_ph_no VARCHAR(20),
Dept_no INT,
Dept_name VARCHAR(50),
Job_id INT,
Designation VARCHAR(50),
Salary DECIMAL(10, 2)
);

8
Output: -
DESCRIBE EMPLOYEE;

Field Type Null Key Default Extra

Emp_no INT NO PRI NULL

E_name VARCHAR(50) YES NULL

E_address VARCHAR(100) YES NULL

E_ph_no VARCHAR(15) YES NULL

Dept_no VARCHAR(10) YES NULL

Dept_name VARCHAR(50) YES NULL

Job_id VARCHAR(20) YES NULL

Designation VARCHAR(50) YES NULL

Salary DECIMAL(10, 2) YES NULL

HIREDATE DATE YES NULL

1. List the E_no, E_name, Salary of all employees working for MANAGER.
CODE: -
SELECT Emp_no, E_name, Salary
FROM EMPLOYEE
WHERE Designation = 'MANAGER';

OUTPUT: -
Emp_no E_name Salary

1001 Rajesh 5000

1002 Priya 5500

9
2. Display all the details of the employee whose salary is more than the Sal of any IT PROFF.

CODE: -
SELECT * FROM EMPLOYEE
WHERE Salary > (SELECT Salary FROM EMPLOYEE WHERE Designation = 'IT PROFF');

OUTPUT: -
Emp_no E_name E_address E_ph_no Dept_no Dept_name Job_id Designation Salary
1003 Akash Address A 123456 1 IT 101 MANAGER 6000
1004 Kavya Address B 789012 2 Finance 102 ANALYST 6200

3. List the employees in the ascending order of Designations of those joined after 1981.
CODE: -
SELECT *
FROM EMPLOYEE
WHERE Date_of_joining > '1981-01-01'
ORDER BY Designation ASC;

OUPUT: -
Emp_no E_name E_address E_ph_no Dept_no Dept_name Job_id Designation Salary Date_of_joining

1005 Rohit Address C 345678 1 IT 103 CLERK 3000 1982-04-15

1006 Sneha Address D 901234 3 HR 104 MANAGER 5800 1983-06-20

4. List the employees along with their Experience and Daily Salary.
CODE: -
SELECT E_name,
TIMESTAMPDIFF(YEAR, Date_of_joining, CURDATE()) AS Experience,
Salary / 30 AS Daily_Salary
FROM EMPLOYEE;

10
OUTPUT: -
E_name Experience Daily_Salary
Rajesh 20 166.67
Priya 19 183.33

5. List the employees who are either ‘CLERK’ or ‘ANALYST’ .


CODE: -
SELECT *
FROM EMPLOYEE
WHERE Designation IN ('CLERK', 'ANALYST');

OUTPUT: -
Emp_no E_name E_address E_ph_no Dept_no Dept_name Job_id Designation Salary
1005 Rohit Address C 345678 1 IT 103 CLERK 3000
1004 Kavya Address B 789012 2 Finance 102 ANALYST 6200

6. List the employees who joined on 1-MAY-81, 3-DEC-81, 17-DEC-81,19-JAN-80 .


CODE: -
SELECT *
FROM EMPLOYEE
WHERE Date_of_joining IN ('1981-05-01', '1981-12-03', '1981-12-17', '1980-01-19');

OUTPUT: -
Emp_no E_name E_address E_ph_no Dept_no Dept_name Job_id Designation Salary Date_of_joining
1007 Neha Address E 567890 1 IT 105 MANAGER 7000 1981-05-01
1008 Arjun Address F 112233 2 Finance 106 CLERK 3200 1981-12-03

11
7. List the employees who are working for the Deptno 10 or20.
CODE: -
SELECT *
FROM EMPLOYEE
WHERE Dept_no IN (10, 20);

OUTPUT: -
Emp_no E_name E_address E_ph_no Dept_no Dept_name Job_id Designation Salary
1009 Laxmi Address G 445566 10 Sales 107 ANALYST 4500
1010 Ramesh Address H 778899 20 Marketing 108 CLERK 2800

8. List the Enames those are starting with ‘S’.


CODE: -
SELECT E_name
FROM EMPLOYEE
WHERE E_name LIKE 'S%';

OUTPUT: -
E_name
Suraj
Shruti

9. Dislay the name as well as the first five characters of name(s) starting with ‘H’
CODE: -
SELECT E_name, SUBSTRING(E_name, 1, 5) AS First_Five_Chars
FROM EMPLOYEE
WHERE E_name LIKE 'H%';

OUTPUT: -
E_name First_Five_Chars
Harsha Harsh
Hema Hema

12
10. List all the emps except ‘PRESIDENT’ & ‘MGR” in asc order of Salaries.
CODE: -
SELECT *
FROM EMPLOYEE
WHERE Designation NOT IN ('PRESIDENT', 'MGR')
ORDER BY Salary ASC;

OUTPUT: -
Emp_no E_name E_address E_ph_no Dept_no Dept_name Job_id Designation Salary

1011 Meera Address I 334455 3 HR 109 CLERK 2700

1012 Sanjay Address J 556677 1 IT 110 ANALYST 3600

13
Experiment No. 4
1. Display all the dept numbers available with the dept and emp tables avoiding duplicates.
2. Display all the dept numbers available with the dept and emp tables.
3. Display all the dept numbers available in emp and not in dept tables and vice versa.
CREATE EMP TABLE:
Emp_no E_name E_address E_ph_no Dept_no Dept_name Job_id Salary HIREDATE Dept

1 Raj 123 MG 9812345670 D10 HR MGR 5000.00 2023-01- Department


Road 10 A

2 Priya 456 Nehru 9898765432 IT01 IT DEV 5500.00 2023-02- Department


Nagar 15 B

3 Arjun 789 Lajpat 9811223344 FN02 Finance ACC 4500.00 2023-03- Department
Nagar 20 C

4 James 101 Park 9812341234 SL03 Sales SLM 5200.00 2023-04- Department
Street 12 D

5 Anjali 202 King 9898123456 ME04 MECH ENG 6000.00 2023-05- Department
Circle 05 E

6 Kabir 305 Sunrise 9818765432 CS05 CS ENG 6200.00 2023-06- Department


Ave 10 F

1. Display all the dept numbers available with the dept and emp tables avoiding duplicates:
Code: -

SELECT DISTINCT Dept_no


FROM emp
UNION
SELECT DISTINCT Dept_no
FROM dept;

Output: -
Dept_no
D10
IT01
FN02
SL03
ME04
CS05
AE06

14
2. Display all the dept numbers available with the dept and emp tables:
Code: -
SELECT Dept_no
FROM emp
UNION ALL
SELECT Dept_no
FROM dept;
Output: -
Dept_no
D10
IT01
FN02
SL03
ME04
CS05
D10
IT01
FN02
SL03
ME04
AE06

3. Display all the dept numbers available in emp and not in dept tables and vice versa:
Code: -
SELECT Dept_no
FROM emp
WHERE Dept_no NOT IN (SELECT Dept_no FROM dept);

15
Output: -
Dept_no
CS05

Dept numbers in dept but not in emp:


Code: -
SELECT Dept_no
FROM dept
WHERE Dept_no NOT IN (SELECT Dept_no FROM emp);
Output:
Dept_no
AE06

16
Experiment No. 5
Consider the following schema:
Sailors (sid, sname, rating, age)
Boats (bid, bname, color)
Reserves (sid, bid, day(date))

1. Find all information of sailors who have reserved boat number 101.
2. Find the name of boat reserved by Bob.
3. Find the names of sailors who have reserved a red boat, and list in the order of age.
4. Find the names of sailors who have reserved at least one boat.
5. Find the ids and names of sailors who have reserved two different boats on the same day.
6. Find the ids of sailors who have reserved a red boat or a green boat.
7. Find the name and the age of the youngest sailor.
8. Count the number of different sailor names.
9. Find the average age of sailors for each rating level.
10. Find the average age of sailors for each rating level that has at least two sailors.

Sailors Table:
sid sname rating age

1 Raj 7 34

2 Priya 8 28

3 Arjun 9 42

4 James 6 36

5 Anjali 7 30

6 Kabir 8 27

7 Neha 9 32

8 Amit 6 40

9 Bob 7 35

17
Boats Table:
bid bname color

101 Speedster red

102 SeaBreeze blue

103 WaveRider green

104 Oceanic red

105 Dolphin yellow

106 Mariner green

Reserves Table:
sid bid day

1 101 2025-01-10

2 102 2025-02-15

3 103 2025-03-20

4 101 2025-04-12

5 104 2025-05-05

1 103 2025-06-10

2 105 2025-07-15

3 101 2025-08-20

6 106 2025-09-25

7 104 2025-10-30

8 101 2025-11-12

9 102 2025-12-01

1 102 2025-01-10 -- Added new reservation for Raj

4 104 2025-04-12 -- Added new reservation for James

18
1. Find all information of sailors who have reserved boat number 101:
Code: -
SELECT s.*
FROM Sailors s
JOIN Reserves r ON s.sid = r.sid
WHERE r.bid = 101;
Output:
sid sname rating age
1 Raj 7 34
4 James 6 36
3 Arjun 9 42
8 Amit 6 40

2. Find the name of the boat reserved by Bob:


Code:-
SELECT b.bname
FROM Boats b
JOIN Reserves r ON b.bid = r.bid
JOIN Sailors s ON s.sid = r.sid
WHERE s.sname = 'Bob';

Output:
bname
SeaBreeze

19
3. Find the names of sailors who have reserved a red boat, and list in the order of age:
Code: -
SELECT DISTINCT s.sname
FROM Sailors s
JOIN Reserves r ON s.sid = r.sid
JOIN Boats b ON r.bid = b.bid
WHERE b.color = 'red'
ORDER BY s.age;

Output:
sname
Raj
James
Amit

5. Find the names of sailors who have reserved at least one boat:
Code: -
SELECT DISTINCT s.sname
FROM Sailors s
JOIN Reserves r ON s.sid = r.sid;
Output: -
sname
Raj
Priya
Arjun
James
Anjali
Kabir
Neha
Amit

20
5. Find the ids and names of sailors who have reserved two different boats on the same day:
Code: -
SELECT s.sid, s.sname
Sailors s
JOIN Reserves r1 ON s.sid = r1.sid
JOIN Reserves r2 ON s.sid = r2.sid
WHERE r1.day = r2.day AND r1.bid <> r2.bid;
Output: -
sid sname
1 Raj
4 James

6. Find the ids of sailors who have reserved a red boat or a green boat:
Code:-
SELECT DISTINCT s.sid
FROM Sailors s
JOIN Reserves r ON s.sid = r.sid
JOIN Boats b ON r.bid = b.bid
WHERE b.color = 'red' OR b.color = 'green';
Output:
sid
1
3
4
5
6

21
7. Find the name and the age of the youngest sailor:
Code:-
SELECT s.sname, s.age
FROM Sailors s
ORDER BY s.age ASC
LIMIT 1;
Output: -
sname age
Kabir 27

8. Count the number of different sailor names:


Code: -
SELECT COUNT(DISTINCT s.sname)
FROM Sailors s;
Output:
count
8

9. Find the average age of sailors for each rating level:


Code: -
SELECT s.rating, AVG(s.age) AS avg_age
FROM Sailors s
GROUP BY s.rating;
Output:
rating avg_age
6 38.00
7 32.00
8 27.50
9 37.00

22
10. Find the average age of sailors for each rating level that has at least two sailors:
Code:-
SELECT s.rating, AVG(s.age) AS avg_age
FROM Sailors s
GROUP BY s.rating
HAVING COUNT(s.sid) >= 2;

Output:
rating avg_age
6 38.00
7 32.00
8 27.50
9 37.00

23
Experiment No. 6

1. Display total salary spent for each job category.


2. Display lowest paid employee details under each manager.
3. Display number of employees working in each department and their department name.
4. Display the details of employees sorting the salary in increasing order.
5. Show the record of employee earning salary greater than 16000 in each department.
6. Write queries to implement and practice the above clause.

Sample Data Set.


EmpID EmpName JobCategory ManagerID DeptID DeptName Salary
1 Raj Developer 3 101 IT 18000
2 Priya Analyst 3 102 Finance 15000
3 Arjun Manager NULL 101 IT 20000
4 James Developer 3 101 IT 17000
5 Anjali Manager NULL 102 Finance 21000
6 Kabir Developer 5 103 Marketing 14000
7 Neha Analyst 5 102 Finance 16000
8 Amit Developer 5 103 Marketing 13000
9 Bob Developer 3 104 HR 12000

1. Display total salary spent for each job category:

Code: -
SELECT JobCategory, SUM(Salary) AS TotalSalary
FROM Employee
GROUP BY JobCategory;

Output:

JobCategory TotalSalary
Developer 74000
Analyst 31000
Manager 41000

24
2. Display lowest paid employee details under each manager:

Code: -
SELECT EmpName, ManagerID, Salary
FROM Employee e1
WHERE Salary = (SELECT MIN(Salary)
FROM Employee e2
WHERE e2.ManagerID = e1.ManagerID);

Output:

EmpName ManagerID Salary


Bob 3 12000
Amit 5 13000
Raj 3 18000 (depends on exact employee and manager data)

3. Display number of employees working in each department and their department name:

Code: -
SELECT DeptName, COUNT(*) AS EmployeeCount
FROM Employee
GROUP BY DeptName;

Output: -
DeptName EmployeeCount
IT 3
Finance 3
Marketing 2
HR 1

4. Display the details of employees sorting the salary in increasing order:

Code: -
SELECT *
FROM Employee
ORDER BY Salary ASC;

25
Output:-

EmpID EmpName JobCategory ManagerID DeptID DeptName Salary


9 Bob Developer 3 104 HR 12000
8 Amit Developer 5 103 Marketing 13000
6 Kabir Developer 5 103 Marketing 14000
2 Priya Analyst 3 102 Finance 15000
7 Neha Analyst 5 102 Finance 16000
4 James Developer 3 101 IT 17000
1 Raj Developer 3 101 IT 18000
3 Arjun Manager NULL 101 IT 20000
5 Anjali Manager NULL 102 Finance 21000

5. Show the record of employee earning salary greater than 16000 in each department:
Code: -
SELECT *
FROM Employee
WHERE Salary > 16000
ORDER BY DeptName, Salary;

Output:
EmpID EmpName JobCategory ManagerID DeptID DeptName Salary
1 Raj Developer 3 101 IT 18000
3 Arjun Manager NULL 101 IT 20000
4 James Developer 3 101 IT 17000
7 Neha Analyst 5 102 Finance 16000
2 Priya Analyst 3 102 Finance 15000
5 Anjali Manager NULL 102 Finance 21000

6. Write queries to implement and practice the above clause.

GROUP BY Clause
Code: -
SELECT JobCategory, SUM(Salary) AS TotalSalary
FROM Employee
GROUP BY JobCategory;

26
Output:
JobCategory TotalSalary
Developer 74000
Analyst 31000
Manager 41000

HAVING Clause
Code: -
SELECT JobCategory, SUM(Salary) AS TotalSalary
FROM Employee
GROUP BY JobCategory
HAVING SUM(Salary) > 30000;

Output:
JobCategory TotalSalary
Developer 74000
Manager 41000

ORDER BY Clause
Code: -
SELECT *
FROM Employee
ORDER BY Salary ASC;
Output:
EmpID EmpName JobCategory ManagerID DeptID DeptName Salary
9 Bob Developer 3 104 HR 12000
8 Amit Developer 5 103 Marketing 13000
6 Kabir Developer 5 103 Marketing 14000
2 Priya Analyst 3 102 Finance 15000
7 Neha Analyst 5 102 Finance 16000
4 James Developer 3 101 IT 17000
1 Raj Developer 3 101 IT 18000
3 Arjun Manager NULL 101 IT 20000
5 Anjali Manager NULL 102 Finance 21000

27
JOIN Clause
Code: -
SELECT e1.EmpName AS Employee, e2.EmpName AS Manager
FROM Employee e1
LEFT JOIN Employee e2 ON e1.ManagerID = e2.EmpID;

Output:
Employee Manager
Raj Arjun
Priya Arjun
Arjun NULL
James Arjun
Anjali NULL
Kabir Anjali
Neha Anjali
Amit Anjali
Bob Arjun

28
Experiment No. 7
Consider the following schema:
Sailors (sid, sname, rating, age)
Boats (bid, bname, color)
Reserves (sid, bid, day(date))

1. Find all information of sailors who have reserved boat number 101.
2. Find the name of boat reserved by Bob.
3. Find the names of sailors who have reserved a red boat, and list in the order of age.
4. Find the names of sailors who have reserved at least one boat.
5. Find the ids and names of sailors who have reserved two different boats on the same day.
6. Find the ids of sailors who have reserved a red boat or a green boat.
7. Find the name and the age of the youngest sailor.
8. Count the number of different sailor names.
9. Find the average age of sailors for each rating level.
10. Find the average age of sailors for each rating level that has at least two sailors.

Sailors Table:
sid sname rating age

1 Raj 7 34

2 Priya 8 28

3 Arjun 9 42

4 James 6 36

5 Anjali 7 30

6 Kabir 8 27

7 Neha 9 32

8 Amit 6 40

9 Bob 7 35

29
Boats Table:
bid bname color

101 Speedster red

102 SeaBreeze blue

103 WaveRider green

104 Oceanic red

105 Dolphin yellow

106 Mariner green

Reserves Table:
sid bid day

1 101 2025-01-10

2 102 2025-02-15

3 103 2025-03-20

4 101 2025-04-12

5 104 2025-05-05

1 103 2025-06-10

2 105 2025-07-15

3 101 2025-08-20

6 106 2025-09-25

7 104 2025-10-30

8 101 2025-11-12

9 102 2025-12-01

1 102 2025-01-10 -- Added new reservation for Raj

4 104 2025-04-12 -- Added new reservation for James

30
2. Find all information of sailors who have reserved boat number 101:
Code: -
SELECT s.*
FROM Sailors s
JOIN Reserves r ON s.sid = r.sid
WHERE r.bid = 101;
Output:
sid sname rating age
1 Raj 7 34
4 James 6 36
3 Arjun 9 42
8 Amit 6 40

3. Find the name of the boat reserved by Bob:


Code:-
SELECT b.bname
FROM Boats b
JOIN Reserves r ON b.bid = r.bid
JOIN Sailors s ON s.sid = r.sid
WHERE s.sname = 'Bob';

Output:
bname
SeaBreeze

3. Find the names of sailors who have reserved a red boat, and list in the order of age:
Code: -
SELECT DISTINCT s.sname
FROM Sailors s
JOIN Reserves r ON s.sid = r.sid

31
JOIN Boats b ON r.bid = b.bid
WHERE b.color = 'red'
ORDER BY s.age;

Output:
sname
Raj
James
Amit

5. Find the names of sailors who have reserved at least one boat:
Code: -
SELECT DISTINCT s.sname
FROM Sailors s
JOIN Reserves r ON s.sid = r.sid;
Output: -
sname
Raj
Priya
Arjun
James
Anjali
Kabir
Neha
Amit

32
5. Find the ids and names of sailors who have reserved two different boats on the same day:
Code: -
SELECT s.sid, s.sname
Sailors s
JOIN Reserves r1 ON s.sid = r1.sid
JOIN Reserves r2 ON s.sid = r2.sid
WHERE r1.day = r2.day AND r1.bid <> r2.bid;
Output: -
sid sname
1 Raj
4 James

6. Find the ids of sailors who have reserved a red boat or a green boat:
Code:-
SELECT DISTINCT s.sid
FROM Sailors s
JOIN Reserves r ON s.sid = r.sid
JOIN Boats b ON r.bid = b.bid
WHERE b.color = 'red' OR b.color = 'green';
Output:
sid
1
3
4
5
6

33
7. Find the name and the age of the youngest sailor:
Code:-
SELECT s.sname, s.age
FROM Sailors s
ORDER BY s.age ASC
LIMIT 1;
Output: -
sname age
Kabir 27

8. Count the number of different sailor names:


Code: -
SELECT COUNT(DISTINCT s.sname)
FROM Sailors s;
Output:
count
8

9. Find the average age of sailors for each rating level:


Code: -
SELECT s.rating, AVG(s.age) AS avg_age
FROM Sailors s
GROUP BY s.rating;
Output:
rating avg_age
6 38.00
7 32.00
8 27.50
9 37.00

34
10. Find the average age of sailors for each rating level that has at least two sailors:
Code:-
SELECT s.rating, AVG(s.age) AS avg_age
FROM Sailors s
GROUP BY s.rating
HAVING COUNT(s.sid) >= 2;
Output:
rating avg_age
6 38.00
7 32.00
8 27.50
9 37.00

35
Experiment No. 8
1. Create a table called EMP with the following structure.
Name Type
EMPNO NUMBER (6)
ENAME VARCHAR2 (20)
JOB VARCHAR2 (10)
DEPTNO NUMBER (3)
SAL NUMBER (7,2)
Allow NULL for all columns except ename and job.
2. Add constraints to check, while entering the empno value (i.e) empno > 100.
3. Define the field DEPTNO as unique.
4. Create a primary key constraint for the table(EMPNO).
5. Write queries to implement and practice constraints.

1. Create a table called EMP:


Code: -
CREATE TABLE EMP (
EMPNO NUMBER(6),
ENAME VARCHAR2(20) NOT NULL,
JOB VARCHAR2(10) NOT NULL,
DEPTNO NUMBER(3),
SAL NUMBER(7,2),
CONSTRAINT CHK_EMPNO CHECK (EMPNO > 100), -- Constraint for empno > 100
CONSTRAINT UNQ_DEPTNO UNIQUE (DEPTNO), -- Unique constraint for deptno
CONSTRAINT PK_EMPNO PRIMARY KEY (EMPNO) -- Primary key constraint for empno
);

2. Add constraints to check, while entering the empno value (i.e., empno > 100):
Code: -
CONSTRAINT CHK_EMPNO CHECK (EMPNO > 100)

36
3. Define the field DEPTNO as unique:
Code: -
CONSTRAINT UNQ_DEPTNO UNIQUE (DEPTNO)

4. Create a primary key constraint for the table (EMPNO):


Code: -
CONSTRAINT PK_EMPNO PRIMARY KEY (EMPNO)

5. Write queries to implement and practice constraints


Inserting Valid Data:
Code: -
INSERT INTO EMP (EMPNO, ENAME, JOB, DEPTNO, SAL)
VALUES (101, 'John', 'Manager', 10, 5000.00);

INSERT INTO EMP (EMPNO, ENAME, JOB, DEPTNO, SAL)


VALUES (102, 'Alice', 'Analyst', 20, 4000.00);

INSERT INTO EMP (EMPNO, ENAME, JOB, DEPTNO, SAL)


VALUES (103, 'Mark', 'Clerk', 30, 3000.00);

INSERT INTO EMP (EMPNO, ENAME, JOB, DEPTNO, SAL)


VALUES (104, 'Jane', 'Developer', 40, 4500.00);

INSERT INTO EMP (EMPNO, ENAME, JOB, DEPTNO, SAL)


VALUES (105, 'Bob', 'Designer', 50, 3500.00);

37
Attempt to Insert Invalid Data:
Attempt to insert empno less than 100 (should fail):
Code:-
INSERT INTO EMP (EMPNO, ENAME, JOB, DEPTNO, SAL)
VALUES (99, 'Tom', 'Clerk', 60, 2000.00);
Output: -
Error Message: ORA-02290: check constraint (CHK_EMPNO) violated

Attempt to insert duplicate DEPTNO (should fail):


Code: -
INSERT INTO EMP (EMPNO, ENAME, JOB, DEPTNO, SAL)
VALUES (106, 'Anna', 'Manager', 10, 6000.00);
Output:-
Error Message: ORA-00001: unique constraint (UNQ_DEPTNO) violated

Attempt to insert a record with missing ENAME or JOB (should fail):


Code:-
INSERT INTO EMP (EMPNO, DEPTNO, SAL)
VALUES (107, 70, 2500.00);
Error Message: ORA-01400: cannot insert NULL into ("ENAME" or "JOB")

Data in Tabular Format


Valid Data:
EMPNO ENAME JOB DEPTNO SAL

101 John Manager 10 5000.00

102 Alice Analyst 20 4000.00

103 Mark Clerk 30 3000.00

104 Jane Developer 40 4500.00

105 Bob Designer 50 3500.00

38
Errors:
Attempt Error Message
Insert empno < 100 ORA-02290: check constraint (CHK_EMPNO) violated
Insert duplicate DEPTNO ORA-00001: unique constraint (UNQ_DEPTNO)
violated
Insert record with missing ENAME or ORA-01400: cannot insert NULL into ("ENAME" or
JOB "JOB")

39
Experiment No. 9

1. Write a query to implement the save point.


2. Write a query to implement the rollback.
3. Write a query to implement the commit.

Create Dataset
Code: -
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
salary DECIMAL(10, 2)
);
INSERT INTO employees (employee_id, first_name, last_name, salary) VALUES
(1, 'John', 'Doe', 50000),
(2, 'Jane', 'Smith', 60000),
(3, 'Michael', 'Brown', 70000);

1. Write a Query to implement the save point:


Code: -
SAVEPOINT savepoint1;
Output: The savepoint savepoint1 is set in the current transaction. No immediate output will be
shown.

40
2. Query to implement the rollback:
Code:-
INSERT INTO employees (employee_id, first_name, last_name, salary) VALUES (4, 'Alice', 'Johnson',
80000);
SAVEPOINT savepoint2;
INSERT INTO employees (employee_id, first_name, last_name, salary) VALUES (5, 'Bob', 'Williams',
55000);
ROLLBACK TO SAVEPOINT savepoint2;

Output: - The insertion of the employee with employee_id = 5 is undone. The table will only have
the new employee Alice added.
employee_id first_name last_name salary

1 John Doe 50000

2 Jane Smith 60000

3 Michael Brown 70000

4 Alice Johnson 80000

41
3. Write a Query to implement the commit:
Code: -
INSERT INTO employees (employee_id, first_name, last_name, salary) VALUES
(6, 'Charlie', 'Davis', 45000);
COMMIT;

Output: The insertion of the employee with employee_id = 6 is made permanent in the database.
employee_id first_name last_name salary

1 John Doe 50000

2 Jane Smith 60000

3 Michael Brown 70000

4 Alice Johnson 80000

6 Charlie Davis 45000

42
Experiment No. 10
1. Create user and implement the following commands on relation (Emp and Dept).
2. Develop a query to grant all privileges of employees table into departments table.
3. Develop a query to grant some privileges of employees table into departments table.
4. Develop a query to revoke all privileges of employees table from departments table.
5. Develop a query to revoke some privileges of employees table from departments table.

1. Create user and implement the following commands on relation (Emp and Dept).

Code: -
CREATE USER employee_user IDENTIFIED BY password111;
GRANT CONNECT, RESOURCE TO employee_user;
Grant the necessary privileges to the user
GRANT SELECT, INSERT, UPDATE, DELETE ON Emp TO employee_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON Dept TO employee_user;
DESCRIBE user creation and privileges granted to employee_user on Emp and Dept tables;

Output:
User 'employee_user created successfully.
'CONNECT' and 'RESOURCE' roles granted to 'employee_user'.
'SELECT', 'INSERT', 'UPDATE', and 'DELETE' privileges granted on 'Emp' and 'Dept' tables to
'employee_user'.

2. Develop a query to grant all privileges of employees table into departments table.

Code:-
GRANT ALL PRIVILEGES ON Emp TO Dept;
DESCRIBE granting all privileges from Emp to Dept table;

Output: -
All privileges granted from 'Emp' to 'Dept'. This allows the 'Dept' table full access (SELECT, INSERT,
UPDATE, DELETE) on the 'Emp' table.

3. Develop a query to grant some privileges of employees table into departments table.

Code:-
GRANT SELECT, INSERT ON Emp TO Dept;
DESCRIBE granting specific privileges (SELECT, INSERT) from Emp to Dept table;

Output: -
SELECT and 'INSERT' privileges granted from 'Emp' to 'Dept table. Now the 'Dept' table can only
query and insert data into the Emp' table.

43
4. Develop a query to revoke all privileges of employees table from departments table.

Code:-
REVOKE ALL PRIVILEGES ON Emp FROM Dept;
DESCRIBE revoking all privileges from Dept on Emp table;

Output: -
All privileges (SELECT, INSERT, UPDATE, DELETE) have been revoked from 'Dept' on the 'Emp table.
The 'Dept' table can no longer access or modify data in 'Emp'.

5. Develop a query to revoke some privileges of employees table from departments table.

Code:-
REVOKE SELECT, INSERT ON Emp FROM Dept;
DESCRIBE revoking specific privileges (SELECT, INSERT) from Dept on Emp table;

Output: -
SELECT' and 'INSERT privileges have been revoked from 'Dept' on the 'Emp' table. The Dept table
can no longer query or insert data into the 'Emp' table, but it may still have other privileges (such
as UPDATE or DELETE) if granted previously.

44

You might also like