Learn Plsql - 10 Code Examples & CST Typing Practice Test
PL/SQL (Procedural Language/Structured Query Language) is Oracle Corporation's procedural extension to SQL. It combines SQL's data manipulation capabilities with procedural constructs like loops, conditions, and exceptions, enabling complex business logic execution directly within the database.
Learn PLSQL with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
PL/SQL Counter and Theme Toggle
DECLARE
count NUMBER := 0;
isDark BOOLEAN := FALSE;
BEGIN
-- Display initial state
DBMS_OUTPUT.PUT_LINE('Counter: ' || count);
DBMS_OUTPUT.PUT_LINE('Theme: ' || CASE WHEN isDark THEN 'Dark' ELSE 'Light' END);
-- Increment counter
count := count + 1;
DBMS_OUTPUT.PUT_LINE('Counter: ' || count);
-- Toggle theme
isDark := NOT isDark;
DBMS_OUTPUT.PUT_LINE('Theme: ' || CASE WHEN isDark THEN 'Dark' ELSE 'Light' END);
-- Decrement counter
count := count - 1;
DBMS_OUTPUT.PUT_LINE('Counter: ' || count);
-- Reset counter
count := 0;
DBMS_OUTPUT.PUT_LINE('Counter: ' || count);
END;
Demonstrates a simple counter with theme toggling using PL/SQL variables and DBMS_OUTPUT for output.
PL/SQL Simple Addition
DECLARE
a NUMBER := 10;
b NUMBER := 20;
sum NUMBER;
BEGIN
sum := a + b;
DBMS_OUTPUT.PUT_LINE('Sum: ' || sum);
END;
Adds two numbers and prints the result.
PL/SQL Factorial
DECLARE
fact NUMBER := 1;
i NUMBER;
BEGIN
FOR i IN 1..5 LOOP
fact := fact * i;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Factorial: ' || fact);
END;
Calculates factorial of 5 using a loop.
PL/SQL Fibonacci Sequence
DECLARE
fib1 NUMBER := 0;
fib2 NUMBER := 1;
next NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE(fib1);
DBMS_OUTPUT.PUT_LINE(fib2);
FOR i IN 3..10 LOOP
next := fib1 + fib2;
DBMS_OUTPUT.PUT_LINE(next);
fib1 := fib2;
fib2 := next;
END LOOP;
END;
Generates first 10 Fibonacci numbers.
PL/SQL Max of Two Numbers
DECLARE
a NUMBER := 15;
b NUMBER := 25;
max_val NUMBER;
BEGIN
IF a > b THEN
max_val := a;
ELSE
max_val := b;
END IF;
DBMS_OUTPUT.PUT_LINE('Max: ' || max_val);
END;
Finds the maximum of two numbers.
PL/SQL Array Sum
DECLARE
type num_array IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
nums num_array;
sum NUMBER := 0;
BEGIN
nums(1) := 1; nums(2) := 2; nums(3) := 3; nums(4) := 4; nums(5) := 5;
FOR i IN 1..5 LOOP
sum := sum + nums(i);
END LOOP;
DBMS_OUTPUT.PUT_LINE('Sum: ' || sum);
END;
Sums elements of an array.
PL/SQL Even Numbers Filter
DECLARE
type num_array IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
nums num_array;
BEGIN
nums(1) := 1; nums(2) := 2; nums(3) := 3; nums(4) := 4; nums(5) := 5;
FOR i IN 1..5 LOOP
IF MOD(nums(i), 2) = 0 THEN
DBMS_OUTPUT.PUT_LINE('Even: ' || nums(i));
END IF;
END LOOP;
END;
Prints even numbers from a given array.
PL/SQL Conditional Counter Increment
DECLARE
count NUMBER := 3;
BEGIN
IF count < 5 THEN
count := count + 1;
END IF;
DBMS_OUTPUT.PUT_LINE('Count: ' || count);
END;
Increment counter only if it is less than 5.
PL/SQL Resettable Counter
DECLARE
count NUMBER := 0;
BEGIN
-- Increment 3 times
count := count + 1;
count := count + 1;
count := count + 1;
DBMS_OUTPUT.PUT_LINE('Count: ' || count);
-- Reset
count := 0;
DBMS_OUTPUT.PUT_LINE('Count after reset: ' || count);
END;
Counter that increments and can be reset to zero.
PL/SQL Theme Toggle Only
DECLARE
isDark BOOLEAN := FALSE;
BEGIN
DBMS_OUTPUT.PUT_LINE('Theme: ' || CASE WHEN isDark THEN 'Dark' ELSE 'Light' END);
isDark := NOT isDark;
DBMS_OUTPUT.PUT_LINE('Theme: ' || CASE WHEN isDark THEN 'Dark' ELSE 'Light' END);
isDark := NOT isDark;
DBMS_OUTPUT.PUT_LINE('Theme: ' || CASE WHEN isDark THEN 'Dark' ELSE 'Light' END);
END;
Toggles theme state multiple times.
Frequently Asked Questions about Plsql
What is Plsql?
PL/SQL (Procedural Language/Structured Query Language) is Oracle Corporation's procedural extension to SQL. It combines SQL's data manipulation capabilities with procedural constructs like loops, conditions, and exceptions, enabling complex business logic execution directly within the database.
What are the primary use cases for Plsql?
Writing stored procedures and functions. Creating database triggers. Automating batch jobs. Data validation and business rule enforcement. Complex reporting and ETL tasks
What are the strengths of Plsql?
Tightly integrated with Oracle SQL. Enables complex business logic within the database. Reduces network traffic by executing logic server-side. Supports modular and reusable code via packages. Robust error handling and security features
What are the limitations of Plsql?
Mostly Oracle-specific; not portable. Slower for very large data sets compared to external processing. Limited GUI or external integration capabilities. Debugging can be harder without proper tools. Not ideal for non-database-centric applications
How can I practice Plsql typing speed?
CodeSpeedTest offers 10+ real Plsql code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.