Write PL/SQL block to display students who have a grade of A. Write PL/SQL block to delete all students who are older than 18 years. Write PL/SQL block to retrieve the Name and Gra... Write PL/SQL block to display students who have a grade of A. Write PL/SQL block to delete all students who are older than 18 years. Write PL/SQL block to retrieve the Name and Grade of all students. Write PL/SQL block to sort Student id in reverse order. Write PL/SQL block to calculate factorial of numbers. Write PL/SQL block to print 9, 18, 27, 36, 45, ..., 90 number series.

Question image

Understand the Problem

The question is asking for PL/SQL blocks to perform various database operations, including deleting students based on age, retrieving specific attributes of students, sorting IDs, and calculating factorials of numbers. Each point outlines a specific task to be accomplished using PL/SQL.

Answer

PL/SQL blocks provided for displaying, deleting, retrieving, sorting, factorials, and series printing as requested.

Here are the PL/SQL blocks for each task:

  1. Display students with grade 'A':
DECLARE
BEGIN
    FOR student IN (SELECT * FROM Students WHERE Grade = 'A')
    LOOP
        dbms_output.put_line('ID: ' || student.ID || ', Name: ' || student.Name);
    END LOOP;
END;
  1. Delete students older than 18:
BEGIN
    DELETE FROM Students WHERE Age > 18;
    COMMIT;
END;
  1. Retrieve Name and Grade of all students:
DECLARE
BEGIN
    FOR student IN (SELECT Name, Grade FROM Students)
    LOOP
        dbms_output.put_line('Name: ' || student.Name || ', Grade: ' || student.Grade);
    END LOOP;
END;
  1. Sort Student ID in reverse order:
DECLARE
BEGIN
    FOR student IN (SELECT * FROM Students ORDER BY ID DESC)
    LOOP
        dbms_output.put_line('ID: ' || student.ID || ', Name: ' || student.Name);
    END LOOP;
END;
  1. Calculate factorial of numbers:
DECLARE
    n NUMBER := 5; --for example
    fact NUMBER := 1;
BEGIN
    FOR i IN 1..n LOOP
        fact := fact * i;
    END LOOP;
    dbms_output.put_line('Factorial of ' || n || ' is ' || fact);
END;
  1. Print 9, 18, 27, ..., 90 series:
DECLARE
    i NUMBER;
BEGIN
    i := 9;
    WHILE i <= 90 LOOP
        dbms_output.put_line(i);
        i := i + 9;
    END LOOP;
END;
Answer for screen readers

Here are the PL/SQL blocks for each task:

  1. Display students with grade 'A':
DECLARE
BEGIN
    FOR student IN (SELECT * FROM Students WHERE Grade = 'A')
    LOOP
        dbms_output.put_line('ID: ' || student.ID || ', Name: ' || student.Name);
    END LOOP;
END;
  1. Delete students older than 18:
BEGIN
    DELETE FROM Students WHERE Age > 18;
    COMMIT;
END;
  1. Retrieve Name and Grade of all students:
DECLARE
BEGIN
    FOR student IN (SELECT Name, Grade FROM Students)
    LOOP
        dbms_output.put_line('Name: ' || student.Name || ', Grade: ' || student.Grade);
    END LOOP;
END;
  1. Sort Student ID in reverse order:
DECLARE
BEGIN
    FOR student IN (SELECT * FROM Students ORDER BY ID DESC)
    LOOP
        dbms_output.put_line('ID: ' || student.ID || ', Name: ' || student.Name);
    END LOOP;
END;
  1. Calculate factorial of numbers:
DECLARE
    n NUMBER := 5; --for example
    fact NUMBER := 1;
BEGIN
    FOR i IN 1..n LOOP
        fact := fact * i;
    END LOOP;
    dbms_output.put_line('Factorial of ' || n || ' is ' || fact);
END;
  1. Print 9, 18, 27, ..., 90 series:
DECLARE
    i NUMBER;
BEGIN
    i := 9;
    WHILE i <= 90 LOOP
        dbms_output.put_line(i);
        i := i + 9;
    END LOOP;
END;

More Information

These PL/SQL blocks handle specific operations related to student data and mathematical calculations, demonstrating key programming constructs like loops and conditionals.

Tips

Ensure correct use of loops and conditions. Order by DESC for reverse sorting.

AI-generated content may contain errors. Please verify critical information

Thank you for voting!
Use Quizgecko on...
Browser
Browser