Oracle PLSQL Interview Adv
Oracle PLSQL Interview Adv
Structures include DDL and DML based queries Includes procedures, functions, etc
and commands
Compatibility with each SQL can be embedded into PL/SQL cant be embedded in SQL
other PL/SQL
https://www.softwaretestinghelp.com/plsql-interview-questions/
1
Q3. What is an alias in SQL statements?
Ans: Alias is a user-defined alternative name given to the column or table. By default column alias
headings appear in upper case. Enclose the alias in a double quotation marks (“ “) to make it case
sensitive. “AS” Keyword before the alias name makes the SELECT clause easier to read.
For ex: Select emp_name AS name from employee; (Here AS is a keyword and “name” is an alias).
For ex: Select last_name||‟is a‟||job_id As “emp details” from the employee; (Here “is a” is a literal).
SQL iSQL*Plus
Is a Language Is an Environment
Character and date columns heading are left-justified Default heading justification is in Centre.
and number column headings are right-justified.
Does not have a continuation character Has a dash (-) as a continuation character if
the command is longer than one line
https://www.softwaretestinghelp.com/plsql-interview-questions/
2
Q6. Define the order of Precedence used in executing SQL statements.
Order of Precedence used in executing SQL statements
3 Comparison conditions
5 [NOT] BETWEEN
8 OR logical condition
Q7. What are SQL functions? Describe in brief different types of SQL functions?
Ans: SQL Functions are a very powerful feature of SQL. SQL functions can take arguments but always
return some value.
There are two distinct types of SQL functions:
1) Single-Row functions: These functions operate on a single row to give one result per row.
1. Character
2. Number
3. Date
4. Conversion
5. General
https://www.softwaretestinghelp.com/plsql-interview-questions/
3
6. 2) Multiple-Row functions: These functions operate on groups of rows to give one result per group
of rows.
1. AVG
2. COUNT
3. MAX
4. MIN
5. SUM
6. STDDEV
7. VARIANCE
Number Functions: accept Numeric input and return numeric values. Number Functions are: ROUND,
TRUNC and MOD
Date Functions: operates on values of the Date data type. (All date functions return a value of DATE
data type except the MONTHS_BETWEEN Function, which returns a number. Date Functions are
MONTHS_BETWEEN, ADD_MONTHS, NEXT_DAY, LAST_DAY, ROUND, TRUNC.
https://www.softwaretestinghelp.com/plsql-interview-questions/
4
Ans: Conversion Functions converts a value from one data type to another. Conversion functions are of
two types:
2. NUMBER To VARCHAR2
3. DATE To VARCHAR2
1. TO_NUMBER
2. TO_CHAR
3. TO_DATE
TO_NUMBER function is used to convert Character string to Number format. TO_NUMBER function
use fx modifier. Format: TO_NUMBER ( char[, „ format_model‟] ). fx modifier specifies the exact
matching for the character argument and number format model of TO_NUMBER function.
TO_CHAR function is used to convert NUMBER or DATE data type to CHARACTER format.
TO_CHAR Function use fm element to remove padded blanks or suppress leading zeros. TO_CHAR
Function formats:TO_CHAR (date, „format_model‟).Format model must be enclosed in single quotation
marks and is case sensitive.
TO_DATE function is used to convert Character string to date format. TO_DATE function use fx
modifier which specifies the exact matching for the character argument and date format model of
TO_DATE function. TO_DATE function format: TO_DATE ( char[, „ format_model‟] ).
https://www.softwaretestinghelp.com/plsql-interview-questions/
5
3. NULLIF: Compares two expressions and returns null if they are equal or the first expression if they
are not equal. NULLIF (exp1, exp2)
4. COALESCE: Returns the first non-null expression in the expression list. COALESCE (exp1, exp2…
expn). The advantage of the COALESCE function over NVL function is that the COALESCE
function can take multiple alternative values.
5. Conditional Expressions: Provide the use of IF-THEN-ELSE logic within a SQL statement.
Example: CASE Expression and DECODE Function.
Q12. What is the difference between COUNT (*), COUNT (expression), COUNT (distinct
expression)? (Where expression is any column name of Table)
Ans: COUNT (*): Returns a number of rows in a table including duplicates rows and rows containing
null values in any of the columns.
COUNT (EXP): Returns the number of non-null values in the column identified by expression.
COUNT (DISTINCT EXP): Returns the number of unique, non-null values in the column identified by
expression.
3. Use Single-row operators with single-row subqueries and Multiple-row operators with multiple-row
subqueries.
Types of subqueries:
1. Single-Row Subquery: Queries that return only one row from the inner select statement. Single-row
comparison operators are: =, >, >=, <, <=, <>
2. Multiple-Row Subquery: Queries that return more than one row from the inner Select statement.
There are also multiple-column subqueries that return more than one column from the inner select
statement. Operators includes: IN, ANY, ALL.
https://www.softwaretestinghelp.com/plsql-interview-questions/
6
Ans: ANY Operator compares value to each value returned by the subquery. ANY operator has a
synonym SOME operator.
https://www.softwaretestinghelp.com/plsql-interview-questions/
7
PL/SQL Interview Questions for Experienced Professionals -
(1/2/3 Years)
Q16. What is the difference between the “VERIFY” and the “FEEDBACK” command?
Ans: VERIFY Command: Use VERIFY Command to confirm the changes in the SQL statement (Old and
New values). Defined with SET VERIFY ON/OFF.
Q17. What is the use of Double Ampersand (&&) in SQL Queries? Give an example?
Ans: Use “&&” if you want to reuse the variable value without prompting the user each time.
For ex: Select empno, ename, &&column_name from employee order by &column_name;
Q18. What are Joins and how many types of Joins are there?
Ans: Joins are used to retrieve data from more than one table.
types of Joins
https://www.softwaretestinghelp.com/plsql-interview-questions/
8
Q19. Explain all Joins used in Oracle 8i?
Ans: Cartesian Join: When a Join condition is invalid or omitted completely, the result is a Cartesian
product, in which all combinations of rows are displayed. To avoid a Cartesian product, always include a
valid join condition in a “where” clause. To Join „N‟ tables together, you need a minimum of N-1 Join
conditions.
For ex: to join four tables, a minimum of three joins is required. This rule may not apply if the table has a
concatenated primary key, in which case more than one column is required to uniquely identify each row.
Equi Join: This type of Join involves primary and foreign key relation. Equi Join is also called Simple or
Inner Joins.
Non-Equi Joins: A Non-Equi Join condition containing something other than an equality operator. The
relationship is obtained using an operator other than equal operator (=).The conditions such as <= and >=
can be used, but BETWEEN is the simplest to represent Non-Equi Joins.
Outer Joins: Outer Join is used to fetch rows that do not meet the join condition. The outer join operator
is the plus sign (+), and it is placed on the side of the join that is deficient in information. The Outer Join
operator can appear on only one side of the expression, the side that has information missing. It returns
those rows from one table that has no direct match in the other table. A condition involving an Outer Join
cannot use IN and OR operator.
Self Join: Joining a table to itself.
For ex: Select a.city, b.dept_name from loc a Join dept b USING (loc_id) where loc_id=10;
https://www.softwaretestinghelp.com/plsql-interview-questions/
9
Joins with the ON clause: Use the ON clause to specify a join condition. The ON clause makes code
easy to understand. ON clause is equals to Self Joins. The ON clause can also be used to join columns
that have different names.
Left/ Right/ Full Outer Joins: Left Outer Join displays all rows from the table that is Left to the LEFT
OUTER JOIN clause, right outer join displays all rows from the table that is right to the RIGHT OUTER
JOIN clause, and full outer join displays all rows from both the tables either left or right to the FULL
OUTER JOIN clause.
Q22. What is a Transaction? Describe common errors can occur while executing any Transaction?
Ans: Transaction consists of a collection of DML statements that forms a logical unit of work.
The common errors that can occur while executing any transaction are:
https://www.softwaretestinghelp.com/plsql-interview-questions/
10
Q24. What is a difference between Commit, Rollback and Savepoint?
COMMIT: Ends the current transaction by making all pending data changes permanent.
ROLLBACK: Ends the current transaction by discarding all pending data changes.
SAVEPOINT: Divides a transaction into smaller parts. You can rollback the transaction till a
particular named savepoint.
4. Table name cannot duplicate the name of another object owned by the same user.
Q28. What is the difference between USER TABLES and DATA DICTIONARY?
Ans: USER TABLES: Is a collection of tables created and maintained by the user. Contain USER
information. DATA DICTIONARY: Is a collection of tables created and maintained by the Oracle Server.
It contains database information. All data dictionary tables are owned by the SYS user.
https://www.softwaretestinghelp.com/plsql-interview-questions/
11
Q29. Describe a few Data Types used in SQL?
Ans: Data Types is a specific storage format used to store column values. Few data types used in SQL
are:
3. NUMBER(P,S): " Precision" can range from 1 to 38 and the “Scale” can range from -84 to 127.
4. DATE
5. LONG: 2GB
6. CLOB: 4GB
9. BLOB: 4GB
11. ROWID: A 64 base number system representing the unique address of a row in the table.
3. You can decrease the width of a column only if the column contains null values or if the table has no
rows.
4. You can change the data type only if the column contains null values.
5. You can convert a CHAR column to the VARCHAR2 data type or convert a VARCHAR2 column to
the CHAR data type only if the column contains null values or if you do not change the size.
https://www.softwaretestinghelp.com/plsql-interview-questions/
12
Ans: SET UNUSED option marks one or more columns as unused so that they can be dropped when the
demand on system resources is lower. Unused columns are treated as if they were dropped, even though
their column data remains in the table‟s rows. After a column has been marked as unused, you have no
access to that column. A select * query will not retrieve data from unused columns. In addition, the names
and types of columns marked unused will not be displayed during a DESCRIBE, and you can add to the
table a new column with the same name as an unused column. The SET UNUSED information is stored
in the USER_UNUSED_COL_TABS dictionary view.
TRUNCATE DELETE
Removes all rows from a table and releases Removes all rows from a table but does not release
storage space used by that table. storage space used by that table.
Is a DDL statement and cannot be Rollback. Is a DDL statement and can be Rollback.
Q35. What are Constraints? How many types of constraints are there?
Ans: Constraints are used to prevent invalid data entry or deletion if there are dependencies. Constraints
enforce rules at the table level. Constraints can be created either at the same time as the table is created or
after the table has been created. Constraints can be defined at the column or table level. Constraint
defined for a specific table can be viewed by looking at the USER-CONSTRAINTS data dictionary table.
https://www.softwaretestinghelp.com/plsql-interview-questions/
13
You can define any constraint at the table level except NOT NULL which is defined only at the column
level. There are 5 types of constraints:
6.
Oracle PL SQL Technical interview questions
https://www.softwaretestinghelp.com/plsql-interview-questions/
14
Unique Key Primary Key
A table can have more than one Unique Key. A table can have only one Primary Key.
Unique key column can store NULL values. Primary key column cannot store NULL values.
Uniquely identify each value in a column. Uniquely identify each row in a table.
Q38. What is a difference between ON DELETE CASCADE and ON DELETE SET NULL?
Ans: ON DELETE CASCADE Indicates that when the row in the parent table is deleted, the dependent
rows in the child table will also be deleted. ON DELETE SET NULL Coverts foreign key values to null
when the parent value is removed. Without the ON DELETE CASCADE or the ON DELETE SET
NULL options, the row in the parent table cannot be deleted if it is referenced in the child table.
https://www.softwaretestinghelp.com/plsql-interview-questions/
15
Ans: The main differences between two views are:
Simple Views Vs Complex Views
Derives data from only one table. Derives data from many tables.
Can perform DML operations through the Does not always allow DML operations through the
view. view.
1. Group Functions
2. A Group By clause
1. Group Functions
2. A Group By clause
Q43. What is PL/SQL, Why do we need PL/SQL instead of SQL, Describe your experience
working with PLSQL and What are the difficulties faced while working with PL SQL and How did
you overcome?
https://www.softwaretestinghelp.com/plsql-interview-questions/
16
2. Oracle introduced PL/SQL
3. It is a combination of SQL and Procedural Statements and used for creating applications.
4. Basically PL/SQL is a block structured programming language whenever we are submitting PL/SQL
5. Blocks then all SQL statements are executing separately by using sql engine and also all procedure
statements are executed separately.
6. Explain your current and previous projects along with your roles and responsibilities, mention some
of the challenging difficulties you've faced in your project while working with PL/SQL.
Statement Level Trigger: In statement-level trigger, the trigger body is executed only once for DML
statement.
Row Level Trigger: In row level trigger, the trigger body is executed for each row DML statement. It is
the reason, we are employing each row clause and internally stored DML transaction in trigger
specification, these qualifiers :old, :new, are also called as records type variables.
These qualifiers are used in trigger specification & trigger body.
Syntax:
:old.column_name
Syntax:
:new column_name
When we are use this qualifiers in trigger specification then we are not allowed to use “:” in forms of the
names of the qualifiers.
Q45. Write a PL/SQL Program which raise a user defined exception on thursday?
Ans:
1 declare
2 a exception
https://www.softwaretestinghelp.com/plsql-interview-questions/
17
3 begin
4 If to_char(sysdate, „DY)=‟THU‟
5 then
6 raise a;
7 end if;
8 exception
9 when a then
10 dbms_output.put_line(„my exception raised on thursday‟);
11 end
12 ;
Output: my exception raised on thursday
Q46.Write a PL/SQL program to retrieve emp table and then display the salary?
Ans:
1 declare
2 v_sal number(10);
3 begin select max(sal)intr v_sal;
4 from emp;
5 dbms_output.put_line(v.sal);
6 end;
7 /
(or)
1 declare
2 A number(10);
3 B number(10);
4 C number(10);
5 begin
6 a:=70;
7 b:=30;
8 c:=greatest+(a,b);
9 dbms_output.put_line(c);
10 end;
11 /
Output:70
Q48. Write a PL/SQL cursor program to display all employee names and their salary from emp
table by using % not found attributes?
Ans:
1 Declare
2 Cursor c1 is select ename, sal from emp;
3 v_ename varchar2(10);
4 v_sal number(10);
5 begin
6 open c1;
7 loop
8 fetch c1 into v_ename, v_sal;
9 exist when c1 % notfound;
10 dbms_output.put_line(v_name ||‟ „||v_sal);
11 end loop;
12 close c1;
13 end;
14 /
Into a row-level trigger based on a table, the trigger body cannot read data from the same table and
also we cannot perform DML operation on the same table.
If we are trying this oracle server returns mutating error oracle-4091: table is mutating.
This error is called a mutating error, and this trigger is called a mutating trigger, and the table is
called a mutating table.
Mutating errors are not occurred in statement-level trigger because through this statement-level
trigger when we are performing DML operations automatically data committed into the database,
whereas in the row-level trigger when we are performing transaction data is not committed and also
again we are reading this data from the same table then only mutating errors is occurred.
https://www.softwaretestinghelp.com/plsql-interview-questions/
19
Oracle PL/SQL Interview Questions and Answers for 10 Years Experience
Q50. What is Triggering Events (or) Trigger Predicate Clauses?
Ans: If we want to perform multiple operations in different tables then we must use triggering events
within trigger body. These are inserting, updating, deleting clauses. These clauses are used in statement,
row-level trigger. These triggers are also called as trigger predicate clauses.
Syntax:
1 If inserting then stmts;
2 else if updating then stmts;
3 else if deleting then stmts;
4 end if;
Discard file we must specify within the control file by using the discard file clause.
Discard file also stores reflected record based on when clause condition within the control file. This
condition must be satisfied into the table tablname clause.
Q52. What is REF CURSOR (or) CURSOR VARIABLE (or) DYNAMIC CURSOR ?
Ans:
Oracle 7.2 introduced ref cursor, This is an user-defined type which is used to process multiple records
and also this is a record by record process.
In static cursor database servers executes only one select statement at a time for a single active set area
where in ref cursor database servers executes number of select statement dynamically for a single active
set area that's why those cursor are also called as dynamically cursor.
Generally we are not allowed to pass static cursor as parameters to use subprograms where as we can also
pass ref cursor as parameter to the subprograms because basically refcursor is an user defined type in
oracle we can also pass all user defined type as parameter to the subprograms.
Generally static cursor does not return multiple record into client application where as ref cursor are
allowed to return multiple records into client application (Java, .Net, php, VB, C++).
This is an user defined type so we are creating it in 2 steps process i.e first we are creating type then only
we are creating variable from that type that‟s why this is also called as cursor variable.
https://www.softwaretestinghelp.com/plsql-interview-questions/
20
Q53. What are The Types of Ref Cursors?
Strong ref cursor is a ref cursor which have return type, whereas weak ref cursor has no return type.
Syntax:
1 Type typename is ref cursor return record type data type;
2 Variable Name typename
Syntax
1 Type typename is ref cursor
2 Variable Name typename;
In Weak ref cursor we must specify select statement by using open for clause this clause is used in
executable section of the PL/SQL block.
Syntax:
1 Open ref cursor varname for SELECT * FROM table_name condition;
https://www.softwaretestinghelp.com/plsql-interview-questions/
21
24 end if;
25 end loop;
26 end;
27 /
https://www.softwaretestinghelp.com/plsql-interview-questions/
22
type to number type then oracle servers returns an error: ora-6502:numeric or value error: character to a
number conversion error
For handling, this error oracle provided exception value_error exception name
Example:
1 begin
2 declare z number(10);
3 begin
4 z:= „&x‟ + „&y‟;
5 dbms_output.put_line(z);
6 exception when value_error then dbms_output.put_line(„enter numeric data value for x & y only‟);
7 end;/
Output:
1 Enter value for x:3
2 Enter value for y:2
3 z:=5
4
5 Enter value for x:a
6 Enter value for y:b
7
8 Error:enter numeric data value for x & y only.
Q59. What is Flashback Query?
Ans:
Flashback query are handle by Database Administrator only flashback queries along allows the
content of the table to be retrieved with reference to the specific point of time by using as of clause
that is flashback queries retrieves clause that is flashback queries retrieves accidental data after
committing the transaction also.
Flashback queries generally uses undo file that is flashback queries retrieve old data before
committing the transaction oracle to provide two methods for flashback queries
Package specification: it acts as a public interface for your application which includes procedures, types,
etc.
https://www.softwaretestinghelp.com/plsql-interview-questions/
23
Package Body: It contains the code which required to implement the Package Specification
Functions and procedures, within the package, shares a common variable among them.
Packages enhance the performance even when the multiple objects loaded into memory.
DBMS_ TRACE
DBMS_ APPLICATION_INFO
Ans: Implicit cursor used in PL/SQL to declare, all SQL data manipulation statements. An implicit
cursor is used to declare SQL statements such as open, close, fetch etc.
An explicit cursor is a cursor and which is explicitly designed to select the statement with the help of a
cursor. This explicit cursor is used to execute the multirow select function. An explicit function is used
PL/SQL to execute tasks such as update, insert, delete, etc.
6) what is a trigger?
https://www.softwaretestinghelp.com/plsql-interview-questions/
24
Ans: It is a program in PL/SQL, stored in the database and executed instantly before or after the
UPDATE, INSERT and DELETE commands.
https://www.softwaretestinghelp.com/plsql-interview-questions/
25