preloader
PL/SQL Interview Questions

Top 50+ PL/SQL Interview Questions and Answers, Prepare Now

author image

PL/SQL is a popular topic to be asked in technical interviews. So for your benefit here we bring you Top 50+ PL/SQL interview questions and answers. Practice these PL/SQL interview questions to crack the interview questions. Here we have tried to cover PL/SQL basic interview questions, PL/SQL technical interview questions, and PL/SQL coding questions which can be asked during the interview.

About PL/SQL: PL/SQL full form is Procedural Language extensions to SQL (Structured Query Language). It was made by Oracle to overcome the issues of SQL and easily build and handling of crucial applications in a comprehensive manner.

PL/SQL Interview Questions

1. What is PL SQL?

2. What is a PL/SQL table?

3. What is the basic structure of PL/SQL?

4. Tell the use of WHERE CURRENT OF in cursors?

5. How will you assign a name to an unnamed PL/SQL Exception Block?

6. Define Trigger? Name some instances of using “Triggers”.

7. When does a DECLARE block of PL/SQL become mandatory?

8. What are the different comments in a PL/SQL code and how do you write it?

9. Tell the use of the WHEN clause in the trigger?

10. Define sequences?

11. Write a PL/SQL procedure for selecting some records from the database using some parameters as filters.

12. For what database links are used?

13. Write a PL/SQL code to find whether a given string is palindrome or not.

14. Write a PL/SQL program to convert each digit of a given number into its corresponding word format.

15. Write a PL/SQL program to find the sum of digits of a number.

16. Show some predefined exceptions.

17. How to find an open cursor in a PL SQL Block?

18. Tell any 2 PL/SQL cursor exceptions.

19. What are the available PL SQL developer packages?

20. Explain 3 basic parts of a trigger.

21. What does fetching a cursor do?

22. What does closing a cursor do?

23. Explain Consistency

24. Differentiate Anonymous blocks and sub-programs.

25. Differentiate DECODE and CASE.

26. Explain autonomous transactions.

27. Difference between SGA and PGA.

28. Where do you find Pre_defined_functions.

29. Explain polymorphism.

30. Uses of MERGE?

31. Can we execute 2 queries together in a Distributed Database System?

32. Explain Raise_application_error.

33. How do you convert date into Julian date format?

34. Explain the SPOOL command

35. What does the PL/SQL package contain?

36. What are the methods to trace the PL/SQL code?

37. What does a hierarchical profiler do?

38. What does PLV msg do?

39. What does the PLV (PL/Vision) package offer?

40. Use of PLVprs and PLVprsps?

41. How to copy a file to file content and file to PL/SQL table?

42. How are exceptions handled in advance PL/SQL?

43. What are the issues that occur while writing log information to a database table?

44. What is the function used to transfer a PL/SQL table log to a database table?

45. Why is PLVtab the easiest to access the PL/SQL table?

46. What does PLVtab enable to show the contents of PL/SQL tables?

47. How can you save messages in a table?

48. Use of the “module procedure” function in PL/SQL?

49. What do PLVcmt and PLVrb do in PL/SQL?

50. Name some character functions?


Learn More Interview Questions Here:


PL SQL Interview Questions and Answers

1. What is PL SQL?

It is a procedural language that interacts with SQL and constructs like conditional branching and iteration.

2. What is a PL/SQL table?

PL/SQL tables are objects of type tables that are displayed as database tables. It is a way to provide arrays that are temporary tables in memory for faster processing. These PL/SQL tables are used in moving bulk data and simplifying the process.

3. What is the basic structure of PL/SQL?

PL/SQL follows the basic BLOCK structure. Each code contains SQL and PL/SQL statements that together make a PL/SQL block.

Each PL/SQL block has these 3 sections:

  • The optional Declaration Section
  • The mandatory Execution Section
  • The optional Exception handling Section

[DECLARE]

--declaration statements (optional)

BEGIN

--execution statements

[EXCEPTION]

--exception handling statements (optional)

END;

4. Tell the use of WHERE CURRENT OF in cursors?

This clause represents the current row from an explicit cursor. This clause applies updates and deletes the row which is currently under consideration without mentioning the row ID.

Syntax:

UPDATE table_name SET field=new_value WHERE CURRENT OF cursor_name

5. How will you assign a name to an unnamed PL/SQL Exception Block?

By using Pragma called EXCEPTION_INIT we can name a PL/SQL Exception Block. It allows the programmer to command the compiler to provide custom error messages based on business logic by overtaking the predefined messages during the compilation time.

Syntax:

DECLARE
  exception_name EXCEPTION;
  PRAGMA EXCEPTION_INIT (exception_name, error_code);
BEGIN
// PL/SQL Logic
EXCEPTION
 WHEN exception_name THEN
	// Steps to handle exception
END;

6. Define Trigger? Name some instances of using “Triggers”.

As the name says, ‘Trigger’ means to start or ‘activate’ something. In PL/SQL, it is a stored procedure that tells what action the database will take when an event related to it is performed.

· Syntax:

TRIGGER trigger_name

trigger_event

[ restrictions ]

BEGIN

actions_of_trigger;

END;

Some instances where Triggers can be used:

  • To sustain complex integrity constraints.
  • For auditing any PL/SQL table information.
  • We also use triggers to signal other actions upon completion of the change whenever changes are done in a table.
  • Implement complex rules of business.
  • To prevent invalid transactions.

7. When does a DECLARE block of PL/SQL become mandatory?

An anonymous block of PL/SQL uses that statement in non-stored and stand-alone procedures. The statement always comes first in the stand-alone file.

8. What are the different comments in a PL/SQL code and how do you write it?

Sentences that do not have any effect on the functionality and are only used for the core purpose of enhancing the readability are called Comments. They are of two types:

  • Single Line Comment: By using the symbol – and writing the comment next to it is way you can create a single line comment
  • Multi-Line comment: To add multiple lines comments use the syntax like /* comment information */

Example:

SET SERVEROUTPUT ON;

DECLARE

-- Hi There! I am a single line comment.

var_name varchar2(40) := ‘I love PL/SQL’ ;

BEGIN

/*

Hi! I am a multi line

comment. I span across

multiple lines

*/

dbms_output.put_line(var_name);

END;

/

Output:

I love PL/SQL

9. Tell the use of the WHEN clause in the trigger?

WHEN clause tells you the trigger has to be activated

10. Define sequences?

Sequences help in making sequence numbers without an overhead of locking. The major drawback is if the transaction is rolled back the sequence numbers are lost.

11. Write a PL/SQL procedure for selecting some records from the database using some parameters as filters.

CREATE PROCEDURE get_employee_details @salary nvarchar(30)

AS

BEGIN

SELECT * FROM ib_employee WHERE salary = @salary;

END;

Database links are made for communication between various databases, or environments like running tests, development, and production. They are for read-only purposes to access essential information.

13. Write a PL/SQL code to find whether a given string is palindrome or not.

DECLARE
-- Declared variables string, letter, reverse_string where string is the original string.
string VARCHAR2(10) := 'abccba';
letter VARCHAR2(20);
reverse_string VARCHAR2(10);
BEGIN
FOR i IN REVERSE 1..LENGTH(string) LOOP
 letter := SUBSTR(string, i, 1);
 -- concatenate letter to reverse_string variable
 reverse_string := reverse_string ||''||letter;
END LOOP;
IF reverse_string = string THEN
       dbms_output.Put_line(reverse_string||''||' is palindrome');
ELSE
   	dbms_output.Put_line(reverse_string ||'' ||' is not palindrome');
END IF;
END;

14. Write a PL/SQL program to convert each digit of a given number into its corresponding word format.

DECLARE
-- declare necessary variables
   -- num represents the given number
   -- number_to_word represents the word format of the number
   -- str, len and digit are the intermediate variables used for program execution
num   INTEGER;
number_to_word VARCHAR2(100);
digit_str   VARCHAR2(100);
len   INTEGER;
digit   INTEGER;
BEGIN
   num := 123456;
   len := LENGTH(num);
   dbms_output.PUT_LINE('Input: ' ||num);
   -- Iterate through the number one by one
   FOR i IN 1..len LOOP
   	digit := SUBSTR(num, i, 1);
   	-- Using DECODE, get the str representation of the digit
   	SELECT Decode(digit, 0, 'Zero ',
                   	1, 'One ',
                   	2, 'Two ',
                 	  3, 'Three ',
                   	4, 'Four ',
                   	5, 'Five ',
                   	6, 'Six ',
                   	7, 'Seven ',
                   	8, 'Eight ',
                   	9, 'Nine ')
   	INTO digit_str
   	FROM dual;
   	-- Append the str representation of digit to final result.
   	number_to_word := number_to_word || digit_str;
   END LOOP;
   dbms_output.PUT_LINE('Output: ' ||number_to_word);
END;
Input: 12345
Output: One Two Three Four Five

15. Write a PL/SQL program to find the sum of digits of a number.

DECLARE
--Declare variables num, sum_of_digits and remainder of datatype Integer
num  INTEGER;
sum_of_digits INTEGER;
remainder  INTEGER;
BEGIN
num := 123456;
sum_of_digits := 0;
-- Find the sum of digits until original number doesn't become null
WHILE num <> 0 LOOP
 remainder := MOD(num, 10);
 sum_of_digits := sum_of_digits + remainder;
 num := TRUNC(num / 10);
END LOOP;
dbms_output.PUT_LINE('Sum of digits is '|| sum_of_digits);
END;
Input: 9874
Output: 28

16. Show some predefined exceptions.

DUP_VAL_ON_INDEX

ZERO_DIVIDE

NO_DATA_FOUND

TOO_MANY_ROWS

CURSOR_ALREADY_OPEN

INVALID_NUMBER

INVALID_CURSOR

PROGRAM_ERROR

TIMEOUT _ON_RESOURCE

STORAGE_ERROR

LOGON_DENIED

VALUE_ERROR

etc.

17. How to find an open cursor in a PL SQL Block?

Use the %ISOPEN cursor status variable.

18. Tell any 2 PL/SQL cursor exceptions.

Cursor_Already_Open
Invaid_cursor

19. What are the available PL SQL developer packages?

DBMS_ series of packages, such as, DBMS_PIPE, DBMS_LOCK, DBMS_DDL, DBMS_OUTPUT, DBMS_ALERT, DBMS_UTILITY, DBMS_JOB, DBMS_TRANSACTION, UTL_FILE, DBMS_SAL.

20. Explain 3 basic parts of a trigger.

  • A triggering statement or event.
  • A restriction
  • An action

21. What does fetching a cursor do?

Fetching a cursor goes through the Result Set row by row.

22. What does closing a cursor do?

Closing a cursor frees the private SQL area and de-allocates memory.

23. Explain Consistency

Consistency displays the data which will not be shown to other users until it is committed, so that consistency is maintained properly.

24. Differentiate Anonymous blocks and sub-programs.

Anonymous blocks are unnamed and not stored anywhere whereas sub-programs are compiled at runtime and stored in a database.

25. Differentiate DECODE and CASE.

DECODE and CASE statements are the same but the difference is CASE is an extended version of DECODE. DECODE won’t allow Decision-making statements in its place.

select decode(totalsal=12000,’high’,10000,’medium’) as decode_tesr from smp where smpno in (10,12,14,16);

This statement returns an error.

CASE is used in PL SQL, but DECODE is used in PL SQL through SQL only.

26. Explain autonomous transactions.

It is an independent transaction of the parent transaction. It is not nested if initiated by another transaction. Event logging and auditing are some situations to use autonomous transactions.

27. Difference between SGA and PGA.

SGA means System Global Area and PGA means Program or Process Global Area. PGA is given 10% RAM size, whereas SGA is allocated 40% RAM size.

28. Where do you find Pre_defined_functions.

“Functions, Procedures and Packages” is the standard package where you can find the Pre_defined_functions.

29. Explain polymorphism.

Polymorphism is a feature of OOP. It creates a variable, an object, or function with multiple forms. PL/SQL enables Polymorphism in the program units which overloads inside a member function or package. Unambiguous logic needs to be avoided when overloading is being done.

30. Uses of MERGE?

MERGE combines multiple DML statements into one.

Syntax: merge into table name

using(query)

on(join condition)

when not matched then

[insert/update/delete] command

when matched then

[insert/update/delete] command

31. Can we execute 2 queries together in a Distributed Database System?

Yes, we can execute 2 queries at the same time out of which one needs to be independent of the second query in a distributed database system based on the 2 phase commit.

32. Explain Raise_application_error.

It is a procedure of the DBMS_STANDARD package that enables issuing user_defined error messages from the database trigger or stored sub-program.

33. How do you convert date into Julian date format?

By using the J format string:

  1. SQL > select to_char(to_date(’29-Mar-2013′,’dd-mon-yyyy’),’J’) as Julian from dual;
  2. JULIAN

34. Explain the SPOOL command

Spool command can bring the output of SQL statements and print it in a file.

  1. spool/tmp/sql_outtxt
  2. select smp_name, smp_id from SMP where dept=’accounts’;
  3. spool off;

35. What does the PL/SQL package contain?

  • PL/SQL table and record TYPE statements
  • Procedures and Functions
  • Cursors
  • Variables ( tables, scalars, records, etc.) and constants
  • Exception names and pragmas for relating an error number with an exception
  • Cursors

36. What are the methods to trace the PL/SQL code?

Tracing code is a critical technique to calculate the code performance during its runtime. Different methods for tracing include

  • DBMS_APPLICATION_INFO
  • DBMS_TRACE
  • DBMS_SESSION and DBMS_MONITOR
  • trcsess and tkproof utilities

37. What does a hierarchical profiler do?

The hierarchical profiler can profile the calls made in PL/SQL. It fills the gap between the loopholes and the expectations of performance tracing.

  • SQL and PL/SQL time consumption reporting
  • Reports count of distinct sub-programs calls made in the PL/SQL, and the time spent with each subprogram call
  • By using the command-line utility you can have multiple interactive analytics reports in HTML format.
  • Effective than conventional profilers.

38. What does PLV msg do?

  • Allocate individual text messages to a defined row in the PL/SQL table
  • It recovers the message text by number
  • It automatically replaces your standard Oracle error messages with restricted toggle
  • Bulk load message numbers and text from a database table directly

39. What does the PLV (PL/Vision) package offer?

  • Null substitution value
  • Set of assertion routines
  • Miscellaneous utilities
  • Set of constants used throughout PL vision
  • Pre-defined data types

40. Use of PLVprs and PLVprsps?

  • PLVprs: It is an extension and the lowest level of string parsing for PL/SQL functionality

  • PLVprsps: It is the highest level package of parsing PL/SQL source code into separate atomics. It depends on other parsing packages to get work done.

41. How to copy a file to file content and file to PL/SQL table?

If you use a single program call – “fcopy procedure”, you can copy the content of a file to another. The “file2pstab” program is used to copy the contents of a file directly into a PL/SQL table.

42. How are exceptions handled in advance PL/SQL?

To handle exceptions PL/SQL has an effective plugin that is PLVexc. It allows different exception handling actions.

  • Continue processing
  • Record and then continue
  • Halt processing
  • Record and then halt processing

43. What are the issues that occur while writing log information to a database table?

The main issue that occurs when you write log information to a database table is that the information is available only once the new rows are committed to the database. This is a problem as PLVlog is usually positioned to track errors and in many instances, the current transaction fails to track or needs a rollback.

44. What is the function used to transfer a PL/SQL table log to a database table?

“PROCEDURE ps2db” function is used to transfer a PL/SQL table log to a database log table.

45. Why is PLVtab the easiest to access the PL/SQL table?

The PL/SQL tables are closest to and to access this table you need to declare a table type and PL/SQL table. But PLVtab helps you to avoid defining your PL/SQL table type and make PL/SQL data-table access easy.

46. What does PLVtab enable to show the contents of PL/SQL tables?

PAVtab does different things when the contents of PL/SQL tables are shown.

  • Display a header for the table
  • Display the row numbers for the table values
  • Display a prefix before each row of the table

47. How can you save messages in a table?

There are two ways to save a message in a table:

  • Use add_text procedure to load individual messages with calls

  • Use load_from_dbms procedure to load sets of messages from a database table

48. Use of the “module procedure” function in PL/SQL?

The “module procedure” changes or converts all the lines of code in a definite program unit with one procedure call. The main three arguments for module procedures are:

  • module_in
  • cor_in
  • Last_module_in

49. What do PLVcmt and PLVrb do in PL/SQL?

PLVcmt and PLVrb are the two packages that manage the transaction processes in PL/SQL applications.

  • PLVcmt: It wraps logic and complexity to manage commit processing

  • PLVrb: It is a programmatic interface to roll-back activity

50. Name some character functions?

INITCAP, UPPER, SUBSTR, LOWER, and LENGTH are some important character functions.

You can cover the above given PL/SQL interview questions and answers to ace your interview. In the above section, we have tried to cover all questions related to the PL/SQL system which was asked in the Interview of freshers and professionals. If you have any questions or need help related to interview questions and answers on PL/SQL then feel free to reach us.

Want to prepare for these languages:

Recent Articles