Skip to content

PL/SQL Control Structures (Practice) πŸš€ΒΆ

BCA Semester 2 RDBMSPL/SQL Control Structures

Recommended order: Semester 2 overview -> RDBMS lab -> PL/SQL control structures -> PL/SQL data handling

Best for: BCA students preparing SQL and PL/SQL practical work

Prerequisites: PL/SQL Basics

Mentor's Note: In the PL/SQL Control Flow Tutorial, we learned the theory. Now, let's solve the 5 core problems from your college assignments! πŸ’‘


🌟 The Scenario: The Office Manager 🏒¢

Imagine you are managing an office database. You need to automate decisions based on employee performance and salary.

  • The Logic:
    • IF an employee earns a lot, mark them as "High Salary" πŸ’Ž.
    • LOOP through a list of IDs to print a batch of reports πŸ“‹.
  • The Result: Instead of manual checks, your database makes decisions automatically! βœ…

πŸ’» Solved Assignment ProblemsΒΆ

1. Calculate Annual SalaryΒΆ

Problem: Accept monthly employee salary and display their total annual salary.

DECLARE
    v_monthly_salary NUMBER(10, 2);
    v_annual_salary  NUMBER(12, 2);
BEGIN
    v_monthly_salary := &Enter_Monthly_Salary;
    v_annual_salary := v_monthly_salary * 12;

    DBMS_OUTPUT.PUT_LINE('Monthly Salary: ' || v_monthly_salary);
    DBMS_OUTPUT.PUT_LINE('Annual Salary: ' || v_annual_salary || ' πŸ’°');
END;
/

2. High or Low Salary CheckΒΆ

Problem: Check whether an employee’s salary is greater than 25,000 and print "High Salary" or "Low Salary".

DECLARE
    v_salary NUMBER(10, 2);
BEGIN
    v_salary := &Enter_Salary;

    IF v_salary > 25000 THEN
        DBMS_OUTPUT.PUT_LINE('Result: High Salary πŸ’Ž');
    ELSE
        DBMS_OUTPUT.PUT_LINE('Result: Low Salary πŸ›’');
    END IF;
END;
/

3. Salary Grade CalculationΒΆ

Problem: Display a grade (A, B, or C) based on an employee's salary range.

DECLARE
    v_salary NUMBER(10, 2);
BEGIN
    v_salary := &Enter_Salary;

    IF v_salary >= 50000 THEN
        DBMS_OUTPUT.PUT_LINE('Grade: A πŸ†');
    ELSIF v_salary >= 30000 THEN
        DBMS_OUTPUT.PUT_LINE('Grade: B ✨');
    ELSE
        DBMS_OUTPUT.PUT_LINE('Grade: C πŸ‘');
    END IF;
END;
/

4. Simple Loop (1 to 10)ΒΆ

Problem: Print numbers from 1 to 10 using a simple LOOP.

DECLARE
    v_counter NUMBER := 1;
BEGIN
    LOOP
        DBMS_OUTPUT.PUT_LINE('Counting: ' || v_counter);
        v_counter := v_counter + 1;
        EXIT WHEN v_counter > 10; -- 🏁 Exit Condition
    END LOOP;
END;
/

5. WHILE Loop (Even Numbers)ΒΆ

Problem: Display the first 10 even numbers using a WHILE loop.

DECLARE
    v_counter NUMBER := 1;
    v_num     NUMBER := 2;
BEGIN
    WHILE v_counter <= 10 LOOP
        DBMS_OUTPUT.PUT_LINE('Even Number ' || v_counter || ': ' || v_num);
        v_num := v_num + 2;
        v_counter := v_counter + 1;
    END LOOP;
END;
/

🎨 Visual Logic: The Loop Assembly Line¢

graph TD
    A[Start Loop 🏁] --> B[i = 1]
    B --> C{i <= 10?}
    C -- Yes --> D[Print Number πŸ“€]
    D --> E[i = i + 1 βš™οΈ]
    E --> C
    C -- No --> F[End Loop 🏁]

πŸ“Š Sample Dry Run (WHILE Loop)ΒΆ

Goal: Print first 3 even numbers

Turn Condition (count <= 3) Number to Print Next Count
1 1 <= 3 (True) βœ… 2 2
2 2 <= 3 (True) βœ… 4 3
3 3 <= 3 (True) βœ… 6 4
4 4 <= 3 (False) ❌ -- Stop! 🏁

πŸ’‘ Interview Tip πŸ‘”ΒΆ

"Interviewers often ask why we use ELSIF instead of multiple IF statements. Answer: ELSIF is more efficient because once a match is found, the computer skips all the other checks!"


πŸ’‘ Pro Tip: "The art of programming is the art of organizing complexity." - Edsger W. Dijkstra


πŸ“ˆ Learning PathΒΆ