0% found this document useful (0 votes)
11 views8 pages

Lab 6 Adms

Lab 06 of CS236 focuses on decision-making in PL/SQL, teaching students about conditional statements such as IF THEN, IF THEN ELSE, and NESTED-IF-THEN. The lab tasks require students to write PL/SQL programs to categorize employees based on their experience, countries based on GDP status, and job roles based on job titles. Students are expected to submit a PDF document containing their SQL queries and execution outcomes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Lab 6 Adms

Lab 06 of CS236 focuses on decision-making in PL/SQL, teaching students about conditional statements such as IF THEN, IF THEN ELSE, and NESTED-IF-THEN. The lab tasks require students to write PL/SQL programs to categorize employees based on their experience, countries based on GDP status, and job roles based on job titles. Students are expected to submit a PDF document containing their SQL queries and execution outcomes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Department of Computer Science

CS236: Advance Database Systems

Class: BSCS-13

Lab 06: Decision Making in PL/SQL


Date: 04-03-2025
Time: 9:45 to 11:55
Instructor: Dr. Ayesha Hakim
Lab Engineer: Syed Muhammad Ali Musa

CS236: Advance Database Systems Page 1


Lab 06: Decision Making in PL/SQL
Introduction
PL/SQL (Procedural Language/Structured Query Language) is Oracle’s extension to SQL that
allows for procedural programming within databases. It features various conditional statements to
control the flow of execution based on specific conditions.

Objectives
The objective of this Lab is to understand the basics of Conditional statements in PL/SQL.

Tools/Software Requirement
SQL Developer
VSCode
or you can directly run your commands in

https://livesql.oracle.com/next/

Description
PL/SQL includes various conditional statements that allow developers to execute different blocks
of code based on specific conditions. Decision-making statements in programming
languages decide the direction of the flow of program execution. Conditional Statements available
in PL/SQL are defined below:

1. IF THEN
2. IF THEN ELSE
3. NESTED-IF-THEN
4. IF THEN ELSIF-THEN-ELSE Ladder

IF THEN
if then the statement is the most simple decision-making statement. It is used to decide whether a
certain statement or block of statements will be executed or not i.e if a certain condition is true
then a block of statement is executed otherwise not.

Example:
-- pl/sql program to illustrate If statement
declare
num1 number:= 10;
num2 number:= 20;

begin

if num1 > num2 then

CS236: Advance Database Systems Page 2


dbms_output.put_line('num1 small');
end if;

dbms_output.put_line('I am Not in if');

end;

IF THEN ELSE
The if statement alone tells us that if a condition is true it will execute a block of statements and if
the condition is false it won’t. But what if we want to do something else if the condition is false.
Here comes the else statement. We can use the else statement with if statement to execute a block
of code when the condition is false.

Example
-- pl/sql program to illustrate If else statement
declare
num1 number:= 10;
num2 number:= 20;

begin

if num1 < num2 then


dbms_output.put_line('i am in if block');

ELSE
dbms_output.put_line('i am in else Block');
end if;

dbms_output.put_line('i am not in if or else Block');


end;

NESTED-IF-THEN
A nested if-then is an if statement that is the target of another if statement. Nested if-then
statements mean an if statement inside another if statement. Yes, PL/SQL allows us to nest if
statements within if-then statements. i.e, we can place an if then statement inside another if then
statement.

Example
-- pl/sql program to illustrate nested If statement
declare
num1 number:= 10;
num2 number:= 20;
num3 number:= 20;

CS236: Advance Database Systems Page 3


begin
if num1 < num2 then
dbms_output.put_line('num1 small num2');

if num1 < num3 then


dbms_output.put_line('num1 small num3 also');
end if;

end if;

dbms_output.put_line('after end if');


end;

Lab Task
1) Write a PL/SQL program to find out how long an employee has been working at the
company.
• Check their hire date from the EMPLOYEES table.
• Compare it with today's date (Hint: SYSDATE).
• Based on their experience, display one of the following messages:
a. "New Employee" If they’ve been with the company for less than a year.
b. "Experienced Employee" If they’ve worked for 1 to 5 years.
c. "Senior Employee" If they’ve been there for more than 5 years.

CODE:

DECLARE

v_first_name HR.EMPLOYEES.FIRST_NAME%TYPE;
v_last_name HR.EMPLOYEES.LAST_NAME%TYPE;
v_employee_id HR.EMPLOYEES.EMPLOYEE_ID%TYPE;
v_hire_date HR.EMPLOYEES.HIRE_DATE%TYPE;
v_experience NUMBER;

BEGIN

v_employee_id := &Enter_Employee_ID;

--Retrieving Information from Tables


SELECT FIRST_NAME , LAST_NAME , HIRE_DATE INTO v_first_name , v_last_name , v_hire_date
FROM HR.EMPLOYEES
WHERE EMPLOYEE_ID = v_employee_id;

--Calculating Per Year Experience ( TRUNC or truncate (remove the decimal part) of a number or date.)
v_experience := TRUNC ( MONTHS_BETWEEN ( SYSDATE , v_hire_date ) / 12 );

DBMS_OUTPUT.PUT_LINE ( 'EMPLOYEE NAME: ' || v_first_name || ' ' || v_last_name );


DBMS_OUTPUT.PUT_LINE ( 'Years Worked: ' || v_experience );

CS236: Advance Database Systems Page 4


IF v_experience < 1 THEN
DBMS_OUTPUT.PUT_LINE ( 'NEW EMPLOYEE ');
ELSIF v_experience BETWEEN 1 AND 5 THEN
DBMS_OUTPUT.PUT_LINE ( ' EXPERIENCED EMPLOYEE ');
ELSE
DBMS_OUTPUT.PUT_LINE ( ' SENIOR EMPLOYEE ');

END IF;

EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with employee ID ' || v_employee_id );
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;

OUTPUT:

2) In this task you have to categorize countries based on their GDP status using the
COUNTRIES table in the HR schema. The goal is to check the GDP category of a given
country and print the result like this.
a. US, DE, JP (Developed Country)
b. IN, CN, BR (Emerging Economy Others)
c. Others (Developing Countries)

CODE:

DECLARE
v_Country_Id HR.COUNTRIES.COUNTRY_ID%TYPE;
Country_Name HR.COUNTRIES.COUNTRY_NAME%TYPE;

BEGIN

v_Country_Id := TO_CHAR('&Enter_Country_Id');

SELECT COUNTRY_NAME
INTO Country_Name
FROM HR.COUNTRIES
WHERE COUNTRY_ID = v_Country_Id;

DBMS_OUTPUT.PUT_LINE('Country Name: ' || Country_Name);

CS236: Advance Database Systems Page 5


IF v_Country_Id = 'US' OR v_Country_Id = 'DE' OR v_Country_Id = 'JP' THEN
DBMS_OUTPUT.PUT_LINE('Developed Country');
ELSIF v_Country_Id = 'IN' OR v_Country_Id = 'CN' OR v_Country_Id = 'BR' THEN
DBMS_OUTPUT.PUT_LINE('Emerging Economy');
ELSE
DBMS_OUTPUT.PUT_LINE('Developing Countries');
END IF;

EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No country found with the provided Country ID.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
/

OUTPUT:

CS236: Advance Database Systems Page 6


3) In this task, you have to categorize job roles based on their JOB_TITLE from the JOBS
table in the HR schema. The goal is to check the job title and classify it into one of three
categories:
a) Leadership Role If JOB_TITLE contains "Manager"
b) Technical Role If JOB_TITLE contains "Engineer" or "Developer"
c) Other Role If JOB_TITLE doesn’t match the above conditions

CODE:

DECLARE

v_job_id HR.JOBS.JOB_ID%TYPE;

v_job_title HR.JOBS.JOB_TITLE%TYPE;

BEGIN

v_job_id := TO_CHAR ('&Enter_Job_ID');

SELECT JOB_TITLE INTO v_job_title

FROM HR.JOBS

WHERE JOB_ID = v_job_id;

IF v_job_title LIKE '%Manager%' THEN

DBMS_OUTPUT.PUT_LINE(v_job_title || ' - Leadership Role');

ELSIF v_job_title LIKE '%Engineer%' OR v_job_title LIKE '%Developer%' THEN

DBMS_OUTPUT.PUT_LINE(v_job_title || ' - Technical Role');

ELSE

DBMS_OUTPUT.PUT_LINE(v_job_title || ' - Other Role');

END IF;

END;

/
CS236: Advance Database Systems Page 7
OUTPUT:

Deliverables:
Submit a PDF document including the SQL queries to answer above-mentioned information needs
as well as snapshot of their outcome when executed over MySQL using the Workbench.

CS236: Advance Database Systems Page 8

You might also like