30 Simple PL/SQL Interview Questions and Answers
1. What is PL/SQL?
PL/SQL is Oracle's procedural extension for SQL that allows writing full programs to manipulate data in the
database.
2. What is a PL/SQL block?
A PL/SQL block is the basic unit of a PL/SQL program. It contains DECLARE, BEGIN, EXCEPTION, and
END sections.
3. What are the components of a PL/SQL block?
DECLARE, BEGIN, EXCEPTION, END.
4. What is the difference between SQL and PL/SQL?
SQL is a query language; PL/SQL adds procedural features like loops and error handling.
5. What is a variable in PL/SQL?
A variable stores data temporarily. It must be declared before use.
6. How do you declare a variable in PL/SQL?
Example: v_name VARCHAR2(50);
7. What is a constant in PL/SQL?
A variable whose value doesn't change. Example: c_tax CONSTANT NUMBER := 0.18;
8. What is the %TYPE attribute?
Declares a variable using a table column's datatype. Example: v_salary employees.salary%TYPE;
9. What is the %ROWTYPE attribute?
Declares a record that holds an entire row of a table. Example: v_emp employees%ROWTYPE;
10. What is a control structure in PL/SQL?
IF, CASE, LOOP, WHILE - for decision making and iteration.
11. How is an IF statement used in PL/SQL?
IF v_salary > 5000 THEN DBMS_OUTPUT.PUT_LINE('High salary'); END IF;
12. What is a loop in PL/SQL?
Used to repeat statements. Use EXIT WHEN to stop.
13. What is a WHILE loop?
Repeats as long as a condition is true.
14. What is a FOR loop?
Repeats from a lower to upper bound. Example: FOR i IN 1..5 LOOP
15. What is a cursor?
A pointer to the result of a query for row-by-row processing.
16. Difference between implicit and explicit cursor?
Implicit: automatic. Explicit: declared and controlled by the programmer.
17. How do you declare and use an explicit cursor?
Example: CURSOR c_emp IS SELECT * FROM employees;
18. What is exception handling in PL/SQL?
Used to handle runtime errors with EXCEPTION block.
19. What are predefined exceptions?
NO_DATA_FOUND, TOO_MANY_ROWS, ZERO_DIVIDE, etc.
20. What is a procedure?
A named block that performs an action. It does not return a value.
21. How do you create a procedure?
CREATE OR REPLACE PROCEDURE name IS BEGIN ... END;
22. What is a function in PL/SQL?
Similar to a procedure but returns a value.
23. How do you create a function?
CREATE OR REPLACE FUNCTION name RETURN datatype IS BEGIN RETURN ... END;
24. What is a trigger?
A block that runs automatically on a DML event (INSERT, UPDATE, DELETE).
25. What is a package?
A group of procedures, functions, and variables stored together.
26. What is a record in PL/SQL?
A composite type representing a table row.
27. Difference between DELETE and TRUNCATE?
DELETE: logged and rollback possible. TRUNCATE: faster, cannot rollback.
28. How to display output in PL/SQL?
Using DBMS_OUTPUT.PUT_LINE('text');
29. Difference between IN, OUT, and IN OUT parameters?
IN: input only, OUT: output only, IN OUT: input and output.
30. Advantages of PL/SQL?
SQL integration, error handling, performance, and reusable code.