You are on page 1of 24

1.

Delete vs Truncate
Delete Truncate
 DML Command DDL Command (autocommit)
 Can use where condition with delete Cannot use where condition with truncate
 Delete specific rows “where” Condition All rows will be removed from table
 Slower, because it uses undo Segment Faster
 Triggers will get fired (on delete triggers) No triggers will get invoked
 Wont reclaim the space used by Table Reclaim the space used by table
 Wont reset the high level watermark Reset the high level watermark
 “On Delete Cascade” from “Truncate table <tbl_name> CASCADE”
from oracle 12C from oracle 12C

2. Procedure vs Function
Procedure Function
 Procedure can optionally return value Function must always return a value
using “OUT” & “IN OUT” parameter
 Can not be called from SQL statement Function can be called from SQL stmnt
 Used to implement logical data flow Used for computational purpose
 Procedure can have DML statements **Functions having DML statements
----------------------------------------------------------------cannot be called from SQL stmnts.
----------------------------------------------------------------Autonomous transaction functions can
----------------------------------------------------------------be called from sql statement.
 “RETURN” keyword to return out of procedure “RETURN” keyword to return the value
o Can we compile a function without the RETURN keyword
Yes however, we can not call that particular function from select statement
o How will you identify the functions without the return keywords
By enabling compiler warning setting & by recompiling all the functions
o From Oracle 12.1 version onwards, by using “With” clause, we can write the procedure &
function in the with clause itself & then we can execute as a part of select stmnt, in this case,
function & procedure are not created inside the database it’s just a part of with stmnt.

3. NVL vs NVL2 vs NULLIF vs COALESCE


NVL function will check whether the first input parameter is null, if first input parameter is null then the
function returns the second parameter value as output.

NVL2 function will check the first parameter, and return second parameter if the first parameter is not
null, otherwise returns third parameter.

NULLIF will compare the input parameters ( first and second parameter ), and returns NULL of both are
same, otherwise returns the first parameter value.

COALESCE, returns the first not null expression in the given input parameters.

4. Rank vs Dense Rank


Both RANK() and DENSK_RANK() function gives the ranking within the ordered partition of data, However
if same rank is assigned to more than one record, then the next rank(s) will be skipped by RANK() function,
whereas, the ranks are consecutive for DENSE_RANK. No ranks will be skipped in case of DENSE_RANK
even if more than one row are assigned with same rank.

5. What is SQL, What are the types of SQL statements


SQL stands for Structured query language,
In simpler terms it’s the way to communicate with the database, what I mean by communication here is
, any instruction we want to give to database would be through SQL.

So, these instructions are nothing but commands or statements in SQL.

For instance, if you want to store some new data into database, then you need to use "INSERT"
statement. similarly if you want to remove some data, then you need to use "DELETE" statement.
Depends on the functionality of these instructions or statements, the SQL statements are broadly
classified into five sub categories, or five SUB SQL languages as given below,

DDL ( Data Definition Language )


DML ( Data manipulation Language )
DCL ( Data Control language )
TCL ( Transaction Control Language )
DRL ( Data Retrieval Language )

These language are classified based on the core functionality they perform, Given below commands for
each language.

DDL – CREATE, ALTER, DROP, TRUNCATE


DML – INSERT, UPDATE, DELETE
DCL – GRANT, REVOKE
TCL – COMMIT, ROLLBACK, SAVEPOINT
DRL – SELECT

6. How To Get unique records without using distinct in oracle


select * from Student_list
order by roll_no,stu_name;

With DISTINCT
SELECT DISTINCT ROLL_NO, STU_NAME
FROM STUDENT_LIST
ORDER BY ROLL_NO, STU_NAME;

Using UNIQUE SELECT UNIQUE ROLL_NO, STU_NAME


FROM STUDENT_LIST
ORDER BY ROLL_NO, STU_NAME;
Using GROUP BY SELECT ROLL_NO, STU_NAME
FROM STUDENT_LIST
GROUP BY ROLL_NO, STU_NAME
ORDER BY ROLL_NO, STU_NAME;
Using UNION SELECT ROLL_NO, STU_NAME
FROM STUDENT_LIST
UNION
SELECT ROLL_NO, STU_NAME
FROM STUDENT_LIST;
Using UNION (but with DUAL table) SELECT ROLL_NO,STU_NAME
FROM STUDENT_LIST
UNION
SELECT NULL,NULL
FROM DUAL WHERE 1=2;

Using INTERSECT SELECT ROLL_NO,STU_NAME


FROM STUDENT_LIST
INTERSECT
SELECT ROLL_NO,STU_NAME
FROM STUDENT_LIST;
Using MINUS SELECT ROLL_NO,STU_NAME
FROM STUDENT_LIST
MINUS
SELECT NULL,NULL
FROM DUAL;
** USING ROW NUMBER() SELECT ROLL_NO,STU_NAME FROM (
SELECT ROLL_NO,STU_NAME,
ROW_NUMBER() over(partition by
ROLL_NO,STU_NAME
ORDER BY ROLL_NO,STU_NAME) R
FROM STUDENT_LIST)
where R=1;
Using RANK() SELECT ROLL_NO,STU_NAME FROM (
SELECT ROLL_NO,STU_NAME,
RANK() over(partition by ROLL_NO,STU_NAME
ORDER BY ROWNUM) R
FROM STUDENT_LIST)
where R=1;
Using ROWID with SUBQUERY SELECT * FROM STUDENT_LIST
WHERE ROWID IN (
SELECT MIN(ROWID)
FROM STUDENT_LIST
GROUP BY ROLL_NO,STU_NAME)
ORDER BY ROLL_NO,STU_NAME;
Using ROWID with Corelated SUBQUERY SELECt * FROM STUDENT_LIST A
WHERE 1 = ( SELECT COUNT(1)
FROM STUDENT_LIST B
WHERE A.ROLL_NO = B.ROLL_NO
AND A.STU_NAME = B.STU_NAME
AND A.ROWID >= B.ROWID);

7. what is dual table in oracle


Oracle dual table is automatically created as part of installation in SYS schema. Dual table has one
column called DUMMY of VARCHAR2(1) datatype. This table is useful for computing a constant
expression with select statement

8. what is trigger in oracle


A Trigger is a special PLSQL code, that will be automatically invoked when a specific event occurs in the
database.

The even can be DML event, DDL event or SYSTEM event.

What are the types of triggers?

DML Trigger - This trigger will get fired for DML events like INSERT,UPDATE and DELETE
DDL trigger - This trigger will get fired for DDL events like CREATE, ALTER, DROP, TRUNCATE etc.,
System trigger - This trigger will get fired for system events like LOGON, LOGOFF, STARTUP, SHUTDOWN
etc
Instead of trigger - This trigger is created on view to redirect the DML operations to undelying base
tables
compound trigger - This is a single trigger having blocks for all the DML timing events.
9. What is the difference between dml and ddl

10. What is the difference between decode and case

Decode

* it is a function

* it us used for single column

* it is used to replace more than one string

* it works like as an IF condition but it doesn't allow the relational operators

* it supports only = operator; performance - slow

Case

* case is a statement

* it is used to replace more than one string by using relational operators

* it can be used for multiple columns

* case is extended version of decode; performance – faster


11. What is the difference between replace and translate function
REPLACE() function replaces all occurrences of a specified substring in a string with another.

TRANSLATE() function returns a string with all occurrences of each character in a string replaced by its
corresponding character in another string.

The TRANSLATE() function allows you to make several single-character, one-to-one translations or
substitutions in one operation.

The TRANSLATE() function performs a case-sensitive match

TRANSLATE() Function in Oracle (database.guide)

12. What is the view in oracle? simple view? complex view?

13. Can we use DML and DDL statements inside function?


Function test_fn1 is created
SELECT statement returns 1 as well

In the Function test_fn1, we’re updating the emp table ; function test_fn1 is created
However, with the SELECT statement it throws an error ORA – 14551
This is because, a select statement is supposed to read the data and it is not supposed to change the
state of the database
As a workaround if we want to change the data, then we can use AUTONOMOUS TRANSACTION which
will be a child transaction rather than the main transaction

Now as we made the function as autonomous transaction function, we are able to successfully call the
function even though there’s a DML statement

While creating the function we get an error PLS - 00103


For any PL/sql block(procedure, package, function, trigger), we are unable to give the DDL statement
directly.
Whenever we want to execute DDL commands, we have to use as a part of Dynamic SQL (execute
immediate)
Here test_fn1 is created
But when we’re calling the function through SELECT statement, we get an error ORA – 14552

 commit is not mandatory in this case, DDL stmnts


Function is compiled & it’s successfully invoked & returns 1 as well

Calling the function test_fn1 as a part of expression within a PL/sql block, no error

Calling the function test_fn1 as a part of select statement within a PL/sql block, this returns a error, to
overcome this we need to make the function test_fn1 as a autonomous transaction function.
SELECT statement takes precedence over any PL/sql block
14. Can we use OUT and INOUT parameter in function

15. difference between view and materialized view

16. set operator union, unionall, intersect, minus

17. What is %rowtype and %type

lv_emp_rec can only had 1 row of the table (i.e., scalar)


%rowtype can be used as a part of table or cursor
Whenever, we use %type, if there is any change in datatype of underlying base table, automatically it
will take the updated datatype during the execution.

18. Questions on Index


19. Questions on Index
20. Questions on Index
21. BITMAP index
22. function based index
23. reverse key index
24. Questions on Index

25. Types of DML triggers | Order of trigger Execution if more than one

 Row level

 statement level
26. SQL loader

27. difference between collections: VARRAY, NESTED TABLE, ASSOCIATIVE ARRAY

28. What is Instead of Trigger


Trigger on Views
Instead Of Trigger – write on a view instead of a table where we will say that instead of updating the
view go & update the base table as view itself doesn’t have any data
29. What is exception and how to handle exception in PLSQL
Rather than throwing an error we use exceptions in PL/sql blocks inorder to terminate the program
gracefully and to show more functional & meaningful readable error message
30. What are the types of exceptions

31. What is pragma Exception Init


32. What is raise application error
33. What is SQLCODE and SQLERRM
SQLCODE – returns the number code of the most recent exception
SQLERRM – returns the description of the exception

34. What is SUBQUERY and its types


35. What is Simple Subquery
36. What is Correlated subquery
37. Difference between simple vs corelated subquery
38. NULL and Arithmetic operation

NULL and Order by Clause


When order by clause is used, by default nulls are displayed at the end
To displays nulls at first when order by is used, then
ORDER BY <colmn_name> NULLS (ASC/DESC) FIRST;

39. Difference between IN vs ANY


IN is same as =ANY
>ANY is same as GREATER THAN SMALLEST NUMBER
<ANY is same as LESS THAN GREATEST NUMBER
40. Difference between SQL LOADER and EXTERNAL TABLE

41. RANK and DENSE RANK as Aggregate Function and Analytical Function

 aggregate function
Computing the rank & dense_rank value for a particular value

 analytical function
Computes the rank & dense_rank for the given result set, the advantage is that we can use partition by
clause to do that sub partitioning within the result set & do the ranking & dense_ranking

42. Query was running fine yesterday but its very slow today | Tuning
Since oracle engine will generate the explain plan
based on the available statistics information,
chances are it may come up with wrong execution
plan because of the outdated statistics. This might
be the reason why the query is taking lot of time

As lot of DMLs are performed, data in the underlying the base


tables is drastically changed, which might impact the access
path for ex, till yesterday it was using index to access the
table, but because of this drastic change it might be using full
table scan. This could be another reason.

43. What is cursor and what are the types of cursor in oracle
Implicit Cursor

44. What is Explicit Cursor

DECLARE OPEN FETCH CLOSE

45. Explain Cursor Attributes


46. Explain Parameterized Cursor and For Cursor
Parameterized cursor – we can even pass the parameter as a part of cursor & while opening the cursor,
we need to pass the parameter which gets passed to the cursor & it gets assigned to the parameter
value whatever we have used in the select statement at the run time
For cursor – it takes care of open, fetch, exit & close operations
In case we are not planning to use the same cursor anywhere we can just declare select statement as a
part of for cursor
Explicit cursor FOR LOOP statement
Passing parameters to explicit cursor FOR LOOP statement

47. Explain REF Cursor Strongly Typed Ref Cursor and Weakly typed Ref Cursor

48. Explain FOR UPDATE clause and WHERE CURRENT OF Clause


49. When to use a CURSOR & COLLECTION
50. CURSOR Vs REFCURSOR
CURSOR Vs REFCURSOR what can be Global Cursor
PLSQL Cursor can be global while Ref Cursor can not be

51. What is Compound Trigger and it’s advantages?


A compound trigger combines the following four triggers into one:
- before statement
- before row
- after row
- after statement
So if you have a table with two or more of these you could use a compound trigger to rationalize these.
The most common reasons for wanting to use compound triggers are:
- To avoid the mutating table problem
- To collect the affected rows for processing in batch (e.g. to log them).

52. What is Mutating Trigger


Whenever, we’re doing a DML operation & if the DML operation fire sets the trigger & from within the
body of trigger if we’re trying to read or write the same table then we will get the mutating trigger.
Mutating trigger occurs whenever a row level trigger tries to modify or select data from the table that is
already undergoing change so this is called mutating error, this mutating trigger will get only from the
row level trigger and it is not from any statement level trigger
53. How to Solve Mutating Trigger
54. How to Solve Mutating Trigger Using Compound trigger
55. SQL to Delete duplicate records
delete
from emp a
where rowid > ( select min(rowid) from emp_t b
where a.empno = b.empno and a.ename = b.ename and a.job=b.job and a.sal= b.sal and
a.deptno = b.deptno);

56. Maximum number of triggers on same table


12 different types of triggers can be defined on the same table

all of the BEFORE triggers will fire *in some order*


then, all of the BEFORE FOR EACH ROW will fire *in some order*
then, all of the AFTER for each row *in some order*
then all of the AFTER triggers *in some order*
If your triggers depend on the "firing order", you NEED to (must) consolidate them into a single trigger
(put them in separate procedures, have one trigger that calls them in order)

57. Creating Same Type of trigger on same table


58. What is pragma autonomous transaction
We have to put COMMIT or ROLLBACK within autonomous transaction, otherwise we get run time error
We can define the autonomous transaction in either Procedure, Function, Trigger etc.,

59. pragma autonomous transaction real time use case

 3rd is not recommended

60. What are the advantages of packages


61. constraint with DEFERRABLE NOVALIDATE option
I have a table with duplicate entries in production, I cannot delete existing rows, however I want to
prevent further duplicate rows to get inserted. How can I do?

62. constraint related interview question


Does creating a primary key constraint always create an index  Yes
Will “Dropping Constraint” , will drop underlying index  Yes
Will “Dropping Table” , will drop underlying index  Yes
Can Primary Key create Nonunique index  No

Unique index or Unique Constraint both enforces column uniqueness but when to use what

63. How to Exclude duplicate records while insertion| Error Log Table
I want only the “non-duplicate” records to get inserted, duplicated records should get excluded during
insertion. How can I do
To avoid this,
 We can use Oracle Error Log Table we can achieve this

 We can also use the hint IGNORE_ROW_ON_DUPKEY_INDEX by providing table_name &


primary_key column_name as input
The drawback with this approach is it will not capture the error information any where
 We can also use MERGE

 We can also use INSERT with SUBQUERY

64. ORACLE FORCE VIEW | VIEW WITH CHECK OPTION | VIEW WITH READ ONLY

Write a procedure to create a view for all the tables in the given schema. The view name should be
same as table name suffixed with “_V”

65. Differences between FOR vs FORALL


66. What is MERGE statement
MERGE INTO
target_table
USING source_table
ON search_condition
WHEN MATCHED THEN
UPDATE SET col1 = value1, col2 = value2,...
WHERE <update_condition>
[DELETE WHERE <delete_condition>]
WHEN NOT MATCHED THEN
INSERT (col1,col2,...)
values(value1,value2,...)
WHERE <insert_condition>;

You might also like