SQL PL SQL Interview Questions
SQL PL SQL Interview Questions
DELETE TRUNCATE
Delete command is used to delete a row in a Truncate is used to delete all the rows from a
table. table.
You can rollback data after using delete
You cannot rollback data.
statement.
It is a DML command. It is a DDL command.
It is slower than truncate statement. It is faster.
Constraints are used to specify the limit on the data type of the table. It can be specified while
creating or altering the table statement. The sample of constraints are:
NOT NULL
CHECK
DEFAULT
UNIQUE
PRIMARY KEY
FOREIGN KEY
DROP command removes a table and it cannot be rolled back from the database whereas
TRUNCATE command removes all the rows from the table.
Correlated subquery: These are queries which select the data from a table referenced in the
outer query. It is not considered as an independent query as it refers to another table and refers
the column in a table.
Non-Correlated subquery: This query is an independent query where the output of subquery is
substituted in the main query.
HAVING clause can be used only with SELECT statement. It is usually used in a GROUP BY
clause and whenever GROUP BY is not used, HAVING behaves like a WHERE clause.
Having Clause is only used with the GROUP BY function in a query whereas WHERE Clause is
applied to each row before they are a part of the GROUP BY function in a query.
Q. What is a View?
A view is a virtual table which consists of a subset of data contained in a table. Since views are
not present, it takes less space to store. View can have data of one or more tables combined and
it depends on the relationship.
The NVL function is used to replace NULL values with another or given value. Example is –
COALESCE function is used to return the value which is set to be not null in the list. If all
values in the list are null, then the coalesce function will return NULL.
Coalesce(value1, value2,value3,…)
Translate is used for character by character substitution and Replace is used substitute a single
character with a word.
Aggregate function is a function where values of multiple rows or records are joined together to
get a single value output. Common aggregate functions are –
Average
Count
Sum
What is a sub query and what are the different types of subqueries?
Sub Query is also called as Nested Query or Inner Query which is used to get data from multiple
tables. A sub query is added in the where clause of the main query.
A Correlated sub query cannot be as independent query but can reference column in a table listed
in the from list of the outer query.
Non-Correlated subquery
This can be evaluated as if it were an independent query. Results of the sub query are submitted
to the main query or parent query.
1. %FOUND
Returns NULL if cursor is open and fetch has not been executed
%NOT FOUND
Returns NULL if cursor is open and fetch has not been executed
%ISOPEN
Returns true if the cursor is open
%ROWCOUNT
Returns the number of rows fetched. It has to be iterated through entire cursor to give exact real
count.
SUBSTR returns specific portion of a string and INSTR provides character position in
which a pattern is found in a string.
What is the maximum number of triggers that can be applied to a single table?
The DUAL table is a one-column table present in oracle database. The table has a single
VARCHAR2(1) column called DUMMY which has a value of ‘X’.
How to get distinct records from the table without using distinct keyword.
select * from Employee a where rowid = (select max(rowid) from Employee b where
a.Employee_no=b.Employee_no);
Ans: A Sequence generates a serial list of unique numbers for numerical columns of a database's
tables.
What is a synonym?
Ans: An index is an optional structure associated with a table to have direct access to rows,
which can be created to increase the performance of data retrieval. Index can be created on one
or more columns of a table. Index may also be considered as a ordered list of content of a
column.
What is a schema?
Ans: The set of objects owned by user account is called the schema.
What are inline views?
Ans: Inline view is Sub-query(queries written in a where clause of SQL statements.). It is a query
whose return values are used in filtering conditions of the main query.
Ans: Foreign key: A foreign key is one or more columns whose values are based on the primary
or candidate key values from another table. Unique key can be null; Primary key cannot be null.
Answer: Data types define the ways to identify the type of data and their associated operations.
There are 4 types of predefined data types explained as follows
Scalar Data Types: A scalar data type is an atomic data type that does not have any
internal components.
For example
o CHAR (fixed length character value between 1 and 32,767 characters)
o VARCHAR2 (variable length character value between 1 and 32,767 characters)
o NUMBER ( fixed-decimal, floating-decimal or integer values)
o BOOLEAN ( logical data type for TRUE FALSE or NULL values)
o DATE (stores date and time information)
o LONG (character data of variable length)
Composite Data Types: A composite data type is made up of other data types and
internal components that can be easily used and manipulated. For example RECORD,
TABLE, and VARRAY.
Reference Data Types: A reference data types holds values, called pointers that
designate to other program items or data items. For example REF CURSOR.
Large Object Data Types: A Large Object datatype holds values, called locators, that
defines the location of large objects( such as video clips, graphic image, etc) stored out of
line.
For example
o BFILE (Binary file)
o BLOB (Binary large object)
o CLOB ( Character large object)
o NCLOB( NCHAR type large object)
Ans: Trigger is also same as stored procedure & also it will automatically invoked whenever
DML operation performed against table or view.
Statement Level Trigger: In statement level trigger, trigger body is executed only once for
DML statement.
Row Level Trigger: In row level trigger, trigger body is executed for each row DML
statements. 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.
Synatx:
:old.column_name
Synatx:
:new column_name
When we are use this qualifiers in trigger specification then we are not allowed to use “:” in
forms of the qualifiers names.
What is Mutating Trigger?
Ans:
Into a row level trigger based on a table trigger body cannot read data from same table and also
we cannot perform DML operation on same table.
If we are trying this oracle server returns mutating error oracle-4091: table is mutating.
This error is called mutating error, and this trigger is called mutating trigger, and table is called
mutating table.
Mutating errors are not occurred in statement level trigger because through these statement
level trigger when we are performing DML operations automatically data committed into the
database, whereas in 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.
Answer: The INSTEAD OF triggers are the triggers written especially for modifying views,
which cannot be directly modified through SQL DML statements.
Ans: Overload is refers to same name can be used for different purpose, in oracle we can also
implement overloading procedure through package. Overloading procedure having same name
with different type or different number of parameters.
Ans: In oracle we are declaring global variables in Package Specification only.
Ans: In oracle declaring procedures within package body is called forward declaring generally
before we are calling private procedures into public procedure first we must implements private
into public procedure first we must implements private procedure within body otherwise use a
forward declaration within package body.
Error handling part of PL/SQL block is called Exception. They have two types: user_defined and
predefined.
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
Explain Raise_application_error.
The PRAGMA EXECPTION_INIT tells the complier to associate an exception with an oracle
error. To get an error message of a specific oracle error.
DECODE and CASE statements are very similar, but CASE is extended version of DECODE.
DECODE does not allow Decision making statements in its place.
Question : Which Function Is Used To Find The Largest Integer Less Than Or Equal To A
Specific Value?
Answer :
FLOOR.
There are several situations to use autonomous transactions like event logging and auditing.
Answer: The variables or an expression referred to as parameters that appear in the procedure
call statement is known as Actual parameters.
Here in the above example, emp_num and amount are the two actual parameters.
The variables that are declared in the procedure header and are referenced in the procedure body
are called as Formal parameters.
For example:
PROCEDURE raise_sal( emp_id INTEGER) IS
curr_sal REAL:
………..
BEGIN
SELECT sal INTO cur_sal FROM emp WHERE empno = emp_id;
…….
END raise_sal;
Answer: 3 modes of the parameter are IN, OUT, IN OUT. These can be explained as follows
IN parameters: IN parameters allow you to pass values to the procedure being called and can be
initialized to default values. IN parameters acts like a constant and cannot be assigned any value.
OUT parameters: OUT parameters return value to the caller and they must be specified. OUT
parameters act like an uninitialized variable and cannot be used in an expression.
IN OUT parameters: IN OUT parameters passes initial values to a procedure and returns
updated values to the caller. IN OUT parameters act like an initialized variable and should be
assigned a value.
Answer: A PL/SQL records can be referred as a collection of values or say, a group of multiple
pieces of information, each of which is of simpler types and can be related to one another as
fields.
Ans: A REF CURSOR is basically a data type. A variable created based on such a data type is
generally called a cursor variable. A cursor variable can be associated with different queries at
run-time. The primary advantage of using cursor variables is their capability to pass result sets
between sub programs (like stored procedures, functions, packages etc.).
What are cascading triggers? What is the maximum no of cascading triggers at a time?
Ans: When a statement in a trigger body causes another trigger to be fired, the triggers are said to
be cascading. Max = 32
Ans: PL/SQL requires that you declare an identifier before using it. Therefore, you must declare
a subprogram before calling it. This declaration at the start of a subprogram is called forward
declaration. A forward declaration consists of a subprogram specification terminated by a
semicolon.