Monday, July 8, 2019

List Of Most Important Oracle Interview Questions

Q #1) What is Oracle and what are its different editions?
Ans: Oracle is one of the popular database provided by Oracle Corporation, which works on relational management concepts and hence it is referred as Oracle RDBMS as well.
It is widely used for online transaction processing, data warehousing, and enterprise grid computing.
Q #2) How will you identify Oracle Database Software Release?
Ans: Oracle follows a number of format for every release.
For Example, release 10.1.0.1.1 can be referred as below mentioned:
10: Major DB Release Number
1: DB Maintenance Release Number
0: Application Server Release Number
1: Component Specific Release Number
1: Platform Specific Release Number
Q #3) How will you differentiate between VARCHAR & VARCHAR2?
Ans: Both VARCHAR & VARCHAR2 are Oracle data types that are used to store character strings of variable length.
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.
Q #4) What is the difference between TRUNCATE & DELETE command?
Ans: Both the commands are used to remove data from a database.
The finer differences between the two include:
  • TRUNCATE is a DDL operation while DELETE is a DML operation.
  • TRUNCATE drops the structure of a database and hence cannot be rolled back while DELETE command can be rolled back.
  • The TRUNCATE command will free the object storage space while the DELETE command does not.
Q #5) What is meant by RAW datatype?
Ans: RAW datatype is used to store variable-length binary data or byte strings.
The difference between RAW & VARCHAR2 datatype is that PL/SQL does not recognize this data type and hence, cannot do any conversions when RAW data is transferred to different systems. This data type can only be queried or inserted in a table.
Syntax: RAW (precision)
Q #6) What is meant by Joins? List out the types of joins.
Ans: Joins are used to extract data from multiple tables using some common column or condition.
There are various types of Joins as listed below:
  • INNER JOIN
  • OUTER JOIN
  • CROSS JOINS or CARTESIAN PRODUCT
  • EQUI JOIN
  • ANTI JOIN
  • SEMI JOIN
Q #7) What is the difference between SUBSTR & INSTR functions?
Ans: SUBSTR function returns the sub-part identified by numeric values from the provided string.
Example: [Select SUBSTR (‘India is my country’, 1, 4) from dual] will return “Indi”.
INSTR will return the position number of the sub-string within the string.
Example: [SELECT INSTR (‘India is my country’, ‘a’) from dual] will return 5.
Q #8) How can we find out the duplicate values in an Oracle table?
Ans: We can use the below example query to fetch the duplicate records.
SELECT EMP_NAME, COUNT (EMP_NAME)
FROM EMP
GROUP BY EMP_NAME
HAVING COUNT (EMP_NAME) > 1;
Q #9) How does the ON-DELETE-CASCADE statement work?
Ans: Using ON DELETE CASCADE will automatically delete a record in the child table when the same is deleted from the parent table. This statement can be used with Foreign Keys.
We can add ON DELETE CASCADE option on an existing table using the below set of commands.
Syntax:
ALTER TABLE CHILD_T1 ADD CONSTRAINT CHILD_PARENT_FK REFERENCES
PARENT_T1 (COLUMN1) ON DELETE CASCADE;
Q #10) What is a NVL function? How can it be used?
Ans: NVL is a function, which helps the user to substitute a value if null is encountered for an expression.
It can be used as the below syntax.
[NVL (Value_In, Replace_With)]
Q #11) What is the difference between a Primary Key & a Unique Key?
Ans:  Primary key is used to identify each table row uniquely, while a Unique Key prevents duplicate values in a table column.
Given below are few differences:
  • The primary key can be only one on the table while unique keys can be multiple.
  • The primary key cannot hold null value at all while Unique key allows multiple null values.
  • The primary key is a clustered index while a unique key is a non-clustered index.
Q #12) How TRANSLATE command is different from REPLACE?
Ans: TRANSLATE command translates characters one by one in the provided string with the substitution character. REPLACE will replace a character or a set of characters with a complete substitution string.
Example:
TRANSLATE (‘Missisippi’,’is’,’15) => M155151pp1
REPLACE (‘Missisippi’,’is’,’15) =>  M15s15ippi
Q #13) How can we find out the current date and time in Oracle?
Ans: We can find the current Date & Time using SYSDATE in Oracle.
Syntax:
SELECT SYSDATE into CURRENT_DATE from dual;
Q #14) Why do we use COALESCE function in Oracle?
Ans: COALESCE function is used to return the first non-null expression from the list of arguments provided in the expression. Minimum two arguments should be there in an expression.
Syntax:
COALESCE (expr 1, expr 2, expr 3…expr n)
Q #15) How will you write a query to get a 5th RANK student from a table STUDENT_REPORT?
Ans: The Query will be as follows:
SELECT TOP 1 RANK
FROM (SELECT TOP 5 RANK
FROM STUDENT_REPORT
ORDER BY RANK DESC) AS STUDENT
ORDER BY RANK ASC;
Q #16) When do we use GROUP BY clause in a SQL Query?
Ans: GROUP BY clause is used to identify and group the data by one or more columns in the query results. This clause is often used with aggregate functions like COUNT, MAX, MIN, SUM, AVG etc.
Syntax:
SELECT COLUMN_1, COLUMN_2
FROM TABLENAME
WHERE [condition]
GROUP BY COLUMN_1, COLUMN_2
Q #17) What is the quickest way to fetch the data from a table?
Ans: The quickest way to fetch the data would be to use ROWID in the SQL Query.
Q #18) Where do we use DECODE and CASE Statements?
Ans: Both DECODE & CASE statements will function like IF-THEN-ELSE statement and they are the alternatives for each other. These functions are used in Oracle to transform the data values.
Example:
DECODE Function 
Select ORDERNUM,
DECODE (STATUS,'O', ‘ORDERED’,'P', ‘PACKED,’S’,’SHIPPED’,’A’,’ARRIVED’)
FROM ORDERS;
CASE Function
Select ORDERNUM
, CASE (WHEN STATUS ='O' then ‘ORDERED’
WHEN STATUS ='P' then PACKED
WHEN STATUS ='S' then ’SHIPPED’
ELSE ’ARRIVED’) END
FROM ORDERS;
Both the commands will display Order Numbers with respective Status as,
If,
Status O= Ordered
Status P= Packed
Status S= Shipped
Status A= Arrived
Q #19) Why do we need integrity constraints in a database?
Ans: Integrity constraints are required to enforce business rules so as to maintain the integrity of the database and prevent the entry of invalid data into the tables. With the help of the below-mentioned constraints, relationships can be maintained between the tables.
Various integrity constraints available include Primary Key, Foreign Key, UNIQUE KEY, NOT NULL & CHECK.
Q #20) What do you mean by MERGE in Oracle and how can we merge two tables?
Ans: MERGE statement is used to merge the data from two tables. It selects the data from the source table and inserts/updates it in the other table based on the condition provided in the MERGE query.
Syntax:
MERGE INTO TARGET_TABLE_1
USING SOURCE_TABLE_1
ON SEARCH_CONDITION
WHEN MATCHED THEN
INSERT (COL_1, COL_2…)
VALUES (VAL_1, VAL_2…)
WHERE <CONDITION>
WHEN NOT MATCHED THEN
UPDATE SET COL_1=VAL_1, COL_2=VAL_2…
WHEN <CONDITION>
Q #21) What is the use of Aggregate functions in Oracle?
Ans: Aggregate functions perform summary operations on a set of values to provide a single value. There are several aggregate functions that we use in our code to perform calculations.
Few of them are listed below:
  • AVG
  • MIN
  • MAX
  • COUNT
  • SUM
  • STDEV
Q #22) What are the set operators UNION, UNION ALL, MINUS & INTERSECT meant to do?
Ans: 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 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.
Q #23) Can we convert a date to char in Oracle and if so, what would be the syntax?
Ans: We can use the TO_CHAR function to do the above conversion.
The syntax will be as follows:
[SELECT to_char (to_date ('30-01-2018′, ‘DD-MM-YYYY'), ‘YYYY-MM-DD') FROM dual;]
Q #24) What do you mean by a database transaction & what all TCL statements are available in Oracle?
Ans: 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.
The set of statements include:
  • COMMIT: Used to make a transaction permanent.
  • ROLLBACK: Used to roll back the state of DB to last the commit point.
  • SAVEPOINT: Helps to specify a transaction point to which rollback can be done later.
Q #25) What do you understand by a database object? Can you list a few of them?
Ans: 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.
Q #26) What is a Nested table and how is it different from a normal table?
Ans: A nested table is a database collection object, which can be stored as a column in a table. While creating a normal table, an entire nested table can be referenced in a single column. Nested tables have only one column with no restriction of rows.
Example:
CREATE TABLE EMP (
EMP_ID NUMBER,
EMP_NAME  TYPE_NAME)
Here we are creating a normal table as EMP and referring a nested table TYPE_NAME as a column.
Q #27) Can we save images in a database and if yes, how?
Ans: BLOB stands for Binary Large Object, which is a datatype that is generally used to hold images, audio & video files or some binary executables.
This datatype has the capacity of holding data up to 4 GB.
Q #28) What do you understand by database schema and what does it hold?
Ans: 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.
Q #29) What is a Data Dictionary and how can it be created?
Ans: Whenever a new database is created, a database specific data dictionary gets created by the system. This dictionary maintains all the metadata related to the database and owned by the SYS user.
It has a set of read-only tables and views and it is physically stored in the SYSTEM tablespace.
Q #30) What is a View and how is it different from a table?
Ans: A view is a user-defined database object that is used to store the results of a SQL query, which can be referenced later. Views do not store this data physically but as a virtual table, hence it can be referred as a logical table.
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.
Q #31) What is meant by a deadlock situation?
Ans: Deadlock is a situation when two or more users are simultaneously waiting for the data, which is locked by each other and hence, results in all blocked user sessions.
Q #32) What is meant by an index?
Ans: An index is a schema object, which is created to search the data efficiently within the table. Indexes are usually created on certain columns of the table, which are accessed the most.
Indexes can be clustered or non-clustered.
Q#33) What is a Role in Oracle database?
Ans: Giving access to individual objects to the individual users is a tough administrative task. In order to make this job easy, a group of common privileges is created in a database, which is known as Role. The role, once created can be assigned to or revoked from the users by using Grant & Revoke command.
Syntax: 
CREATE ROLE READ_TABLE_ROLE;
GRANT SELECT ON EMP TO READ_TABLE_ROLE;
GRANT READ_TABLE_ROLE TO USER1;
REVOKE READ_TABLE_ROLE FROM USER1;
Q #34) What are the attributes that are found in a cursor?
Ans: A cursor has various attributes as mentioned below:
(i) %FOUND:
  • Returns INVALID_CURSOR if the cursor has been declared but closed.
  • Returns NULL if fetch has not happened but the cursor is open only.
  • Returns TRUE if the rows are fetched successfully and FALSE if no rows are returned.
(ii) NOT FOUND:
  • Returns INVALID_CURSOR if the cursor has been declared but closed.
  • Returns NULL if fetch has not happened but the cursor is open only.
  • Returns FALSE if rows are fetched successfully and TRUE if no rows are returned
(iii) %ISOPEN: Returns TRUE if the cursor is OPEN else FALSE
(iv) %ROWCOUNT: Returns the count of fetched rows.
Q #35) Why do we use %ROWTYPE & %TYPE in PLSQL?
Ans: %ROWTYPE & %TYPE are the attributes in PL/SQL which can inherit the datatypes of a table defined in a database. The purpose of using these attributes is to provide data independence and integrity.
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.
Q #36) Why do we create Stored Procedures & Functions in PL/SQL and how are they different?
Ans: 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.

Stored Procedures
Functions
SPs may or may not return a value and can return multiple values as well.Function will always return only single value.
SPs can include DML statements like insert, update & delete.We cannot use DML statements in a function.
SPs can call functions.Functions cannot call stored procedures.
SPs support exception handling using Try/Catch block.Functions does not support Try/Catch block.
Q #37) What are the parameters that we can pass through a stored procedure?
Ans: We can pass IN, OUT & INOUT parameters through a stored procedure and they should be defined while declaring the procedure itself.
Q #38) What is a trigger and what are its types?
Ans: A trigger is a stored program which is written in such a way that it gets executed automatically when some event occurs. This event can be any DML or a DDL operation.
PL/SQL supports two types of triggers:
  • Row Level
  • Statement Level
Q #39) How will you distinguish a global variable with a local variable in PL/SQL?
Ans: 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.
Q #40) What are the packages in PL SQL?
Ans: A Package is a group of related database objects like stored procs, functions, types, triggers, cursors etc. that are stored in Oracle database. It is a kind of library of related objects which can be accessed by multiple applications if permitted.
PL/SQL Package structure consists of 2 parts: package specification & package body.

No comments:

Post a Comment

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...