Oracle Interview Questions and Answers (32) - Page 1

Wht is Dual Table in Oracle?

It is a dummy table in Oracle with one row and one Varchar2 column whose length is 1.
We can ask for any dummy values from dual as shown under

SQL> Select 1, SysDate From dual;

1 SYSDATE
---------- ---------
1 30-APR-13
What is VARRAY in Oracle?

They are variable length,1D array and store elements of same type as arrays of other languages. We need to specify the size at the time of VArray declaration.

Let us say that a person has 2 mobile numbers. We can store these mobile numbers into a varray. Let us see how.

Step 1:First we need to create a Varray Type as under

Declare

Type Mobile_Numbers_Type IS VARRAY(2) OF NUMBER;
Mobile_Numbers Mobile_Numbers_Type;


Step 2: Then we need to initialize it

Declare 


Type Mobile_Numbers_Type IS VARRAY(2) OF NUMBER;

Mobile_Numbers Mobile_Numbers_Type;

Begin

Mobile_Numbers := Mobile_Numbers_Type(1111111111,1000000000);
End;


Step3:We can access the values as under

SQL> Set ServerOutput On;

SQL> Declare
2
3 Type Mobile_Numbers_Type IS VARRAY(2) OF NUMBER;
4
5 Mobile_Numbers Mobile_Numbers_Type;
6
7 Begin
8
9 Mobile_Numbers := Mobile_Numbers_Type(1111111111,1000000000);
10
11 DBMS_OUTPUT.PUT_LINE('First value is: ' || Mobile_Numbers(1));
12 DBMS_OUTPUT.PUT_LINE('Second value is: ' || Mobile_Numbers(2));
13 End;
14 /
First value is: 1111111111
Second value is: 1000000000

PL/SQL procedure successfully completed.

What is Nested Table in Oracle?

They are 1D array like VArrays and can be use either in a relational table or in functions.Like VArrays, they are use for columns having multiple values.But they does not need any size to be define on them like VArrays and hence are unbound.They are,however, limited to the amount of memeory available.Each row of the nested table should be of the same type.

e.g. Let us say that a person has 2 mobile numbers. We can store these mobile numbers into a Nested Table.

Step 1:First we need to create a Nested Table Type

Declare

Type Mobile_Numbers_Type IS TABLE OF NUMBER;
Mobile_Numbers Mobile_Numbers_Type;


Step 2:Then we need to initialize it

Declare 


Type Mobile_Numbers_Type IS TABLE OF NUMBER;

Mobile_Numbers Mobile_Numbers_Type;

Begin

Mobile_Numbers := Mobile_Numbers_Type(1111111111,1000000000);
End;


Step 3: We can access the values as under

SQL> Set ServerOutput On;

SQL> Declare
2
3 Type Mobile_Numbers_Type IS TABLE OF NUMBER;
4
5 Mobile_Numbers Mobile_Numbers_Type;
6
7 Begin
8
9 Mobile_Numbers := Mobile_Numbers_Type(1111111111,1000000000);
10
11 DBMS_OUTPUT.PUT_LINE('First value is: ' || Mobile_Numbers(1));
12
13 DBMS_OUTPUT.PUT_LINE('Second value is: ' || Mobile_Numbers(2));
14 End;
15 /
First value is: 1111111111
Second value is: 1000000000

PL/SQL procedure successfully completed.

Name some of the inbuilt method of Nested Table of Oracle

a)First : Returns the subscript of the first element in the nested table

b)Last : Returns the subscript of the last element in the nested table.

c)Prior : Returns the subscript of the previous element in the nested table.

d)Next : Returns the subscript of the next element in the nested table.

e)Exists : Returns true if the element at te specified position is found else false

f)Trim : Removes the last N elements from the collection
Name some of the inbuilt methods of VARRAY

a)Count : Returns the number of elements in the collection.

b)Delete : Delete all elements in the collection

c)Extend : Increase the size of the collection by the number specified

d)Exists : Returns true if the element at te specified position is found else false

e)Trim :Removes the last N elements from the collection
What is the purpose of Oracle's VSIZE function?

It returns the number of bytes in the internal representation of expression. If expression is null, it returns null.

SQL> Select EMPNAME, VSize(EMPNAME) VSizeExample

2 From tblemployee;

EMPNAME VSIZEEXAMPLE
-------------------------------------------------- ------------
Deepak Kumar Goyal 18
Shashi Dayal 12
Amitav Mallik 13
Amit Ojha 9
Sumanta Manik 13


It is similar to DataLength function in Sql Server.
What is Associative Array?

Oracle 9i R2 has renamed the index-by tables available in earlier versions of Oracle to Associative Arrays.Like VArrays/Nested Tables, they are also 1D array and can be use either in a relational table or in functions. They can not exist in the database and rather are found only in PL/SQL memory structures.They are use for columns having multiple values.It is basically a two-Column table where the first column is the INDEX while the second column holds the DATA ELEMENT.They are UNBOUND since they don't have any upper limits to grow.
How do Associative Array differs from VARRAY or NESTED tables ?

1.There is no need to extend an Associative Array for adding elements.
2.They don't need to be initialized.
3.The array elements are added in any order and at any position.
Explain Associative Array with an example.

Let us say that a person has 2 houses.So it means he has two residential addresses. We can store these addresses into an Associative Array. Let us see how

First we need to create an Associative Array Type as under. Please note that the Existing type can only be BINARY_INTERGER or VARCHAR.

Declare Type Resedential_Address_Type IS TABLE OF  VARCHAR(20) INDEX BY  VARCHAR(20);

Resedential_Addresses Resedential_Address_Type;


Then assign the value

Declare Type Resedential_Address_Type IS TABLE OF  VARCHAR(20) INDEX BY  VARCHAR(20);

Resedential_Addresses Resedential_Address_Type;

Begin

Resedential_Addresses(1) := '407 St John Street London EC1V 4AD';
Resedential_Addresses(2) := 'Street Address, 64 Newman Street. City, London';
End;


We can access the values as under

SQL> Set ServerOutput On;

SQL> Declare
2 Type Resedential_Address_Type IS TABLE OF VARCHAR(20) INDEX BY VARCHAR(20);
3 Resedential_Addresses Resedential_Address_Type;
4 Begin
5 Resedential_Addresses(1) := '407 St John Street London EC1V 4AD';
6 Resedential_Addresses(2) := 'Street Address, 64 Newman Street. City, London';
7 DBMS_OUTPUT.PUT_LINE('First Value is: ' || Resedential_Addresses(1));
8 DBMS_OUTPUT.PUT_LINE('Second Value is: ' || Resedential_Addresses(2));
9 End;
10 /
First Value is: 407 St John Street London EC1V 4AD
Second Value is: Street Address, 64 Newman Street. City, London

PL/SQL procedure successfully completed.

Tell about some of the Built-In Methods of ASSOCIATIVE ARRAYS

It provides many built in methods to work with e.g.

a)First: Returns the subscript of the first element in the associative array.

b)Last: Returns the subscript of the last element in the associative array.

c)Prior: Returns the subscript of the previous element in the associative array.

d)Next: Returns the subscript of the next element in the associative array.

e)Exists: Returns true if the element at te specified position is found else false

f)Trim: Removes the last N elements from the collection

g)Count: Returns number of elemets in the collection

h)Delete: Delete the element in the collection
What is Compose Function in Oracle?

The Compose function of Oracle accepts a string and returns a Unicode string.It also accepts an expression that can be resolve to a string.

Syntax

Compose(string)

where "string" parameter can be a Char,Nchar,NClob,NCHAR, DUACLOB, VARCHAR2, or NVARCHAR2.

SQL> Select

2 Compose('A' || UniStr('\0300')) GraveAccent
3 ,Compose('A' || UniStr('\0301')) AcuteAccent
4 ,Compose('A' || UniStr('\0302')) CircumFlex
5 ,Compose('A' || UniStr('\0303')) Tilde
6 ,Compose('A' || UniStr('\0308')) Umlaut
7 From Dual;

G A C T U
- - - - -
À Á Â Ã Ä

What is Decompose function in Oracle?

This function of Oracle returns a Unicode string.It is the exact opposite of Compose function.

Syntax

Decompose(string)

where "string" parameter can be a Char,Nchar,NClob,NCHAR, DUACLOB, VARCHAR2, or NVARCHAR2.

SQL> Select

2 DeCompose(Compose('A' || UniStr('\0300'))) Example
3 From Dual;

EX
--
A`

What is the purpose of Ratio_To_Report Function of Oracle?

It is an analytical function that computes the ratio of a value over the total set of values.RATIO_TO_REPORT of 5 over (1,2,3,4,5,6,7,8,9,10) is (5/55) i.e. 0.09090909090909090909090909090909

e.g

SQL> Select EmpName,Salary,Ratio_to_Report(SALARY) Over() As RatioReportExample

2 From tblEmployee
3 Where DeptId = 1;

EMPNAME SALARY RATIOREPORTEXAMPLE
-------------------------------------------------- ---------- ------------------
Arina Biswas 6000 .146305779
Jitesh Mallik 16000 .390148744
Deepak Singh 6890 .168007803
Shashi Bhushan 5120 .124847598
Atithi Salonki 7000 .170690076

What are the benefits of PL/SQL ?

1)Supports for Object-Oriented Programming

2)Multiple SQL statements can be executed

3)Errors can be handle more efficiently

4)An entire block of statements can be send to Oracle engine at a single point of time for processing .

5)We can achieve data abstraction using PL/SQL statements

6)Code reusability etc.
What are different normalization forms?

1NF: Eliminate Repeating Groups
Make a separate table for each set of related attributes, and give each table a primary key. Each field contains at most one value from its attribute domain.

2NF: Eliminate Redundant Data
If an attribute depends on only part of a multi-valued key, remove it to a separate table.

3NF: Eliminate Columns Not Dependent On Key
If attributes do not contribute to a description of the key, remove them to a separate table. All attributes must be directly dependent on the primary key. (Read More Here)
BCNF: Boyce-Codd Normal Form
If there are non-trivial dependencies between candidate key attributes, separate them out into distinct tables.

4NF: Isolate Independent Multiple Relationships
No table may contain two or more 1:n or n:m relationships that are not directly related.

5NF: Isolate Semantically Related Multiple Relationships
There may be practical constrains on information that justify separating logically related many-to-many relationships.

ONF: Optimal Normal Form
A model limited to only simple (elemental) facts, as expressed in Object Role Model notation.

DKNF: Domain-Key Normal Form
A model free from all modification anomalies is said to be in DKNF. Remember, these normalization guidelines are cumulative. For a database to be in 3NF, it must first fulfill all the criteria of a 2NF and 1NF database.
What is a Linked Server?

Linked Servers is a concept in SQL Server by which we can add other SQL Server to a Group and query both the SQL Server dbs using T-SQL Statements. With a linked server, you can create very clean, easy to follow, SQL statements that allow remote data to be retrieved, joined and combined with local data. Stored Procedure sp_addlinkedserver, sp_addlinkedsrvlogin will be used add new Linked Server.
How to change Data-Type of a column in Oracle Database?

With the help of Modify Column,we can change data-type of a column in Oracle database?

It's same as in MySql Database.

Syntax:-
Alter Table table_name

Modify Column column_name datatype.


For Example:-
Alter Table employee_master

Modify Column employee_code varchar(30);

What is the output of the query, select * from Person where rowid in (select decode(mod(rownum,2),0,rowid, null) from Person)

Whenever you want to select alternate records with even numbers then you need to execute the following query,

select * from Person where rowid in (select decode(mod(rownum,2),0,rowid, null) from Person)

The output will be the even number records from your databse like 2, 4, 6 and so on
What is the output of the following query? select * from Person where rowid in (select decode(mod(rownum,2),0,null, rowid) from Person)

When you want to select records that contain row id with odd number, then you need to execute this query.
Which keyword is used to select N no of records in Oracle Database?

NOTE: This is objective type question, Please click question title for correct answer.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories