Google+ Followers


Friday, July 6, 2012

Oracle SQL & PL/SQL Interview Questions

How to display row number with records?
Select rownum, ename from emp;

How to view version information in Oracle?

Select banner from v$version;

How to find the second highest salary in emp table?

select min(sal) from emp a
where 1 = (select count(*) from emp b where a.sal < b.sal) ;

How to delete the duplicate rows from a table?

create table t1 ( col1 int, col2 int, col3 char(1) );
insert into t1 values(1,50, ‘a’);
insert into t1 values(1,50, ‘b’);
insert into t1 values(1,89, ‘x’);
insert into t1 values(1,89, ‘y’);
insert into t1 values(1,89, ‘z’);
select * from t1;


delete from T1
where rowid <> ( select max(rowid)
from t1 b
where b.col1 = t1.col1
and b.col2 = t1.col2 ) 3 rows deleted.

select * from t1;
will do it.
How to select a row using indexes?
You have to specify the indexed columns in the WHERE clause of query.
How to select the first 5 characters of FIRSTNAME column of EMP table?
select substr(firstname,1,5) from emp
How to concatenate the firstname and lastname from emp table?
select firstname ‘ ‘ lastname from emp
What's the difference between a primary key and a unique key?
Primary key does not allow nulls,Unique key allow nulls.
What is a self join?
A self join joins a table to itself.


SELECT a.last_name Employee, b.last_name Manager
FROM employees a, employees b
WHERE b.employee_id = a.manager_id;
What is a transaction and ACID?
Transaction - A transaction is a logical unit of work. It must be commited or rolled back.
ACID - Atomicity, Consistency, Isolation and Duralbility, these are properties of a transaction.
How to add a column to a table?
alter table t1 add sal number;
alter table t1 add middle_name varchar(20);
Is it possible for a table to have more than one foreign key ?
A table can have any number of foreign keys. It can have only one primary key .
How to display number value in words?
SQL> select sal, (to_char(to_date(sal,'j'), 'jsp')) from emp;
What is candidate key, alternate key, composite key.
Candidate Key A candidate key is one that can identify each row of a table uniquely. Generally a candidate key becomes the primary key of the table.
Alternate KeyIf the table has more than one candidate key, one of them will become the primary key, and the rest are called alternate keys.
Composite Key: - A key formed by combining at least two or more columns is called composite key.
What's the difference between DELETE TABLE and TRUNCATE TABLE commands? Explain drop command.
Both Delete and Truncate will leave the structure of the table. Drop will remove the structure also.

  1. Example

    If tablename is T1.
    To remove all the rows from a table t1.
    Delete t1
    Truncate table t1
    Drop table t1.
  2. Truncate is fast as compared to Delete. DELETE will generate undo information, in case of rollback, but TRUNCATE will not.
  3. Full Table scan and index fast scan read data blocks up to high water mark and truncate resets high water mark but delete does not.So full table scan after Delete will not improve but after truncate it will be fast.
  4. Delete is DML. Because truncate is a DDL, it performs implicit commit. You cannot rollback a truncate. Any uncommitted DML changes will also be committed with the TRUNCATE.
  5. You cannot specify a WHERE clause in the TRUNCATE statement, but you can specify that in Delete.
  6. When you truncate a table the storage for the table and all the indexes can be reset back to its initial size,but a Delete will never shrink the size of the a table or its indexes.
About Dropping
Dropping a table removes the data and definition of the table. The indexes, constraints, triggers, and privileges on the table are also dropped. The action of dropping a table cannot be undone. The views, materialized views or other stored programs that reference the table are not dropped but they are marked as invalid.
Explain the difference between a FUNCTION, PROCEDURE and PACKAGE.
Procedures and functions are stored in compiled form in database.
Functions take zero or more parameters and return a value. Procedures take zero or more parameters and return no values.
Both functions and procedures can take or return zero or more values through their parameter lists.
Another difference between procedures and functions, other than the return value, is how they are called. Procedures are called as stand-alone executable statements:
Functions can be called anywhere in an valid expression :
1) IF (tell_salary(empno) < 500 ) THEN … 2) var1 := tell_salary(empno); 3) DECLARE var1 NUMBER DEFAULT tell_salary(empno); BEGIN …
Packages contain function , procedures and other data structures.
There are a number of differences between packaged and non-packaged PL/SQL programs.Package The data in package is persistent for the duration of the user’s session.The data in package thus exists across commits in the session.
If you grant execute privilege on a package, it is for all functions and procedures and data structures in the package specification. You cannot grant privileges on only one procedure or function within a package.You can overload procedures and functions within a package, declaring multiple programs with the same name. The correct program to be called is decided at runtime, based on the number or datatypes of the parameters.
Describe the use of %ROWTYPE and %TYPE in PL/SQL
%ROWTYPE associates a variable to an entire table row.
The %TYPE associates a variable with a single column type.
What are SQLCODE and SQLERRM and why are they important for PL/SQL developers?
SQLCODE returns the current database error number. These error numbers are all negative, except NO_DATA_FOUND, which returns +100.
SQLERRM returns the textual error message.. These are used in exception handling.
How can you find within a PL/SQL block, if a cursor is open?
By the Use of %ISOPEN cursor variable.
How do you debug output from PL/SQL?
By the use the DBMS_OUTPUT package.
By the use of SHOW ERROR command, but this only shows errors.
The package UTL_FILE can also be used.
What are the types of triggers?
  • Use Row and Statement Triggers
  • Use INSTEAD OF Triggers
    Explain the usage of WHERE CURRENT OF clause in cursors ?
    It refers to the latest row fetched from a cursor in an update and delete statement.
    Name the tables where characteristics of Package, procedure and functions are stored ?
    User_objects, User_Source and User_error.
    What are two parts of package ?
    They consist of package specification, which contains the function headers, procedure headers, and externally visible data structures. The package also contains a package body, which contains the declaration, executable, and exception handling sections of all the bundled procedures and functions.
    What are two virtual tables available during database trigger execution ?
    The table columns are referred as OLD.column_name and NEW.column_name.
    For INSERT only TRIGGERS NEW.column_name values ARE only available.
    For UPDATE only TRIGERS OLD.column_name NEW.column_name values ARE only available.
    For DELETE only TRIGGERS OLD.column_name values ARE only available.v
    What is Overloading of procedures ?
    What are the return values of functions SQLCODE and SQLERRM ?
    SQLCODE returns the latest code of the error that has occurred.
    SQLERRM returns the relevant error message of the SQLCODE.
    Is it possible to use Transaction control Statements such a ROLLBACK or COMMIT in Database Trigger ? Why ?
    It is not possible.,because of the side effect to transactions. You can use them indirectly by calling procedures or functions .
    What are the modes of parameters that can be passed to a procedure ?
    IN, OUT, IN-OUT parameters.


    Oracle PL/SQL Interview questions:
    1. what is the difference between database trigger and application trigger?

    2. what are the file utilit comands used in PL/SQL procedures? 

    3. what is a cluster and what is the real time use and business reasons to use Clustering 

    4. In PL/SQL if we write select statement with INTO clause it may return two exceptions NO_DATA_FOUND or TOO_MANY_ROW . To do you avoid these execeptions. How do you write SQL statement in alternative way? 

    5. what is dense_rank function and it's usage ? 

    6. What is HIGH WATERMARK?I got to know that it is reset when the TRUNCATE command is executed on a table. 

    7. explian rowid,rownum?what are the psoducolumns we have? 

    8. 1)what is the starting "oracle error number"?2)what is meant by forward declaration in functions? 

    9. what is the difference between database server and data dictionary 

    10. Hi,Can anyone tell me the difference between instead of trigger, database trigger, and schema trigger?Thanks. 

    11. what is difference b/w stored procedures and application procedures,stored function and application function.. 

    12. State the difference between implict and explict cursor's 

    13. what is pragma? can any one give me the example of autonomous Transaction ?can we change the order of procedure parameter while calling procedure? 

    14. What is the data type of Null? 

    15. What is autonomous Transaction? Where are they used? 

    16. Details about FORCE VIEW why and we can use 

    17. How can one view all the procedures,functions,triggers and packages created by the user 

    18. What is the difference between User-level, Statement-level and System-level Rollback? Can you please give me example of each? 

    19. What happens when DML Statement fails?A.User level rollbackB.Statement Level RollbackC.Sustem Level Rollback 

    20. Why Functions are used in oracle ?Can Functions Return more than 1 values?Why Procedures are used in oracle ?What are the Disadvantages of packages?What are the Global Variables in Packages? 
    21. Is there any limitation on no. of triggers that can be created on a table? 

    22. what is p-code and sourcecode ? 

    23. What are ref cursors ? 

    24. Which type of binding does PL/SQL use? 

    25. Talk about "Exception Handling" in PL/SQL? 

    26. What are the return values of functions SQLCODE and SQLERRM ? 

    27. What are advantages fo Stored Procedures / Extensibility,Modularity, Reusability,&
    What are advantages fo Stored Procedures / Extensibility,Modularity, Reusability, Maintainability and one time compilation. 
    28. What are two parts of package ? 

    29. What is Overloading of procedures ? 

    30. What are the modes of parameters that can be passed to a procedure ? 
    31. What is difference between a PROCEDURE & FUNCTION ? 

    32. What is a stored procedure ? 

    33. Where the Pre_defined_exceptions are stored ? 

    34. What is Raise_application_error ? 

    35. What is Pragma EXECPTION_INIT ? Explain the usage ? 

    36. Is it possible to use Transaction control Statements such a ROLLBACK or COMMIT in Database Trigger ? Why ? 

    37. Explain the usage of WHERE CURRENT OF clause in cursors ? 

    38. What is a cursor for loop ? 

    39. What is nested table in Oracle and and difference between table and nested table 

    40. What are the components of a PL/SQL block ? 
    41. What do you mean by OCI, Data guard and Advance queue responsibilities for a Oracle developers? 

    42. How do you encrypt the function to prevent accessing from users without specific permission. ? 

    43. HI,What is Flashback query in Oracle9i...? 

    44. Oracle refcursor and procedure 

    45. Convert SQL to Oracle Procedure using cursor 

    46. I want to insert the following information in userAction table:Which user execute which query on which date?the userAction table contains the foolowing attributes:USER DATE QUERYplease write to me how to resolve this problem? 

    47. what are purity rules for functions? why they use ? what effects if not follow these rules? 

    48. In function and procedure the parameter p***is "call by value" or "call by referenc
    In function and procedure the parameter p***is "call by value" or "call by reference"? 

    49. What can be the Maximum size of a plsql block? 

    50. Compare EXISTS and IN Usage with advantages and disadvantages. 
    51. Which two statements are true?A. A function must return a value.B. A procedure must return a value.C. A function executes a PL/SQL statement.D. A function is invoked as part of an expression.E. A procedure must have a return Data 

    52. Oracle extract records from temporary table 

    53. Pragma Init Exception 

    54. What are the disadvantages of Packages and triggers?? 

    55. Hi, How do we display the column values of a table using cursors without knowing the column names inside the loop? 

    56. What will happen after commit statement ? 

    57. What is PL/SQL ? 

    58. 1.How to display current Date & Time in Pl/Sql2.How to use DML in Procedure? 

    59. How do you call procedure have a DDL or commit/rollback statement from a trigger? 

    60. Oracle Cursor types 
    61. Suppose I have 2 triggers on table T, tr1- a before insert trigger & tr2- a before update trigger.tr1 has update (T) statement inside body of tr1andtr2 has insert (T) statement inside body of tr2Now, I'm tring to insert a row into T.What will hppn?? 

    62. What is difference between a Cursor declared in a procedure and Cursor declared in a package specification ? 

    63. what is diffrence between IS and AS in procedure? 

    64. Hi Friends!! Can anybody answer what are the constraints on Mutating tables? How to remove the mutating errors in triggers? 

    65. What are mutating tables? 
    66. Write sample code that can create a hierachical set of data without using a start with and connect by clause in PL/SQL 

    67. how can i import .dmp file in lower version of oracle from higher version ? 
    68. how can i get set identity for last coloumn of the table. 

    69. What is the basic structure of PL/SQL ? 

    70. you have compiled some PL/SQL packages in your schema, and found aome errors in one do you find which procedure produced the error?how do you find which section of the code produced the error and look at? 
    71. char(20) = 'name' varchar2(20)='name' When comparing these two values, are
    char(20) = 'name' varchar2(20)='name' When comparing these two values, are the spaces padded in char are considered or not? Are both values equal? 

    72. What is the difference between right join and right outer join.. 

    73. What is the disadvantage of out paramter in functions 

    74. What is the need for using function purity in pl/sql 

    75. What is the difference between using IS and AS while creating a procedure, function package and package body? 

    76. What are the restrictions on Functions ? 

    77. What is PL/SQL table? SNO MARK ------- ------------------1 592 403 ‘A’4 60 Write a single query to I) Sorted Marks II)First mark III) replace the mark ‘A’ with 0(zero)? 

    78. Without closing the cursor, If you want to open it what will happen. If error, get what is the error? 

    79. What are the components of a PL/SQL Block ? 

    80. Is it possible to use commit or rollback in exception section. 
    81. Why DUAL table is not visible? 

    82. What are the PL/SQL Statements used in cursor processing ? 

    83. What are % TYPE and % ROWTYPE ? What are the advantages of using these over datatypes? 

    84. how can we avoid duplicate rows. without using distinct command 
    85. can procedures have parameters 

    86. How to return more than one value from a function?What are the types of triggers?What are the featu
    How to return more than one value from a function?What are the types of triggers?What are the features of oracle 9i 

    87. How can I speed up the execution of query when number of rows in the tables increased 

    88. 1.What is bulk collect?2.What is instead trigger3.What is the difference between Oracle table & PL/SQL table?4.What R built in Packages in Oracle?5.what is the difference between row migration & row changing? 

    89. what is diff between strong and weak ref cursors 

    90. 1)any one can tell me,suppose we have 1000 of records,ok.then we want to update only 500 records,how can we solve this problem?2)how many types of "explicit cursors" we have?
    91. why do we need to create a force view?what is its purpose?give some examples? 

    92. What is a cursor ? Why Cursor is required ? 

    93. What is materialized view? 

    94. What happens if a procedure that updates a column of table X is called in a database trigger of the same table ? 

    95. What will happen to an anonymus block,if there is no statement inside the block?eg:-declarebeginend; 

    96. Can we have same trigger with different names for a table?eg: create trigger trig1after insert on tab1;andeg: create trigger trig2after insert on tab1;If yes,which trigger executes first. 

    97. difference between truncate and delete 

    98. What are the two parts of a procedure ? 

    99. How to reduce the the burden/main memory on database if i am using refcursor to hold large data to increase performance. 

    100. Name the tables where characteristics of Package, procedure and functions are stored ? 
    101. How packaged procedures and functions are called from the following? 

    102. Explain how procedures and functions are called in a PL/SQL block ? 

    103. How many types of database triggers can be specified on a table ? What are they ? 

    104. What will be the impact of replacing an API call with a stored PL/SQL call? 

    105. How PL SQL is different from T-SQL 

    106 > Select Count(*) from T1 where a=10 3> Select count(*) from T1 where b=20 11Now, What will b the O/P of the count(*) from T1 where a=10 or b=20.............................. 

    107. What is the purpose of FORCE while creating a VIEW 

    108. What is an Exception ? What are types of Exception ? 

    109. What is the output of the following pl/sql block ?declare v_empno emp.empno%type;begin select empno into v_empno from emp where empno = 10;exception when others then dbms_output.put_line ( 'no data found'); when no_data_found then dbms_output.put_line ( 'ther is no data found ');end; 

    110. Explain the two type of Cursors ? 
    111. What is the difference between private packages and public package . what is the difference in declaration of these 2 packages. 

    112. how to avoid the mutating error with sample program 

    113. Give the structure of the procedure ? 

    114. Explain about CURSOR and REF CURSUR with real time scenario where this can be used. 

    When using a count(distinct) is it better to use a self-join or temp table to find redundant data, and provide an example? 

    116. How do you set table for read only access ? 

    117. what is the use of nocopy parameter in oracle procedure 

    118. What is CODEX function? 
    119. can we use commit in trigger and cursors? 

    120. Why we use instead of trigger. what is the basic structure of the instead of trigger. Explain specific business reason of it's use 
    121. how to create a constraint for a tablecolumn which is already created 

    122. How to disable a trigger for a particular table ? 

    123. Force View 

    124. How do you debug the PL/SQL ? 

    125. What is mutatinig trigger? How do you avoid mutating trigger? 

    126. What is a purity level? How it is should be taken into consideration when your writing any database objects i.e., trigger,function, procedure etc., 

    127. how to trace the errors in pl/sql block code.. 

    128. How to get the 25th row of a table. 

    129. if there is an index including three columns A, B and C. And if we issue a query in which where clause uses only column B....will the index be useful??and what if the where clause only has coulmn A..will the index b useful?? 

    130. What is difference between PL/SQL tables and arrays? 
    131. What is the use of NOCOPY Compiler Hint while writing PL/SQL procedures/subprograms??? 

    132. Select from A table through cursor and update B table. If it updates successfully then insert into another table. Handled every type of exception in the code? 

    133. Is it possible create table in procedure or function? If Not Why? 

    134. what are the advantages & disadvantages of packages ? 


    136. Suppose thr are 10 DMLs(insert,update,delete ) in the main section of the PL/SQL block .The exception in them is handled as a whole in the exception handling section .....The error may occur in any of this DMLs ,so how can we understand that which DML has failed ?? 

    137. What are the advantages and disadvantages of DBMS-SQL 

    138. how to insert a music file into the database 

    139. What is Atomic transaction? 

    140. what is the order of execution if there is a statement level and row level trigger on a same table? 

    141. Explain, Is it possible to have same name for package and the procedure in that package. 

    142. How to trace PL/SQL Package?How to trace PL/SQL procedures?How to trace SQL statement?what is DBMS_TRACE? How to use?SET AUTOTRACE ON; ?If anyone tell me how we can use trace and create log that would be great? 


    144. HiWhile creating a table, what is the difference between VARCHAR2(80) and VARCHAR2(80 BYTE)? 

    145. How can i see the time of execution of a sql statement? 

    146. what happens when commit is given in executable section and an error occurs ?please tell me what happens if exception block is committed at the last? 

    147. What are the Limitations of Packages,views,procedures?What is the maximum number of subprograms inside a package? 

    148. what is difference between varray and nested table.can u explain in brief and clear my these concepts.also give a small and sweet example of both these. 

    149. Wheather a Cursor is a Pointer or Reference? 

    150. How to find the nth hightest record holder from a table 
    151. What is the difference between In, Out, InOut Parameters. Can we p***value or reference or both to the In Out Parameter. 

    152. What is a NOCOPY parameter? Where it is used? 

    153. What is PL/SQL table ? 

    155. Can we create a table using with Procedure or Function?wat is the Mutating trigger error? 

    156. Can e truncate some of the rows from the table instead of truncating the full table. 

    157. What are the Restrictions on Cursor Variables?Thanks Ramki, Hyd, TCS 

    158. How to change owner of a table? 

    159. Mention the differences between aggregate functions and analytical functions clearly with examples? 

    160. how can u create session variable in pakages? 
    161. How can I create a new table by using other two table's values. 

    162. what is the diff between %Rowtype and %type? 

    163. what is the difference between database trigger and schema trigger? 

    164. How to avoid using cursors? What to use instead of cursor and in what cases to do so? 
    165. How to disable multiple triggers of a table at at a time? 

    166 What will the Output for this Coding> Declare Cursor c1 is select * from emp FORUPDATE; Z c1%rowtype;Begin Open C1;Fetch c1 into Z;Commit;Fetch c1 in to Z;end; 

    167. Can we use commit or rollback command in the exception part of PL/SQL block? 

    168. Suppose, I've created a new database DB1 n i've created a table DB1.T1.Now, DESC T1 --> d
    Suppose, I've created a new database DB1 n i've created a table DB1.T1.Now, DESC T1 --> desplaying the table structure butselect * from DB1.T1 ---->giving--> table or view does not exist.. Can any one explain possible reason behind this. 

    169. What are the datatypes a available in PL/SQL ? 

    170. can i change the elements of listitems at runtimes?
    171. Give the structure of the function ? 

    172. pls send the interview qustions from pl/sql, sql, datawarehousing questions. 

    173. What is the difference between a reference cursor and normal cursor ? 


    175. what is difference between Cursor and Ref Cursor. Please give example. 

    176. State the advatage and disadvantage of Cursor's 

    177. can we declare a column having number data type and its scale is larger than pricesionex: column_name NUMBER(10,100), column_name NUMBAER(10,-84) 

    178. what is datatype of x when we say define x in oracle 

    179. How we can create a table in PL/SQL block. insert records into it??? is it possible by some procedure or function?? please give example... 

    180. Can any one explain Perforance Tuning in PL/SQL 
    181. How to display the contents of a current record fetched in a ref cursor 

    182. How to handle exception in Bulk collector? 

    183. What is the DATATYPE of PRIMARY KEY?is it Binary integer..i'm not sure..1.Varchar22.Char3.Binary integer4.Number 

    184. In a Distributed Database System Can we execute two queries simultaneously ? Justify ? 

    185. #1 What are the advantages and disadvantages of using PL/SQL or JAVA as the primary programming tool for database automation.#2 Will JAVA replace PL/SQL? 

    186. Write the order of precedence for validation of a column in a table ? 

    187. 1) Why it is recommonded to use INOUT instead of OUT parameter type in a procedure?2) What happen if we will not assign anything in OUT parameter type in a procedure? 

    188. What is Mutation of a trigger? why and when does it oocur? 

    189. can anybody tell me a sample OCI function which will be able to call from Tourbo cthanx!! 

    190. we have a trigger on data the trigger body we have created a body using dbms_output.put_line(********) ;this should be firedwhen ever trigger executed; 
    191. What is PL/Sql tables?Is cursor variable store in PL/SQL table? 

    192. What type of binding is PL/SQL? 

    193. What steps should a programmer should follow for better tunning of the PL/SQL blocks?Difference between procedure and function?What is the use of ref cursor return type? 

    194. Based on what conditions can we decide whether to use a table or a view or a materialized view ? 

    195. What is Data Concarency and Consistency? 

    196. What is bulk binding please explain me in brief ? 

    197. What is the difference between all_ and user_ tables ? 

    198. what is crosstab 

    199. can i write plsql block inside expection 

    200. What is a database trigger ? Name some usages of database trigger ? Subscribe 
    Database trigger is stored PL/SQL program unit associated with a specific database table. Usages are Audit data modifications,&n
    201. Describe in brief some of the featurs of oracle9i.What is LogMiner? 

    202. What happens when a package is initialized ? 

    203. What are the cursor attributes used in PL/SQL ? 

    204. What is difference between % ROWTYPE and TYPE RECORD ? 

    205. What are two virtual tables available during database trigger execution ? 

    1. wonderful information, I had come to know about your blog from my friend nandu , hyderabad,i have read atleast 7 posts of yours by now, and let me tell you, your website gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you! Thanks a ton once again, Regards, sql and plsql difference

    2. Hello,
      This information is impressive. I am inspired with your post writing style. Thanks for sharing!