10X Sale
kh logo
All Courses

Introduction

PL SQL is a procedural language that is designed for SQL statements within its syntax. Oracle database server compiles the PL SQL program units and is kept inside a database. Whether you are a beginner or an intermediate or an experienced PL SQL professional, this guide will aid you in increasing your confidence and knowledge of PL/SQL. The below write-up comprises a systematic and sequential set of 95 PL/SQL interview questions and answers with 35 of them being of basic-level, 34 intermediate-level, and 26 advanced-level. These will help you get your dream job as a PL/SQL Developer. With PL SQL interview questions, you can be confident on preparing well for your upcoming interview.

PL SQL Interview Questions and Answers
Beginner

1. What is PL/SQL?

This is a frequently asked question in PL SQL interview questions.  

PL/SQL is an extension for SQL and Oracle. It enhances the capabilities of SQL. Therefore, with every release of the Oracle Database, the feature and functionalities of PL/SQL are enhanced.

It is available in Oracle Database since version 6. However, PL/SQL is also available in TimesTen in-memory database since version 11.2.1 and IBM DB2 since version 9.7.

PL/SQL is strongly integrated with SQL and supports both static and dynamic SQL.

The following are the features in PL/SQL:

  • PL/SQL is a procedural language that extends SQL.
  • PL/SQL has a built-in interpreted programming environment.
  • PL/SQL's syntax has shaped ADA and Pascal programming languages.
  • Structured programming is possible with PL/SQL through functions and procedures.
  • It has Block Structures as well as nest block structures. Executes as a whole block.
  • PL/SQL has object-oriented features in it.
  • PL/SQL also comes with the functionality of Triggers. Triggers are stored in a database and fired when some event occurs. The event here can be a Database Manipulation statement i.e. DELETE, INSERT, or UPDATE, or a Database Definition statement (CREATE, ALTER, or DROP).
  • PL/SQL is a standard language for Oracle development.
  • PL/SQL is used to write program blocks, functions, procedures, triggers, etc.
  • It has cursors. Oracle forms a memory area when an SQL statement is processed. This memory area is called the context area. A cursor in PL/SQL is a pointer to this context area. It has information about processing the statement.

2. PL/SQL vs T/SQL

PL/SQL is an extension for SQL and Oracle. It enhances the capabilities of SQL. Therefore, with every release of the Oracle Database, the feature and functionalities of PL/SQL is enhanced.

It is available in Oracle Database since version 6. However, PL/SQL is also available in TimesTen in-memory database since version 11.2.1 and IBM DB2 since version 9.7.

TL/SQL is Transact Structure Query language. T-SQL expands on the SQL standard to add procedural programming, local variables, etc. It is Microsoft's and Sybase's proprietary extension to the SQL.

The following are the differences:

Basis
PL/SQL
T/SQL
Efficiency
It is more powerful than T/SQL.
T/SQL is less powerful than PL/SQL.
Relation with SQL
PL/SQL is an extension for SQL and Oracle. It enhances the capabilities of SQL.
T-SQL expands on the SQL standard to add procedural programming, local variables, etc.
Full Form
PL SQL is Procedural Language Structural Query Language.
TL SQL is Transact Structure Query language.
Compatibility
PL-SQL works best with Oracle database Server.
T-SQL works best with Microsoft SQL Server
Easier/ Complex
PL/SQL is considered complex to learn than
T/SQL
It is easier to learn and work with T/SQL.
Developer
It is developed and owned by Oracle.
T/SQL is Microsoft's and Sybase's proprietary extension to the SQL.


3. PL/SQL vs SQL

PL/SQL is an extension for SQL and Oracle. It enhances the capabilities of SQL. Therefore, with every release of the Oracle Database, the feature and functionalities of PL/SQL is enhanced. It is available in Oracle Database since version 6. However, PL/SQL is also available in TimesTen in-memory database since version 11.2.1 and IBM DB2 since version 9.7.

SQL is a domain-specific language used in programming. It is a Structured Query Language for database creation, deletion, fetching rows, modifying rows, etc. relational database management system

The following are the differences:

Basis
PL/SQL
SQL
Embed
Can have SQL embedded in it.
Cannot have PL/SQL code embedded in it.
Interaction with database server
There is no interaction with the database server.
Interaction with the database server.
How/What
PL/SQL defines how it needs to be done.
Defines what needs to be done.
Writing program/functions/procedures
PL/SQL is used to write program blocks,
functions, procedures triggers, etc.
SQL is used to write queries, DDL and DML statements.
Execution
Executes as a whole block.
Issue a single query or execute a single insert/update/delete. Executes one statement at a time.

4. What are the characteristics of PL/SQL

PL/SQL is an extension for SQL and Oracle. It enhances the capabilities of SQL. Therefore, with every release of the Oracle Database, the feature and functionalities of PL/SQL is enhanced. It is strongly integrated with SQL and supports both static and dynamic SQL.

The following are the characteristics of PL/SQL:

  • PL/SQL is a procedural language that extends SQL.
  • PL/SQL has a built-in interpreted programming environment.
  • PL/SQL's syntax is shaped ADA and Pascal programming language.
  • It is an Extension to SQL
  • Structured programming is possible with PL/SQL through functions and procedures.
  • It has Block Structures as well as nest block structures. Executes as a whole block.
  • PL/SQL has object-oriented features in it.
  • Provides High Performance for Applications
  • Triggers are stored in a database and fired when some event occurs. The event here can be a Database Manipulation statement i.e. DELETE, INSERT, or UPDATE, or a Database Definition statement (CREATE, ALTER, or DROP).
  • Productivity to Programmers
  • PL/SQL is a standard language for Oracle development.
  • PL/SQL is used to write program blocks, functions, procedures triggers, etc.
  • It has cursors. Oracle forms a memory area, when an SQL statement is processed. This memory area is called context area. A cursor in PL/SQL is a pointer to this context area. It has information about processing the statement.
  • PL/SQL also comes with the functionality of Triggers.

5. Why do we use Cursors in PL/SQL?

Expect to come across this popular question in PL SQL questions.  

Oracle forms a memory area when an SQL statement is processed. This memory area is called the context area. A cursor in PL/SQL is a pointer to this context area. It has information about processing the statement. A PL/SQL cursor holds the rows (one or more) returned by a SQL statement.

The following are the two forms of cursors:

  • Implicit Cursors (Automatic)

Implicit cursors in PL/SQL are created automatically by Oracle when an SQL statement is executed. This occurs when there is no explicit cursor for the statement.

An implicit cursor is associated when INSERT, UPDATE, and DELETE is issued. The attributes include %FOUND, %ISOPEN, %NOTFOUND, etc. With the attributes, you can check which rows got affected by the operation.

The attributes:

  • %FOUND: It returns TRUE if an INSERT, UPDATE, or DELETE statement affected one or more rows or a SELECT INTO statement returned one or more rows. Otherwise, it returns FALSE.
  • %NOTFOUND: The logical opposite of %FOUND. It returns TRUE if an INSERT, UPDATE, or DELETE statement affected no rows, or a SELECT INTO statement returned no rows. Otherwise, it returns FALSE.
  • %ISOPEN: It returns FALSE for implicit cursors because Oracle closes the SQL cursor automatically after executing its associated SQL statement.
  • %ROWCOUNT: It returns the number of rows affected by an INSERT, UPDATE, or DELETE statement or returned by a SELECT INTO statement.

To use any of the above attributes, use the following syntax:

sql%attribute_name

Above, set any attribute name under “attribute_name”.Let us consider the following table for our example:

<DEPTBUDGET>
+-----------+-----------------+---------------+------------------+
| DEPTID | DEPTNAME  | DEPTLOC | BUDGET|
+----------+------------------+---------------+------------------+
|  1 | Finance          | NORTH | 56678      |
|  2 | Marketing     | EAST |      87687 |
|  3 | Operations   | WEST |      95979 |
|  4 | Technical      | SOUTH |    76878 |
|  5 | Accounting   | NORTHWEST| 86767      |
+----------+------------------+------------------+--------------+

Now, we will update the budget of all the departments and then check what all rows got affected using the implicit cursor attribute. With that, we will use other attributes as well:

DECLARE  
  rows_count number(2);
BEGIN
  UPDATE deptbudget
  SET budget = budget + 3000;
  IF sql%notfound THEN
     dbms_output.put_line('Nothing selected!');
  ELSIF sql%found THEN
     rows_count := sql%rowcount;
     dbms_output.put_line( rows_count || ' rows affected! ');
  END IF;  
END;
/    

The output:

5 rows affected!

Since we updated the budget for all the departments above, our table rows would look like this:

<DEPTBUDGET>
+-----------+-----------------+---------------+------------------+
| DEPTID | DEPTNAME  | DEPTLOC | BUDGET|
+----------+------------------+---------------+------------------+
|  1 | Finance          | NORTH | 59678      |
|  2 | Marketing     | EAST |      90687 |
|  3 | Operations   | WEST |      98979 |
|  4 | Technical      | SOUTH |    79878 |
|  5 | Accounting   | NORTHWEST| 89767      |
+----------+------------------+------------------+--------------+
  • Explicit Cursors (Programmer Defined)

Explicit cursors are custom defined i.e. programmer-defined cursors that give more control over the context area. Oracle forms a memory area when an SQL statement is processed, which is called a context area.

The following is the syntax to create an explicit cursor:

CURSOR name_of_cursor IS select_statement;

Here,
“name_of_cursor” is the cursor name

As you can see above, an explicit cursor is created on a SELECT statement. Let us now see how to work with Explicit Cursors,

  • Cursor Declaration

Let us declare a cursor and set a cursor name with the SELECT statement:

CURSOR d_deptbudget IS
  SELECT deptid, deptname, deptloc, budget FROM deptbudget;

Above, “d_deptbudget” is our cursor.

  • Open the Cursor

The memory gets allocated for the cursor when it is opened in PL/SQL. Opening the cursor allocates the memory for the cursor and makes it ready for fetching the rows returned by the SQL statement into it.

OPEN d_deptbudget;
  • Fetch the Cursor

Access one row at a time when a cursor is fetched in PL/SQL. The following is when you fetch a cursor that is already opened:

FETCH d_deptbudget INTO d_deptid, d_deptname, d_deptloc, d_budget;
  • Close the Cursor

When a cursor is closed, then the allocated memory is released. To close the above-created cursor:

CLOSE d_deptbudget;

Let us now see an example of working with explicit cursors in PL/SQL:

DECLARE  
  d_deptid deptbudget.deptid%type;  
  d_deptname deptbudget.deptname%type;    
  CURSOR d_deptbudget IS  
     SELECT deptid, deptname FROM deptbudget;
BEGIN  
  OPEN d_deptbudget;  
  LOOP  
     FETCH d_deptbudget into d_deptid, d_deptname;  
     EXIT WHEN d_deptbudget%notfound;  
     dbms_output.put_line(d_deptid || ' ' || d_deptname);  
  END LOOP;  
  CLOSE d_deptbudget;  
END;  
/  

The output display department id and name:

1 Finance
2 Marketing
3 Operations
4 Technical
5 Accounting

Want to Know More?
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Description

PL/SQL, known as "Procedural Language extensions to SQL", is Oracle Corporation's procedural extension for SQL and the Oracle relational database.

Normally, Oracle Corporation increases PL/SQL functionality with every release of the Oracle Database. PL/SQL allows the programmer to write code in a procedural format, which is different in the case of SQL. It serves the purpose of combining the SQL’s data manipulation power with the processing power of the procedural language, which ends up generating extremely well-built SQL queries. It drives the compiler on what needs to be done through SQL and how it can be done via its procedural method. The built-in functions, provided by Oracle Corporation themselves, enhance the user-defined functions. It utilises loops, conditions, and object-oriented concepts, thus offering the programmers more control. This process is similar to that of other database languages.

As a fresher in PL/SQL development, one will begin with an entry-level database developer job or a job related to database administration while working for a company utilising Oracle solutions. After working as a junior developer for quite some time, you may move up the ladder to the role of a lead developer and will be leading a team of developers. Once you gain enough supervisory experience, you can apply for more senior positions. A senior DBA or Business Intelligence Architect is the one that comes to mind, or you can move into database consulting. Following this career path can also open opportunities for you to work in the medical or banking industries, and more so, provided you gain more experience in the relevant line of work. Some of them are as follows:

  • Analyst role
  • Unix Developer/Administrator
  • DBA
  • PL/SQL performance optimization developer
  • Pro*C developer
  • SQL developer
  • PL/SQL administrator
  • ETL developer
  • Informatica Developer
  • DB2 professional

According to ziprecruiter.com, the average annual salary of a PL/SQL Developer in the USA is $106,219. Payscale.com states that in India, the figure goes to Rs. 504,255 for an Oracle Developer, Rs. 435,613 for a Software Developer, and Rs 415,289 for a Software Engineer. The major companies hiring PL/SQL Developers in India are Oracle, Capgemini, Infosys Limited, IBM India, Accenture, Epsilon, and Wipro.

There are plenty of job opportunities available for a skilled PL/SQL Developer. What you will need to crack a PL/SQL interview is decisive planning along with extreme precision to answering the tricky and tough questions. Do not worry. We are there to help you.

We are well aware of what a candidate goes through at an interview. Many interview questions can make one tensed and nervous, resulting in answering them incorrectly, also because of the mental trauma one goes through at that time. However, our experts have systematically compiled a set of PL/SQL interview questions that will not only guide you to know how you will answer any possible question in a clear manner without any confusion but also give you an edge over the other candidates to excel in any PL/SQL job interview.

So why wait any longer? Go through our PL/SQL interview questions to crack any PL/SQL job interview and get the dream job that you have been longing to get.

All the best!

Recommended Courses

Learners Enrolled For