0% found this document useful (0 votes)
415 views

SQL PL SQL Interview Questions

The document discusses various SQL concepts and interview questions related to SQL. It covers topics such as the difference between DELETE and TRUNCATE statements, data constraints, subquery types, aggregate functions, and more. Constraints are used to specify data type limits and can be specified in CREATE and ALTER table statements. There are two types of subqueries - correlated and non-correlated. Aggregate functions join multiple row values into a single output value.

Uploaded by

Sohaib Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
415 views

SQL PL SQL Interview Questions

The document discusses various SQL concepts and interview questions related to SQL. It covers topics such as the difference between DELETE and TRUNCATE statements, data constraints, subquery types, aggregate functions, and more. Constraints are used to specify data type limits and can be specified in CREATE and ALTER table statements. There are two types of subqueries - correlated and non-correlated. Aggregate functions join multiple row values into a single output value.

Uploaded by

Sohaib Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

SQL Interview Questions

Q. What is the difference between DELETE and TRUNCATE statements?

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.

Q. What are Constraints?

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

Q. What is the difference between DROP and TRUNCATE commands?

DROP command removes a table and it cannot be rolled back from the database whereas
TRUNCATE command removes all the rows from the table.

Q. What are the different types of a subquery?

There are two types of subquery namely, Correlated and Non-Correlated.

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.

Q. What is the need of MERGE statement?


This statement allows conditional update or insertion of data into a table. It performs an
UPDATE if a row exists, or an INSERT if the row does not exist.

Q. What is the difference between ‘HAVING’ CLAUSE and a ‘WHERE’


CLAUSE?

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.

What is the use of NVL function?

The NVL function is used to replace NULL values with another or given value. Example is –

NVL(Value, replace value)

What is COALESCE function?

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,…)

What is the difference between TRANSLATE and REPLACE?

Translate is used for character by character substitution and Replace is used substitute a single
character with a word.

What is the use of Aggregate functions in Oracle?

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 do you mean by GROUP BY Clause?


A GROUP BY clause can be used in select statement where it will collect data across multiple
records and group the results by one or more columns.

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.

There are two different types of subqueries:

 Correlated sub 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.

How can we delete duplicate rows in a table?

Duplicate rows in the table can be deleted by using ROWID.

What are the attributes of Cursor?

Attributes of Cursor are

1. %FOUND

Returns NULL if cursor is open and fetch has not been executed

Returns TRUE if the fetch of cursor is executed successfully.

Returns False if no rows are returned.

 %NOT FOUND

Returns NULL if cursor is open and fetch has not been executed

Returns False if fetch has been executed

Returns True if no row was returned

 %ISOPEN
Returns true if the cursor is open

Returns false if the cursor is closed

 %ROWCOUNT

Returns the number of rows fetched. It has to be iterated through entire cursor to give exact real
count.

What are the various constraints used in Oracle?

Following are constraints used:

 NULL – It is to indicate that particular column can contain NULL values


 NOT NULL – It is to indicate that particular column cannot contain NULL values
 CHECK – Validate that values in the given column to meet the specific criteria
 DEFAULT – It is to indicate the value is assigned to default value

What is difference between SUBSTR and INSTR?

 SUBSTR returns specific portion of a string and INSTR provides character position in
which a pattern is found in a string.

 SUBSTR returns string whereas INSTR returns numeric.

What are the different Oracle Database objects?

There are different data objects in Oracle –

 Tables – set of elements organized in vertical and horizontal


 Views  – Virtual table derived from one or more tables
 Indexes – Performance tuning method for processing the records
 Synonyms – Alias name for tables
 Sequences – Multiple users generate unique numbers
 Tablespaces – Logical storage unit in Oracle

What is the maximum number of triggers that can be applied to a single table?

 12 is the maximum number of triggers that can be applied to a single table.

What is the data type of DUAL 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);

Write a SQL query to fetch only odd rows from table.


Ans. This can be achieved by using Row_number in SQL server-

 SELECT E.EmpId, E.Project, E.Salary


 FROM (
 SELECT *, Row_Number() OVER(ORDER BY EmpId) AS RowNumber
 FROM EmployeeSalary
 ) E
 WHERE E.RowNumber % 2 = 1

What is an Oracle sequence?

Ans: A Sequence generates a serial list of unique numbers for numerical columns of a database's
tables.

What is a synonym?

Ans: A synonym is an alias for a table, view, sequence or program unit.

What are the types of synonyms?

Ans: There are two types of synonyms private and public.

What is a private synonym?

Ans: Only its owner can access a private synonym.

What is a public synonym?

Ans: Any database user can access a public synonym

What is an Oracle index?

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.

Difference between unique key,primary key and foreign key ?

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.

PL SQL Interview Question’s


Question ) What are the data types available in PL/SQL?

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)

Describe the use of %ROWTYPE and %TYPE in PL/SQL


Ans: %ROWTYPE allows you to associate a variable with an entire table row. The %TYPE
associates a variable with a single column type.

What is a difference between Simple and Complex Views?

Ans: The main differences between two views are:

Simple Views Vs Complex Views

Simple View Complex View


Derives data from only one table. Derives data from many tables.
Contains no functions or group of data  Contain functions or groups of data.
Can perform DML operations through the view.

What are the different functionalities of a Trigger?

Ans: Trigger is also same as stored procedure & also it will automatically invoked whenever
DML operation performed against table or view.

There are two types of triggers supported by PL/SQL

1. Statement Level Trigger.


2. Row Level Trigger

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.

Question) What are INSTEAD of triggers?

Answer: The INSTEAD OF triggers are the triggers written especially for modifying views,
which cannot be directly modified through SQL DML statements.

Q. What is Overloading Procedures?

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.

Q. What is Global Variables?

Ans:  In oracle we are declaring global variables in Package Specification only.

Q. What is Forward Declaration?

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.

What are the two types of exceptions?

Error handling part of PL/SQL block is called Exception. They have two types: user_defined and
predefined.

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

Explain Raise_application_error.

It is a procedure of package DBMS_STANDARD that allows issuing of user_defined error


messages from database trigger or stored sub-program.

What is Pragma EXECPTION_INIT? Explain the usage?

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.

e.g. PRAGMA EXCEPTION_INIT (exception name, oracle error number)

Differ between DECODE and CASE.

DECODE and CASE statements are very similar, but CASE is extended version of DECODE.
DECODE does not 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 directly used in PL SQL, but DECODE is used in PL SQL through SQL only.

Question : Which Function Is Used To Find The Largest Integer Less Than Or Equal To A
Specific Value?

Answer :

FLOOR.

Explain autonomous transaction.

An autonomous transaction is an independent transaction of the main or parent transaction. It is


not nested if it is started by another transaction.

There are several situations to use autonomous transactions like event logging and auditing.

Question) What are actual parameters and formal parameters?

Answer: The variables or an expression referred to as parameters that appear in the procedure
call statement is known as Actual parameters.

For example: raise_sal(emp_num, merit+ amount);

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;

Here in the above example, emp_id acts as a formal parameter.

Question) What are the 3 modes of parameter?

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.

Question) What do you understand by PL/SQL Records?

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.

There are three types of records supported in PL/SQL

 Table based records


 Programmer based records
 Cursor based records

What is Ref Cursor?

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 is Autonomous transaction? Where do we use it?

Ans: In Oracle's database products, an autonomous transaction is an independent transaction that


is initiated by another transaction. It must contain at least one Structured Query Language (SQL)
statement. Autonomous transactions allow a single transaction to be subdivided into multiple
commit/rollback transactions, each of which will be tracked for auditing purposes. When an
autonomous transaction is called, the original transaction (calling transaction) is temporarily
suspended. The autonomous transaction must commit or roll back before it returns control to the
calling transaction. Once changes have been made by an autonomous transaction, those changes
are visible to other transactions in the database. Autonomous transactions can be nested. That is,
an autonomous transaction can operate as a calling transaction, initializing other autonomous
transactions within itself.

What do u mean by overloading?

Ans: Function Overloading: Packages allow you to overload procedures or functions.


Overloading a procedure means creating multiple procedures with the same name in the same
package, each taking arguments of different number or datatype.

What are the constructs of a procedure, function or a package?


Ans: The constructs of a procedure, function or a package are : • variables and constants •
cursors • exceptions

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

What is a forward declaration? What is its use?

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.

You might also like