Oracle Interview Questions and Answers PDF 1
Oracle Interview Questions and Answers PDF 1
It is widely used for online transaction processing, data warehousing, and enterprise grid
computing.
It has a set of read-only tables and views and it is physically stored in the SYSTEM tablespace.
If any of the datatypes or precision gets changed in the database, PL/SQL code gets updated
automatically with the data type changes.
%TYPE is used for declaring a variable which needs to have the same data type as of a table
column.
While %ROWTYPE will be used to define a complete row of record having a structure similar to
the structure of a table.
4. Why do we create Stored Procedures & Functions in PL/SQL and how are they
different?
Answer: A stored procedure is a set of SQL statements that are written to perform a specific
task. These statements can be saved as a group in the database with an assigned name and can
be shared with different programs if permissions are there to access the same.
Functions are again subprograms that are written to perform specific tasks but there are
differences between both of them.
5. What is Oracle?
Answer: Oracle is a company. Oracle is also a database server, which manages data in a very
structured way. It allows users to store and retrieve related data in a multi-user environment so
that many users can concurrently access the same data. All this is accomplished while delivering
high performance. A database server also prevents unauthorized access and provides efficient
solutions for failure recovery. A standby database is a database replica created by taking a
backup of a primary database.
Oracle Universal Installer (OUI)- The Oracle Universal Installer installs your Oracle
software and options. It can automatically launch the Database Configuration Assistant to
install a database.
Database Configuration Assistant (DBCA)- The Database Configuration Assistant creates
a database from templates that are Oracle supplied, or you can create your own. It enables
you to copy a preconfigured seed database, thus saving the time and effort of customizing
and generating a database from scratch.
Database Upgrade Assistant- This tool guides you through the upgrade of your existing
database to a new Oracle release.
Oracle Net Manager- This tool guides you through your Oracle Net network configuration.
Oracle Enterprise Manager- The primary tool for managing your database is Oracle
Enterprise Manager, a web-based interface. After you have installed the Oracle software,
created or upgraded a database, and configured the network, you can use Oracle
Enterprise Manager as the single interface for managing your database. In addition, Oracle
Enterprise Manager also provides an interface for performance advisors and an interface
for Oracle utilities such as SQL*Loader and Recovery Manager.
8. What are SQL functions? Describe in brief different types of SQL functions?
Answer: 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.
Types of Single-Row functions:
Character
Number
Date
Conversion
General
2) Multiple-Row functions: These functions operate on groups of rows to give one result per group
of rows.
AVG
COUNT
MAX
MIN
SUM
STDDEV
VARIANCE
NVL: Converts a null value to an actual value. NVL (exp1, exp2) .If exp1 is null then NVL function
return value of exp2.
NVL2: If exp1 is not null, nvl2 returns exp2, if exp1 is null, nvl2 returns exp3. The argument exp1
can have any data type. NVL2 (exp1, exp2, exp3)
NULLIF: Compares two expressions and returns null if they are equal or the first expression if
they are not equal. NULLIF (exp1, exp2)
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.
Conditional Expressions: Provide the use of IF-THEN-ELSE logic within a SQL statement.
Example: CASE Expression and DECODE Function. ( data science training )
Here, a reserved keyword ‘date’ has been used as an alias for the column birthdate. This action is
illegal in Oracle SQL. In order to set a reserved keyword as an alias, we can use quotation marks.
SELECT student_id s_id, student_name name, birthdate “date”, student_number s_no FROM
students;
11. What is the difference between a Primary Key & a Unique Key?
Answer: Primary key is used to identify each table row uniquely, while a Unique Key prevents
duplicate values in a table column.
13. What are the set operators UNION, UNION ALL, MINUS & INTERSECT meant to do?
Answer: Set operator facilitates the user to fetch the data from two or more than two tables at
once if the columns and relative data types are the same in the source tables.
UNION operator returns all the rows from both the tables except the duplicate rows.
UNION ALL returns all the rows from both the tables along with the duplicate rows.
MINUS returns rows from the first table, which does not exist in the second table.
INTERSECT returns only the common rows in both the tables.
14. What do you understand by a database object? Can you list a few of them?
Answer: An object used to store the data or references of the data in a database is known as a
Database object.
The database consists of various types of DB objects such as tables, views, indexes, constraints,
stored procedures, triggers, etc.
15. What do you understand by database schema and what does it hold?
Answer: Schema is a collection of database objects owned by a database user who can create
or manipulate new objects within this schema.
The schema can contain any DB objects like table, view, indexes, clusters, stored procs,
functions, etc
17. What is the relationship among database, tablespace and data file?
Answer: An Oracle database contains one or more logical storage units called tablespaces.
These tablespaces collectively store whole data of databases and each tablespace in Oracle
database consists of one or more files called datafiles. These datafiles are a physical structure
that confirms with the operating system in which Oracle is running.
18. What is the difference between a hot backup and cold backup in Oracle? Tell about
their benefits also?
Answer: Hot backup (Online Backup): A hot backup is also known as an online backup because
it is done while the database is active. Some sites can not shut down their database while making
a backup copy, they are used for 24 hours a day, 7 days a week.
Cold backup (Offline Backup): A cold backup is also known as an offline backup because it is
done while the database has been shut down using the SHUTDOWN normal command. If the
database is suddenly shutdown with an uncertain condition it should be restarted with RESTRICT
mode and then shut down with NORMAL option.
For a complete cold backup, the following files must be backed up.
All datafiles, All control files, All online redo log files(optional) and the init.ora file (you can
recreate it manually).
Pre- select query fires during the execute query and count query processing after Oracle
forms construct the select statement to be issued, but before the statement is actually
issued.
Pre- query trigger fires before Pre-select trigger.
VARCHAR can store characters up to 2000 bytes while VARCHAR2 can store up to 4000 bytes.
VARCHAR will hold the space for characters defined during declaration even if all of them are not
used whereas VARCHAR2 will release the unused space.
21. What do you mean by a database transaction & what all TCL statements are available
in Oracle?
Answer: Transaction occurs when a set of SQL statements are executed in one go. To control
the execution of these statements, Oracle has introduced TCL i.e. Transaction Control
Statements that use a set of statements (top 50 oracle interview questions and answers pdf).
A table can hold data but not SQL Query results whereas View can save the query results, which
can be used in another SQL Query as a whole.
The table can be updated or deleted while Views cannot be done so.
24. How will you distinguish a global variable with a local variable in PL/SQL?
Answer: Global variable is the one, which is defined at the beginning of the program and survives
until the end.
It can be accessed by any methods or procedures within the program, while the access to the
local variable is limited to the procedure or method where it is declared.
25. What is the difference between varchar and varchar2 data types?
Answer: Varchar can store up to 2000 bytes and varchar2 can store up to 4000 bytes. While
Varchar will occupy space for a NULL value, Varchar2 will not occupy any space. They are
differentiated by space.
At the highest level, the idea of grid computing is computing as a utility. In other words, you
should not care where your data resides, or what computer processes your request. You should
be able to request information or computation and have it delivered – as much as you want, and
whenever you want. This is analogous to the way electric utilities work, in that you don’t know
where the generator is, or how the electric grid is wired, you just ask for electricity, and you get it.
The goal is to make computing a utility, a commodity, and ubiquitous. Hence the name ‘The Grid’.
This view of utility computing is, of course, a “client-side” view.
From the “server-side”, or behind the scenes, the grid is about resource allocation, information
sharing, and high availability. Resource allocation ensures that all those that need or request
resources are getting what they need, that resources are not standing idle while requests are
going unserviced. Information sharing makes sure that the information users and applications
need is available where and when it is needed. High availability features guarantee all the data
and computation is always there, just like a utility company always provides electric power.
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
(Top 50 Oracle Interview Questions and Answers pdf). 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
Natural Joins: Is used to join two tables automatically based on the columns which have matching
data types and names, using the keyword NATURAL JOIN. It is equal to the Equi-Join. If the
columns have the same names but different data types, then the Natural Join syntax causes an
error.
Join with the USING clause: If several columns have the same names but the data types do not
match, then the NATURAL JOIN clause can be modified with the USING clause to specify the
columns that should be used for an equi Join. Use the USING clause to match only one column
when more than one column matches. Do not use a table name or alias in the referenced
columns. The NATURAL JOIN clause and USING clause are mutually exclusive.
Forex: Select a.city, b.dept_name from loc a Join dept b USING (loc_id) where loc_id=10;
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
UNIQUE KEY: UNIQUE Key Constraint ensures that every value in a column or set of columns
must be unique, that is, no two rows of a table can have duplicate values in a specified column or
set of columns. If the UNIQUE constraint comprises more than one column, that group of
columns is called a Composite Unique Key. There can be more than one Unique key on a table.
Unique Key Constraint allows the input of Null values. Unique Key automatically creates an index
on the column it is created.
PRIMARY KEY: Uniquely identifies each row in the Table. Only one PRIMARY KEY can be
created for each table but can have several UNIQUE constraints. PRIMARY KEY ensures that no
column can contain a NULL value. A Unique Index is automatically created for a PRIMARY KEY
column. PRIMARY KEY is called a Parent key.
FOREIGN KEY: Is also called Referential Integrity Constraint. FOREIGN KEY is one in which a
column or set of columns take references of the Primary/Unique key of the same or another table.
FOREIGN KEY is called a child key. A FOREIGN KEY value must match an existing value in the
parent table or be null.
CHECK KEY: Defines a condition that each row must satisfy. A single column can have multiple
CHECK Constraints. During CHECK constraint following expressions is not allowed:
If database objects do not exist at compile-time, we can use dynamic SQL to reference
them.
Certain DDL (Data Definition Language) statements and SCL (Session Control Language)
Statements are not supported by Static SQL in PL/SQL. These statements can be
executed by via dynamic SQL programming.
Dynamic SQL can be used for executing Dynamic PL/SQL block. By using EXECUTE
IMMEDIATE clause, PL/SQL calls can be determined at runtime.
38. What are the packages in PL SQL?
Answer: A Package is a group of related database objects like stored procs, functions, types,
triggers, cursors, etc. that are stored in the Oracle database. It is a kind of library of related
objects which can be accessed by multiple applications if permitted.
For example, you could run different applications on a grid of several linked database servers.
When reports are due at the end of the month, the database administrator could automatically
provision more servers to that application to handle the increased demand.
Grid computing uses sophisticated workload management that makes it possible for applications
to share resources across many servers. Data processing capacity can be added or removed on
demand, and resources within a location can be dynamically provisioned. Web services can
quickly integrate applications to create new business processes.
Set sql_trace=true with alter session command. This will generate a trace file for all sql
commands issued by your user session. This is known as a level-1 trace file. One can also create
super detailed level-4 trace files with additional details if the need be. These files are stored in the
form $ORACLE_SID_ora_xxx.trc in the trace directory, where xxx is a sequential number.
To create a level-4 detailed trace file, we need to know the SID and SERIAL# for the session to
trace.
Eg: to trace for a session for SID 5:
Connect system as sysdba;
oradebug SETOSPID 5;
oradebug EVENT 10046 TRACE NAME CONTEXT FOREVER, LEVEL4
43. Explain the character, number, and date function in detail?
Answer: Character functions: accept character input and return both character and number
values. Types of character function are:
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.
Types of subqueries:
Single-Row Subquery: Queries that return only one row from the inner select statement. Single-
row comparison operators are: =, >, >=, <, <=, <>
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 include: IN, ANY, ALL.
46. What is the difference between USER TABLES and DATA DICTIONARY?
Answer: 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.
48. What are Constraints? How many types of constraints are there?
Answer: 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. You can define any constraint at the table level except
NOT NULL which is defined only at the column level.
49. What is a difference between ON DELETE CASCADE and ON DELETE SET NULL?
Answer: 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
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 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 occur.