0% found this document useful (0 votes)
924 views25 pages

ORACLE PL/SQL Interview Questions You'll Most Likely Be Asked

ORACLE PL/SQL Interview Questions You’ll Most Likely Be Asked is a perfect companion to stand a head above the rest in today’s competitive job market. Rather than going through comprehensive, textbook-sized reference guides, this book includes only the information required immediately for job search to build a career as a ORACLE PL/SQL Professional. If you think this book just covers important topics in brief, then you are mistaken! It covers questions those are based on project knowledge and experience gained on successful high-profile ORACLE implementers. This book puts the interviewee in the driver’s seat and helps them steer their way to impress the interviewer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
924 views25 pages

ORACLE PL/SQL Interview Questions You'll Most Likely Be Asked

ORACLE PL/SQL Interview Questions You’ll Most Likely Be Asked is a perfect companion to stand a head above the rest in today’s competitive job market. Rather than going through comprehensive, textbook-sized reference guides, this book includes only the information required immediately for job search to build a career as a ORACLE PL/SQL Professional. If you think this book just covers important topics in brief, then you are mistaken! It covers questions those are based on project knowledge and experience gained on successful high-profile ORACLE implementers. This book puts the interviewee in the driver’s seat and helps them steer their way to impress the interviewer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

ORACLE PL/SQL

Questions
Review these typical interview questions and think about how you would
answer them. Read the answers listed; you will find best possible answers
along with strategies and suggestions.
This page is intentionally left blank
Chapter 1

General-Theory

1: What is PL / SQL? What are its advantages?


Answer:
PL/SQL or Procedural Language extension of SQL is a method to
implement the procedural features of a programming language
using structured query language. To enhance the competencies of
SQL, Oracle Corporation developed PL/SQL. The major purpose
of PL/SQL is to combine programming language and SQL to
smoothen the functioning of a program using SQL.
Several Advantages of using PL/SQL are:
a) Streamlined approach: Each PL/SQL programming code
contains a block of code giving it a structured approach, as
well as the block of code, can be used anywhere in the
program.
b) Conditional Loops: The major advantage of using PL/SQL
is that programming can include loops and other
conditional statements, which helps in the perfect flow of
a program as well as works on multiple entries.
c) Effective performance: The Pl/SQL codes are highly
portable, capable of error-handling hence the performance
is better and even multiple codes can be worked
simultaneously, in a reduces traffic too.
d) Exception handling: PL/ SQL is very effective in error
handling. During the program execution, an exception can
be caught and a user can be notified immediately to
consider the error.
e) Secured: PL/SQL uses stored procedures; hence they are
very secured to use.
f) Easy to Learn: PL/SQL is easy to learn and flexible to use
with its simple code usage.

2: Describe some characteristics of PL / SQL.


Answer:
PL/SQL is highly compatible and flexible to use in any program,
making it highly applicable for all purposes. The major
characteristics of PL/SQL include:
a) PL/SQL is highly portable making it compatible with any
platform.
b) Object-oriented programming is supported which is a
major advantage especially since most of the
programming languages are Object Oriented.
c) Supports web servers and web programming which is a
must in a time when everything goes online.
d) Uses structured programming methods which are easier to
comprehend.
e) Facilitate usage of numerous data types which makes it
compatible with a wide range of data.
f) Exclusive error checking which accounts for the quality
programs those perform without fail.

3: What are the features of PL / SQL?


Answer:
PL/SQL has many features:
a) PL/SQL can be integrated with SQL to let flexibility in
coding with data manipulation, transaction control
statements etc.
b) The error handling mechanism is too strong in PL/SQL as
it is easy to catch an error exemption.
c) PL/SQL supports different data types.
d) Different program structures can be used in PL/SQL.
e) Structured programming is one feature of Pl/SQL where in
many procedures and functions can be used.
f) OOPs or Object oriented programming concept is very
much used in PL/SQL.
g) More extensively used in web development in the client-
server models.

4: What are the sections of a PL/SQL block of code?


Answer:
A PL/SQL block of code are the program units which defines the
program functions. An anonymous block is the one without a
name and is not saved in the database. The syntax of a block in
PL/SQL is:
DECLARE ( All variable and other declarations come under this
section)
Declaration statement;
BEGIN ( All executable statements, processes are under this
section)
Execution statements;
EXCEPTION ( All exception handling done here)
Exception Handling statements;
END;

In the above structure;


Declaration section contains the variables, data types, and
structures to be declared and used in the function. The declaration
must contain the type and the size and initial values of the
declared variables. Execution section in the block must at least
contain one statement for execution or a logic code. The code can
be either procedural or SQL. The exception keyword starts the
exception handling statement, with a code to handle exceptions.
The statement contains a catch or a handling code exception.

5: Differentiate between Decode and Case.


Answer:
Decode and Case are two conditional statements used in PL/SQL.
To differentiate both case and Decode:
a) Case can work only with all other conditional operators

conditional operator.
b) Case can work with searchable subqueries and predicates,
while decode works only scalar values.
c) A Case statement can be effectively substituted as If-then-
else statement as well as a parameter to any procedure
call, but Decode can only be used inside the function.
d) Case and Decode handles Null values differently
e) Case is ANSI compatible while Decode is not.
f) Case cannot handle data type inconsistency.
g) Decode statements are easily readable as compared to
Case.

6: What is a MERGE?
Answer:
The Merge statement is used to insert or update a data at certain
situations. Merge statements help to reduce the time taken to scan
the table, as well as in parallel, do the operation. The Merge
statement can do insert, update, delete simultaneously with the
single statement. As per the usage, the merge statements can be
used like merge into, merge update etc.

7: What is SPOOL?
Answer:
The spool command is used to direct the output of any kind of
query to the server-side file.
Syntax: SPOOL [file_name [. ext] [CREATE] | REPLACE] |
APPEND]] | OFF | OUT]
Example: SQL> spool /tmp/newfile.lst.
The spool command is usually interacting with the base layer. The
create helps to create a new file, replace helps to replace an
existing file with another, and append lets to add details into the
file or modify.

8: What is the difference between an error and exception?


Answer:
Exceptions are handled during the run time of a program but
errors cannot be handled. An exception occurs when a system
throws an unexpected error that is non-fatal, an object is derived
from the System.Exception class which is thrown by the Common
Language Runtime (CLR). With exception handling, the system
gets to catch the error before it causes any problem to the
program. Errors on the other hands are unhandled exceptions and
show up only at the end of the program. Hence nothing much can
be done in the case of an error.

9: What are SQLCODE and SQLERRM?


Answer:
SQLCODE and SQLERRM are two basic functions used in
PL/SQL. SQLCODE returns a number for the recent exception in
the program whereas for an internal exception it returns the
number with respect to the error occurred in the
program.SQLERRM returns the actual error message that
occurred in the last run. When the program raises an exception,
the Oracle captures the error with the SQLCODE and SQLERRM
which are variables defined globally. Both the variables can track
the exceptions under the OTHERS clause easily.

10: What is autonomous transaction?


Answer:
An Autonomous transaction is an independent and uncontrolled
transaction that lets the program to leave the context of calling
function and work independently and return to the same function
without causing any interruptions or affecting any state of the
transaction. The autonomous transaction has no link to the calling
program but can share data if there are any committed
transactions shared by both the programs. Examples of
autonomous transactions are stored procedures and functions etc.
Autonomous functions are to be used carefully else they lead to
deadlocks and confusions.

11: What are the main differences between SQL*Plus and SQL?
Answer:
SQL Plus is an interface used by Oracle where the SQL or PL/SQL
commands are executed. You can type the SQL and PL/SQL
commands on the SQL Plus command line which are executed by
Oracle Database. SQL is Structured Query Language which is
used to create, edit, delete and view the relational database tables
and schema. Simply put, SQL Plus is the GUI or environment
where the SQL commands are executed to manipulate or fetch
data from the relational database. SQL Plus is native to Oracle,
while SQL can be used by many relational databases.

12: Explain the commands Commit, Rollback and Savepoint.


Answer:
A Commit statement is used to save the data changes done by the
transaction. All users can see the changes made. Any lock
obtained by the transaction is released once committed. Once
committed the changes become permanently saved.
A Rollback statement cancels the previously done transaction if it
is not committed. All commands issued after the previous Commit
statement are undone. It releases all the locks obtained during the
transaction.
Savepoint is used when a part of the transaction need to be
undone. When the Rollback command is used with a savepoint, it
cancels all transactions executed after the savepoint was declared.

13: What types of queries are supported in Oracle?


Answer:
Oracle supports Normal Queries, Sub Queries, Co-related queries,
Nested queries and Compound queries.
Normal Queries are the usual SELECT <ColumnName> FROM
<TableName> WHERE <Condition> type of queries. For example,
Select empId, empName from empTable where empSalary >=
15000
A Sub Query is a Query within a Query. For Example,
Select empId, empName from empTable where empSalary >=
(Select Avg(empSalary) from empTable)
Here the subquery is the query that fetches the Avg(empSalary)
and it is executed first. Then the Normal Query is executed.
Co-Related Queries are sub queries which uses the outer table
fields to execute the inner table query. For Example,
Select empId, empName from empTable eT1 where empSalary >=
(Select Avg(empSalary) from empTable eT2 where eT2.deptId =
eT1.deptId)
Nested Queries are multiple levels of sub queries used in
different levels. For Example,
Select empId, Avg(empSalary) from empTable Group By deptId
Having Avg(empSalary) < (Select Max( Avg(minEmpSalary) )
From jobsTable Where deptId In (Select deptId From deptTable
Where deptNo Between 50 And 150) Group By deptId).
Compound queries are multiple queries used with set operators
like Union, Join, Union All, Intersect, Minus etc. For Example,
Select deptId, deptName from empTable
INTERSECT
Select deptId, deptName from deptTable

14: List out the disadvantages of SQL.


Answer:
Though SQL is a widely used language to work with relational
databases, it has some disadvantages also. Some of the demerits of
SQL are,
a) SQL memory management is very poor.
b) SQL does not have a procedural language.
c) SQL cannot be used to drop a field. You have to depend
on the GUI based tool to delete a column instead. Though
this is a safety feature which makes sure no related data is
lost, it is a disadvantage when an unnecessary column has
to be intentionally dropped.
d) SQL cannot be used to rename a field. You will have to
execute a stored procedure to rename a field or use
multiple Alter Table statements.
e) SQL view updation is not effective when it accesses
multiple tables.
f) SQL does not allow to create an index on a view or an
index on another index.

15: What is SQL?


Answer:
SQL or Structured Query Language is the language used to
manage databases and the data stored in them. It consists of
commands for administration of database objects (DML) such as
schema, database, tables, stored procedures, functions, views etc
and for manipulating the information stored in them (DML) such
as fetching, inserting, updating and deleting data. SQL language is
English-like which makes it easier for the Database Administrator
and programmers to manage the structured information stored in
a complex database.
Examples of SQL statements are:
Create Table <my_table_name> columns (column_1 <datatype_1>,
column_2 <datatype_2>, column_3 <datatype_3>).
This statement creates a new table in the database. One main
advantage of SQL is that most of the databases support a similar
syntax of SQL statements. The above statement works in SQL
Server and a similar syntax works in Oracle too.

16: What is database normalization?


Answer:
Data redundancy is a major problem faced by organizations that
deal with bulk data. When there are huge volumes of transactional

of duplicate data which can make the data inconsistent and waste
a lot of precious data storage space. Normalization is the method
that ensures minimal or no data redundancy. A relational
database organizes the tables with minimal data redundancy
through the process of normalization. When the relations and data
depen
redundancy. This not only helps save the data storage space but
also makes data more consistent. If you have customer name and

information has some inconsistency which would result in wrong


aggregates when we query some information based on the
customer name. A simple example is that one table stores the

database will consider these are totally different entities. Instead,


in a normalized database, the customer details are stored in a
customer table and the customer id is used as a relation in other
tables. This way, the master details remain the same in the single
customer table which makes it consistent.

17: What do you understand by Deadlock situation?


Answer:
Deadlock is a situation in a multiuser system that causes two or
more users to wait indefinitely for Locked resources. First user
needs a resource locked by the second user and the second user
needs a resource locked by the first user. To avoid dead locks,
avoid using exclusive table locks and if using, use it in the same
sequence and use commit frequently to release locks.

18: What are the main differences between DBMS and RDBMS?
Answer:
Even though the term database is commonly used for DBMS and
RDBMS, they both differ considerably. In many ways, the RDBMS
is much superior to a simple DBMS model of data storage. Here
are some major differences between a DBMS and RDBMS:
a) The DBMS model stores data as a file whereas in an
RDBMS, data is stored in tabular or structured form.
b)
set between tables. It is done programmatically. In
RDBMS, table relations are set using the Primary Key
Foreign Key values. The keys exist in the database and can
be managed.
c)
the RDBMS strictly follows normalization rules.
d) DBMS is good to handle only small volumes of data while
RDBMS is meant to handle huge volumes of data.
e) DBMS does not handle data security while RDBMS insists
on data security and integrity.
f) DBMS supports only a single user while RDBMS can
handle multiple users.
Two more characteristics that are exclusive to RDBMS are that it
supports the client-server architecture and distributed database.

19: What is a Dual?


Answer:
Dual is a Table that contains a single row. It has only one column
which has varchar2 data type and the value "X".
e.g.
SQL> describe dual;
SQL> Name Null? Type
DUMMY VARCHAR2(1)
SQL> select * from dual;
D
--
X

20: What is a User Account in Oracle?


Answer:
A user account is identified by a user name and it defines the
user's attributes like,
a) Password for database authentication
b) Privileges and role
c) default tablespace for data objects
d) default temporary tablespace for query processing
workspace.

21: What command is used to debug errors?


Answer:
Unless your program is an anonymous block, your errors will not
be reported.
Instead, SQL*Plus will display the message
"warning: procedure created with compilation errors''.
You will then need to type:
SQL> SHOW ERRORS

22: A table has the following data: [[5, Null, 10]].What will the
average function return?
Answer:
The average function typically returns the average of the values
provided as the arguments or parameters. But in Oracle, we can
use the function with tables provided it does not involve another
aggregate function or a subquery. In the given query, there are
three values out of which one is Null. The aggregate will skip the
Null value and consider the rest two only. So the result will be 7
which is the integer value of average of 5 and 10. If you want a
precise answer, you can cast the result into a double to get the
result 7.5.

23: Is Sysdate a system variable or a system function?


Answer:
Sysdate is a system function that returns the current system date.
It returns a Date data type that corresponds to the system date.
You can specify the date format in the To_Char function usually
used with the sysdate function or else, the default date format as
per the NLS_DATE_FORMAT initialization parameter in Oracle.
For example,
-DD-YYYY') "System Date"
FROM DUAL;
This query returns the following result. The To_Char function
converts the Date into the desired format as specified in the
second argument.
System Date
----------------
12-16-2016

24: What is the result of the following SQL?


Select 1 from dual UNION Select 'A' from dual;
Answer:

implies that both the queries in the UNION should have the same
datatype. Here the first query results in an integer 1 and the

mismatch, the UNION query will throw an error.

25: Are views automatically updated when base tables are


updated?
Answer:
Views never store the table data. They only store the query details

the current table data is only fetched which means that the view
results will change along with the change in the values of the table
corresponding to the view. If we create a view to store the results
of all employees belonging to a department, every time we query
the view, all records of employees belonging to the particular
department will be fetched which will also include the records
updated after the view was created.

26: Can you alter synonym of view or view?


Answer:
A synonym of a view is created when we want to hide the schema
details from the users. Instead of using the view name, the view
can be accessed using the synonym or alternative name created for
the same. A synonym can be created for tables, stored procedures
or any other database object. We can create, replace or drop a
synonym but cannot alter the synonym. But we can alter a view
using the Alter View <view_name> statement. We can create,
modify or drop the constraints for the view using the Alter View

view. It only recompiles the view definition including the change


in constraints. To alter the view definition, you need to use the
CREATE or REPLACE VIEW statement.

27: How does the RDBMS maintain Data Integrity?


Answer:
In a RDBMS model, data integrity is ensured in four levels:
a) Domain Integrity This rule ensures that every column
value conforms to the specific data type, format as well as
range of values specified in the table definition.
b) Referential Integrity This rule ensures that no records
are deleted if they are being referenced in any other table.

being referenced by the foreign key in the order table,


unless the order is deleted, the customer record cannot be
deleted.
c) Entity Integrity This rule eliminates duplicate records in
any table. It is usually enforced by the Primary / Unique
constraints or keys.
d) User-Defined Integrity These are the business rules
enforced upon the data by database objects. Though
programmers can do it in the program, for better binding
to data and for hiding the complexities of the business
logic, they do it using triggers, stored procedures and
functions.
28: What is the length of SQL integer?
Answer:
Integer in Oracle SQL corresponds to the Number data type. An
integer value is equivalent to Number (38) where scale is 0. The
size of the Integer depends on the value stored in it. When you
store different 4 digit numbers in different variables, the length of
the integer variable can be different based on the value stored. In
Oracle, the integer values are stored with a scientific notation. The
exponent is stored in the first byte and the next 20 odd bytes are
used to store the digits except the leading and trailing zeroes.

29: What is the difference between foreign key and reference


key?
Answer:
Foreign key is the key i.e. attribute which refers to another table
primary key. Reference key is the primary key of table referred by
another table.

30: What are the operations allowed on Dual Table?


Answer:
Dual is a convenience table in the SYS database of Oracle. We can
delete, update and insert rows in Dual but ideally it is not
recommended because manipulating SYS objects is not
recommended. Another point to note is that, only users who are
authorized to manipulate the tables in the SYS database will be
able to delete, update or insert rows in Dual table. If the user has
the privilege, the Dual table can also be dropped. One cannot alter
the dual table as it is a SYS table.

You might also like