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

Lab 07 Loops

This document outlines Lab 06 for the CS236 Advanced Database Systems course, focusing on loops in PL/SQL. It covers the types of loops available in PL/SQL, including Basic, WHILE, FOR, and Nested loops, along with examples for each. The lab tasks require students to display employee names and salaries, find the highest paid job title, and link employee data with job history, with deliverables including SQL queries and execution snapshots.

Uploaded by

Abdullah Naeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab 07 Loops

This document outlines Lab 06 for the CS236 Advanced Database Systems course, focusing on loops in PL/SQL. It covers the types of loops available in PL/SQL, including Basic, WHILE, FOR, and Nested loops, along with examples for each. The lab tasks require students to display employee names and salaries, find the highest paid job title, and link employee data with job history, with deliverables including SQL queries and execution snapshots.

Uploaded by

Abdullah Naeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Department of Computer Science

CS236: Advance Database Systems

Class: BSCS-13

Lab 06: Loops in PL/SQL


Date: 11-03-2025
Time: 9:45 to 11:55 and 12:00 to 2:25
Instructor: Dr. Ayesha Hakim
Lab Engineer: Syed Muhammad Ali Musa

CS236: Advance Database Systems Page 1


Lab 06: Loops in PL/SQL
Introduction
PL/SQL (Procedural Language/Structured Query Language) is Oracle’s extension to SQL that
allows for procedural programming within databases. Loops are one of the essential constructs in
PL/SQL that enable executing a block of statements multiple times, either for a fixed number of
iterations or until a condition is satisfied.

Objectives
The objective of this Lab is to understand the basics of Looping 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 provides different types of loops to repeatedly execute a block of statements. Loops are
fundamental in automating repetitive tasks, processing data sets, and performing iterative
calculations.
Types of Loops in PL/SQL
1. Basic Loop
2. WHILE Loop
3. FOR Loop
4. Nested Loops

Basic Loop Structure


A basic loop repeats a sequence of statements until an exit condition is met using the exit or exit
when statement.

Example

DECLARE
counter NUMBER := 1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE('Counter: ' || counter);
counter := counter + 1;
EXIT WHEN counter > 5;
END LOOP;
END;
CS236: Advance Database Systems Page 2
While loop
The WHILE loop evaluates a condition before executing the loop’s body. If the condition is
TRUE, the loop executes; if it’s FALSE, the loop terminates.

Example
DECLARE
counter NUMBER := 1;
BEGIN
WHILE counter <= 5 LOOP
DBMS_OUTPUT.PUT_LINE('Counter: ' || counter);
counter := counter + 1;
END LOOP;
END;

For Loop
The FOR loop is used when the number of iterations is known in advance. It automatically
increments the loop counter.

Example
BEGIN
FOR counter IN 1..5 LOOP
DBMS_OUTPUT.PUT_LINE('Counter: ' || counter);
END LOOP;
END;

Nested Loops
Loops can be nested inside one another to perform complex iterations over multi-dimensional
data.

Example
BEGIN
FOR i IN 1..3 LOOP
FOR j IN 1..2 LOOP
DBMS_OUTPUT.PUT_LINE('i = ' || i || ', j = ' || j);
END LOOP;
END LOOP;
END;

CS236: Advance Database Systems Page 3


Lab Task
1) Display All Employee Names and Their Salaries. (Use Only For Loop)
2) Find Highest Paid Job Title Based on MAX_SALARY
Hint:
Loop through all JOBS and
 Find the Job Title with the highest MAX_SALARY
 Display the Job Title and its MAX_SALARY
3) Link Employee table with the Job history table and Loop through each record in job
history table and display Employee ID, Job ID, and Duration (in months) along with
employee name.

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 4

You might also like