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

Oracle PLSQL Additional Code Based Questions - Solution

Uploaded by

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

Oracle PLSQL Additional Code Based Questions - Solution

Uploaded by

pj6974527
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Final Check

Oracle PLSQL

The following schema represents “ABC” corporation data’s regarding Projects, Employees
and Employees working in/worked on Projects.
Table Name : Employee
Stores details of employees of “ABC” Corp.
Name Data Type Constraints Comments
Emp_Id Number(5) Primary Key Employee Number

Emp_Name Varchar2(20) Not Null Name of the Employee


Hire_Date Date Date the employee was hired
Salary Number(8,2) Not Null Employees Salary
EmpMgr Number(5) Foreign Key to Employee’s manager, who
Employee(Emp_Id) may not be the same as the
project manager
Table Name : Project
Stores details about all the projects handled by “ABC” Corporation.
Name Data Type Constraints Comments
Proj_Id Varchar2(4) Primary Key Project Number
Proj_Name Varchar2(30) Not Null Name of the project
Proj_Mgr Number(5) Foreign Key to Emp_ID who manages the
Employee(Emp_Id) project
Proj_StartDate Date Project Start date
Proj_EndDate Date Project End date
Proj_Status Varchar2(5) Check Status of the project, Should
be either Open or Close
Table Name : Works_In
Stores the details of employees working in a project. An employee can work on more than
one project. A project can have many employees working on it. Employees who are
currently working on a project do not have the End_Date recorded (since they are still
working on the project). Employees can discontinue working on a project before completion
of that project.
Name Data Type Constraints Comments
Proj_Id Varchar2(4) Foreign Key to Project Number
Project(Proj_Id)
Emp_Id Number(5) Foreign Key to Employee Number
Employee(Emp_Id)
Start_Date Date Not Null Indicates the start date of the
project
End_Date Date Project completion date
Hours Number(3) Not Null Total Number of Hours spent
by the employee for that
project
Rating Number(1) Check Indicates the performance,
Should be either 1 or 2 or 3.
Note . : Proj_Id and Emp_Id is a composite Primary Key

Q. CREATE ABOVE TABLE STRUCTURE AND ADD 5 RECORDS INTO EACH TABLES

Ans:

drop table Employee cascade constraints;


drop table Project cascade constraints;
drop table Works_In cascade constraints;

create table Employee(


Emp_Id number(5) primary key,
Emp_Name varchar2(20) not null,
Hire_Date date,
Salary number(8,2) not null,
Empmgr number(5) references Employee(Emp_Id)
);

create table Project(


Proj_Id varchar2(4) Primary key,
Proj_Name varchar2(30) not null,
Proj_Mgr number(5) references Employee(Emp_Id),
Proj_StartDate date,
Proj_EndDate date,
Proj_Status varchar2(5) check(upper(Proj_Status) in ('OPEN','CLOSE'))
);

create table Works_In(


Proj_Id varchar2(4) references Project(Proj_Id),
Emp_Id number(5) references Employee(Emp_Id),
Start_Date date not null,
End_Date date,
Hours number(3) not null,
Rating number(1) check(Rating in (1,2,3)),
constraint idpk primary key(Proj_Id,Emp_Id)
);

insert into Employee values(10211,'Ajeesh',sysdate-730,100000,null);


insert into Employee values(10123,'Saravanan', sysdate-365, 20000, 10211);
insert into Employee values(10056,'Mohana',sysdate-440, 28000, 10211 );
insert into Employee values(10022,'Makke',sysdate-500,35000,10211);
insert into Employee values(10213,'Balu',sysdate-300,18000,10022);
insert into Employee values(10250,'Jambu',sysdate-275,18000,10211);
insert into Employee values(10001,'Sindhu',sysdate-550,40000,10022);

insert into Project values('CSS','Customer Support System', 10211, sysdate-555, null,


'OPEN');
insert into Project values('SAR', 'SARA-Retail', 10250, sysdate-400, null, 'OPEN');
insert into Project values('SAW', 'SARA-Wholesale', 10211, sysdate-415, null, 'Open');
insert into Project values('JTP','Jambu Telecom', 10001, sysdate-600, sysdate-15,'close');
insert into Project values('CCO', 'Chinnappa Comm', 10001, sysdate-300, null,'Open');

insert into Works_In values('CSS', 10123,sysdate-365,null,26,3);


insert into Works_In values('CSS', 10211,sysdate-255,null,18,3);
insert into Works_In values('SAR', 10056,sysdate-400, sysdate-55, 30, 1);
insert into Works_In values('SAR', 10022,sysdate-200, null, 14,1);
insert into Works_In values('SAW', 10022,sysdate-415, null, 25,1);
insert into Works_In values('SAW', 10250,sysdate-275, sysdate-10,45,2);
insert into Works_In values('SAW', 10211,sysdate-300, null,26,2);
insert into Works_In values('JTP', 10001,sysdate-550, sysdate-15, 28, 1);
insert into Works_In values('JTP', 10250,sysdate-100, sysdate-15, 10, 2);
insert into Works_In values('CCO', 10001,sysdate-300, null, 18,1);
insert into Works_In values('CCO', 10022,sysdate-60,null, 25,1);

1. Write a procedure named Update_Project_Status that takes a project ID (Proj_Id) as


input and updates the status of the project to 'Closed' if the project end date
(Proj_EndDate) is earlier than the current date. If the project end date is null,
display an appropriate message.

Ans:
CREATE OR REPLACE PROCEDURE Update_Project_Status(p_Proj_Id IN VARCHAR2)
AS
BEGIN
UPDATE Project
SET Proj_Status = 'Closed'
WHERE Proj_Id = p_Proj_Id
AND (Proj_EndDate IS NOT NULL AND Proj_EndDate < SYSDATE);

IF SQL%ROWCOUNT = 0 THEN
DBMS_OUTPUT.PUT_LINE('No projects found with the given ID or the project
end date is in the future.');
ELSE
DBMS_OUTPUT.PUT_LINE('Project status updated successfully.');
END IF;
END;
/

2. Create a function named Calculate_Total_Hours that takes an employee ID (Emp_Id)


as input and returns the total number of hours spent by that employee on all
projects.

Ans:
CREATE OR REPLACE FUNCTION Calculate_Total_Hours(p_Emp_Id IN NUMBER)
RETURN NUMBER AS
v_Total_Hours NUMBER := 0;
BEGIN
SELECT SUM(Hours)
INTO v_Total_Hours
FROM Works_In
WHERE Emp_Id = p_Emp_Id;

RETURN v_Total_Hours;
END;
/

3. Write a cursor named Employee_Info_Cursor to fetch the details of all employees


along with their hire dates and salaries.

Ans:
CREATE OR REPLACE PROCEDURE Employee_Info_Cursor AS
CURSOR emp_cursor IS
SELECT Emp_Id, Emp_Name, Hire_Date, Salary
FROM Employee;
BEGIN
FOR emp_rec IN emp_cursor LOOP

DBMS_OUTPUT.PUT_LINE(emp_rec.Emp_Id || ', ' || emp_rec.Emp_Name || ', ' ||


emp_rec.Hire_Date || ', ' || emp_rec.Salary);
END LOOP;
CLOSE emp_cursor;
END;
/

4. Develop a procedure named Display_Project_Info that takes a project ID (Proj_Id) as


input and displays the project name, start date, end date, and status.

Ans:
CREATE OR REPLACE PROCEDURE Display_Project_Info(p_Proj_Id VARCHAR2) AS
v_Proj_Name Project.Proj_Name%TYPE;
v_Start_Date Project.Proj_StartDate%TYPE;
v_End_Date Project.Proj_EndDate%TYPE;
v_Status Project.Proj_Status%TYPE;
BEGIN
SELECT Proj_Name, Proj_StartDate, Proj_EndDate, Proj_Status
INTO v_Proj_Name, v_Start_Date, v_End_Date, v_Status
FROM Project
WHERE Proj_Id = p_Proj_Id;

DBMS_OUTPUT.PUT_LINE('Project Name: ' || v_Proj_Name);


DBMS_OUTPUT.PUT_LINE('Start Date: ' || v_Start_Date);
DBMS_OUTPUT.PUT_LINE('End Date: ' || NVL(TO_CHAR(v_End_Date, 'DD-MON-
YYYY'), 'Ongoing'));
DBMS_OUTPUT.PUT_LINE('Status: ' || v_Status);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No project found with the given ID.');
END;
/

5. Create a function named Calculate_Project_Duration that calculates the duration of a


project in months based on its start and end dates. If the end date is null, assume
the project is ongoing.

Ans:
CREATE OR REPLACE FUNCTION Calculate_Project_Duration(p_Start_Date DATE,
p_End_Date DATE) RETURN NUMBER AS
v_Months NUMBER;
BEGIN
IF p_End_Date IS NULL THEN
v_Months := MONTHS_BETWEEN(SYSDATE, p_Start_Date);
ELSE
v_Months := MONTHS_BETWEEN(p_End_Date, p_Start_Date);
END IF;

RETURN v_Months;
END;
/

6. Write a cursor named Employee_Projects_Cursor to retrieve the names of projects


along with their start and end dates where a specific employee (given Emp_Id) has
worked.

Ans:
CREATE OR REPLACE PROCEDURE Employee_Projects_Cursor(p_Emp_Id NUMBER)
AS
CURSOR proj_cursor IS
SELECT p.Proj_Name, w.Start_Date, w.End_Date
FROM Project p
JOIN Works_In w ON p.Proj_Id = w.Proj_Id
WHERE w.Emp_Id = p_Emp_Id;
proj_rec proj_cursor%ROWTYPE;
BEGIN
OPEN proj_cursor;
LOOP
FETCH proj_cursor INTO proj_rec;
EXIT WHEN proj_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(proj_rec.Proj_Name || ', ' || proj_rec.Start_Date || ',
' || NVL(TO_CHAR(proj_rec.End_Date, 'DD-MON-YYYY'), 'Ongoing'));
END LOOP;
CLOSE proj_cursor;
END;
/

7. Develop a procedure named Assign_Employee_To_Project that takes an employee ID


(Emp_Id) and a project ID (Proj_Id) as input and inserts a record into the Works_In
table with the current date as the start date.

Ans:
CREATE OR REPLACE PROCEDURE Assign_Employee_To_Project(p_Emp_Id NUMBER,
p_Proj_Id VARCHAR2) AS
BEGIN
INSERT INTO Works_In (Proj_Id, Emp_Id, Start_Date)
VALUES (p_Proj_Id, p_Emp_Id, SYSDATE);
DBMS_OUTPUT.PUT_LINE('Employee ' || p_Emp_Id || ' has been assigned to
project ' || p_Proj_Id || ' with start date ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY'));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error assigning employee to project: ' || SQLERRM);
END;
/

8. Create a function named Get_Project_Manager that takes a project ID (Proj_Id) as


input and returns the name of the project manager.

Ans:
CREATE OR REPLACE FUNCTION Get_Project_Manager(p_Proj_Id VARCHAR2)
RETURN VARCHAR2 AS
v_Manager_Name Employee.Emp_Name%TYPE;
BEGIN
SELECT e.Emp_Name
INTO v_Manager_Name
FROM Employee e
JOIN Project p ON e.Emp_Id = p.Proj_Mgr
WHERE p.Proj_Id = p_Proj_Id;

RETURN v_Manager_Name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
/
9. Write a cursor named Employee_Rating_Cursor to calculate the average rating of all
projects an employee (given Emp_Id) has worked on.

Ans:
CREATE OR REPLACE PROCEDURE Employee_Rating_Cursor(p_Emp_Id NUMBER) AS
CURSOR rating_cursor IS
SELECT AVG(Rating)
FROM Works_In
WHERE Emp_Id = p_Emp_Id;
avg_rating NUMBER;
BEGIN
OPEN rating_cursor;
FETCH rating_cursor INTO avg_rating;
CLOSE rating_cursor;

DBMS_OUTPUT.PUT_LINE('Average rating of projects for employee ' || p_Emp_Id


|| ': ' || avg_rating);
END;
/

10. Develop a procedure named Delete_Project_Records that deletes all records from the
Works_In table associated with a specific project ID (Proj_Id).

Ans:
CREATE OR REPLACE PROCEDURE Delete_Project_Records(p_Proj_Id VARCHAR2) AS
BEGIN
DELETE FROM Works_In
WHERE Proj_Id = p_Proj_Id;
DBMS_OUTPUT.PUT_LINE('Records associated with project ' || p_Proj_Id || ' have
been deleted.');
END;
/

11. Create a Package named Proj_Pack which contains a private member function
mentioned in A and a public procedure mentioned in B.

A. Write a private function named Avg_Rating which accepts Project_Id as a


parameter and check for the existence of the specified Project_Id in the
table named “Works_In”. If found then return the Average rating
of employees working on the specified project, Otherwise return 0.

B. Write a public procedure named Disp_Avg_Rating which invokes the


function named “Avg_Rating” and displays the Average Rating
returned by the invoked function.

Ans:
CREATE OR REPLACE PACKAGE Proj_Pack AS
-- Private member function
FUNCTION Avg_Rating(p_Project_Id VARCHAR2) RETURN NUMBER;
-- Public procedure
PROCEDURE Disp_Avg_Rating(p_Project_Id VARCHAR2);
END Proj_Pack;
/

CREATE OR REPLACE PACKAGE BODY Proj_Pack AS


-- Private member function
FUNCTION Avg_Rating(p_Project_Id VARCHAR2) RETURN NUMBER IS
v_AvgRating NUMBER := 0;
BEGIN
SELECT AVG(Rating) INTO v_AvgRating
FROM Works_In
WHERE Proj_Id = p_Project_Id;

RETURN v_AvgRating;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN 0;
END Avg_Rating;

-- Public procedure
PROCEDURE Disp_Avg_Rating(p_Project_Id VARCHAR2) IS
v_AvgRating NUMBER;
BEGIN
v_AvgRating := Avg_Rating(p_Project_Id);

IF v_AvgRating = 0 THEN
DBMS_OUTPUT.PUT_LINE('Project ' || p_Project_Id || ' not found or no
ratings available.');
ELSE
DBMS_OUTPUT.PUT_LINE('Average rating for project ' || p_Project_Id || ': '
|| v_AvgRating);
END IF;
END Disp_Avg_Rating;
END Proj_Pack;
/

12. Create a procedure named Add_Emp_Works to insert a record into the table
named “Works_In” with the following parameters Proj_Id, Emp_Id, Start_Date,
End_Date, Hours and Rating.

Perform the following validation:

a. Proj_id should exist in the table named “Project”.


b. Emp_Id should exist in the table named “Employee”.
c. Start_Date should be >= Project.Proj_StartDate for the specified
project.

d. End_Date can be either > Start_Date and <= Project.Proj_EndDate


or it can have a “NULL” value.
e. Hours should be a positive integer value > 0.
f. Rating should be between 1 to 3.
g. Insert the record in the table named “Works_in” if all the above
validations are met and display a message “1 Record inserted”.

h. If any of the validations is not satisfied then raise an appropriate


exception by displaying an appropriate message.

i. Exception Handling.
Ans:

CREATE OR REPLACE PROCEDURE Add_Emp_Works(


p_Proj_Id VARCHAR2,
p_Emp_Id NUMBER,
p_Start_Date DATE,
p_End_Date DATE,
p_Hours NUMBER,
p_Rating NUMBER
) AS
BEGIN
-- Validation: Proj_Id should exist in the Project table
SELECT COUNT(*)
INTO v_Proj_Count
FROM Project
WHERE Proj_Id = p_Proj_Id;

IF v_Proj_Count = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Project with ID ' || p_Proj_Id || ' does
not exist.');
END IF;

-- Validation: Emp_Id should exist in the Employee table


SELECT COUNT(*)
INTO v_Emp_Count
FROM Employee
WHERE Emp_Id = p_Emp_Id;

IF v_Emp_Count = 0 THEN
RAISE_APPLICATION_ERROR(-20002, 'Employee with ID ' || p_Emp_Id || '
does not exist.');
END IF;
-- Validation: Start_Date should be >= Project.Proj_StartDate for the specified
project
SELECT Proj_StartDate
INTO v_Proj_StartDate
FROM Project
WHERE Proj_Id = p_Proj_Id;

IF p_Start_Date < v_Proj_StartDate THEN


RAISE_APPLICATION_ERROR(-20003, 'Start date cannot be earlier than project
start date.');
END IF;

-- Validation: End_Date can be either > Start_Date and <= Project.Proj_EndDate


or it can have a NULL value
IF p_End_Date IS NOT NULL THEN
IF p_End_Date <= p_Start_Date THEN
RAISE_APPLICATION_ERROR(-20004, 'End date must be later than start
date.');
END IF;

SELECT Proj_EndDate
INTO v_Proj_EndDate
FROM Project
WHERE Proj_Id = p_Proj_Id;

IF p_End_Date > v_Proj_EndDate THEN


RAISE_APPLICATION_ERROR(-20005, 'End date cannot be later than project
end date.');
END IF;
END IF;

-- Validation: Hours should be a positive integer value > 0


IF p_Hours <= 0 THEN
RAISE_APPLICATION_ERROR(-20006, 'Hours must be a positive integer
value.');
END IF;

-- Validation: Rating should be between 1 to 3


IF p_Rating < 1 OR p_Rating > 3 THEN
RAISE_APPLICATION_ERROR(-20007, 'Rating must be between 1 and 3.');
END IF;

-- Insert record into Works_In table


INSERT INTO Works_In (Proj_Id, Emp_Id, Start_Date, End_Date, Hours, Rating)
VALUES (p_Proj_Id, p_Emp_Id, p_Start_Date, p_End_Date, p_Hours, p_Rating);

DBMS_OUTPUT.PUT_LINE('1 Record inserted.');


EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLCODE || ' - ' || SQLERRM);
END Add_Emp_Works;
/

SQL Queries:

1.
Retrieve the names and hire dates of all employees from the Employee table.

Ans: SELECT Emp_Name, Hire_Date


FROM Employee;

2.
List the names of all employees along with the names of the projects they are currently
working on.

Ans:
SELECT e.Emp_Name, p.Proj_Name
FROM Employee e
LEFT JOIN Works_In w ON e.Emp_Id = w.Emp_Id
LEFT JOIN Project p ON w.Proj_Id = p.Proj_Id;

3.
Find the average salary of all employees.

Ans:
SELECT AVG(Salary)
FROM Employee;

4.
Display the names and hire dates of employees hired after January 1, 2020, sorted by hire
date.

Ans:
SELECT Emp_Name, Hire_Date
FROM Employee
WHERE Hire_Date > TO_DATE('2020-01-01', 'YYYY-MM-DD')
ORDER BY Hire_Date;

5.
List the names of employees who are project managers.
Ans:
SELECT Emp_Name
FROM Employee
WHERE Emp_Id IN (SELECT Proj_Mgr FROM Project);

6.
Update the salary of an employee with Emp_Id 1001 to 85000.
Ans:

UPDATE Employee
SET Salary = 85000
WHERE Emp_Id = 1001;

7.
Insert a new project with Proj_Id "PRJ1", Proj_Name "New Project", Proj_Mgr 1002,
Proj_StartDate '2024-04-01', Proj_EndDate '2024-06-01', and Proj_Status 'Open'.

Ans:
INSERT INTO Project (Proj_Id, Proj_Name, Proj_Mgr, Proj_StartDate, Proj_EndDate,
Proj_Status)
VALUES ('PRJ1', 'New Project', 1002, TO_DATE('2024-04-01', 'YYYY-MM-DD'),
TO_DATE('2024-06-01', 'YYYY-MM-DD'), 'Open');

8.
Retrieve the project names along with the total number of employees working on each
project.

Ans:
SELECT p.Proj_Name, COUNT(w.Emp_Id) AS Total_Employees
FROM Project p
LEFT JOIN Works_In w ON p.Proj_Id = w.Proj_Id
GROUP BY p.Proj_Id, p.Proj_Name;

9.
Calculate the average rating of projects with a status of 'Close'.

Ans:
SELECT AVG(w.Rating)
FROM Works_In w
JOIN Project p ON w.Proj_Id = p.Proj_Id
WHERE p.Proj_Status = 'Close';
10.
Delete all records from the Works_In table where the End_Date is NULL and the
Start_Date is before '2023-01-01'.

Ans:
DELETE FROM Works_In
WHERE End_Date IS NULL AND Start_Date < TO_DATE('2023-01-01', 'YYYY-MM-DD');

10.
Retrieve the names of employees along with the names of the projects they have worked
on, including projects that have no associated employees.

Ans:
SELECT e.Emp_Name, COALESCE(p.Proj_Name, 'No Project') AS Project_Name
FROM Employee e
LEFT JOIN Works_In w ON e.Emp_Id = w.Emp_Id
LEFT JOIN Project p ON w.Proj_Id = p.Proj_Id;

11.
List the names of employees who have worked on projects managed by employee with
Emp_Id 1003.

Ans:
SELECT e.Emp_Name
FROM Employee e
JOIN Works_In w ON e.Emp_Id = w.Emp_Id
JOIN Project p ON w.Proj_Id = p.Proj_Id
WHERE p.Proj_Mgr = 1003;

12.
Find the names of employees who are currently not assigned to any project.

Ans:
SELECT Emp_Name
FROM Employee
WHERE Emp_Id NOT IN (SELECT Emp_Id FROM Works_In);

13.
Display the names of employees, their project names, and the project manager names
they are currently working under.
Ans:
SELECT e.Emp_Name, p.Proj_Name, e_mgr.Emp_Name AS Manager_Name
FROM Employee e
LEFT JOIN Works_In w ON e.Emp_Id = w.Emp_Id
LEFT JOIN Project p ON w.Proj_Id = p.Proj_Id
LEFT JOIN Employee e_mgr ON p.Proj_Mgr = e_mgr.Emp_Id;

14.
List the names of projects along with the total number of employees assigned to each
project.

Ans:
SELECT p.Proj_Name, COUNT(w.Emp_Id) AS Total_Employees
FROM Project p
LEFT JOIN Works_In w ON p.Proj_Id = w.Proj_Id
GROUP BY p.Proj_Id, p.Proj_Name;

15.
Retrieve the names of employees along with the average rating of the projects they have
worked on.

Ans:
SELECT e.Emp_Name, AVG(w.Rating) AS Average_Rating
FROM Employee e
JOIN Works_In w ON e.Emp_Id = w.Emp_Id
GROUP BY e.Emp_Id, e.Emp_Name;

16.
Find the names of employees who have worked on projects managed by employees from
the same department.

Ans:
SELECT e.Emp_Name
FROM Employee e
JOIN Works_In w ON e.Emp_Id = w.Emp_Id
JOIN Project p ON w.Proj_Id = p.Proj_Id
JOIN Employee e_mgr ON p.Proj_Mgr = e_mgr.Emp_Id
WHERE e.Dept_Id = e_mgr.Dept_Id;

17.
Display the names of employees and their respective managers' names.
Ans:
SELECT e.Emp_Name, e_mgr.Emp_Name AS Manager_Name
FROM Employee e
LEFT JOIN Employee e_mgr ON e.EmpMgr = e_mgr.Emp_Id;

18.
List the names of employees along with their salaries compared to the average salary of
all employees.

Ans:
SELECT e.Emp_Name, e_mgr.Emp_Name AS Manager_Name
FROM Employee e
LEFT JOIN Employee e_mgr ON e.EmpMgr = e_mgr.Emp_Id;

19.
Retrieve the names of employees along with the names of projects they have worked on,
where the project end date is within the next month.

Ans:
SELECT e.Emp_Name, p.Proj_Name
FROM Employee e
JOIN Works_In w ON e.Emp_Id = w.Emp_Id
JOIN Project p ON w.Proj_Id = p.Proj_Id
WHERE p.Proj_EndDate >= TRUNC(SYSDATE) AND p.Proj_EndDate <
ADD_MONTHS(TRUNC(SYSDATE), 1);

You might also like