BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
Difference between all topics in oracle
1. Difference between VARCHAR and VARCHAR2?
VARCHAR VARCHAR2
VARCHAR can store up to 2000 bytes. VARCHAR2 can store up to 4000 bytes of
characters.
If we declare datatype as VARCHAR then it will
occupy space for NULL values.
In the case of VARCHAR2 datatype, it will not
occupy any space for NULL values.
Difference between CHAR and VARCHAR?
CHAR VARCHAR
CHAR stores fixed length character string VARCHAR stores variable length character strings
String length is between 1-2000 String length is between 1-4000
It can hold max 256 character It can hold max 65,535 character
It uses static memory allocations It uses dynamic memory allocations
It is 50% faster than VARCHAR It is slower than CHAR
Difference between Commit and Rollback?
COMMIT ROLLBACK
(i) COMMIT validates the modifications made by
the current transaction.
(i) ROLLBACK erases the modifications made by
the current transaction.
(ii) After execution of COMMIT statement, the
transaction can not be ROLLBACK.
(ii) Once ROLLBACK is executed database
reaches its previous state, I.e. before the execution
of the first statement of the transaction.
(iii) COMMIT occurs when the transaction gets
executed successfully.
(iii) ROLLBACK occurs when the transaction is
aborted in middle of the execution.
(iv) Syntax :- COMMIT; (iv) Syntax :- ROLLBACK;
Difference between Delete and Truncate?
DELETE TRUNCATE
(i) We can Rollback after delete.
Example:-
BEGIN TRAN
DELETE FROM tranTest
SELECT * FROM tranTest
ROLLBACK
SELECT * FROM tranTest
(i) We can't Rollback after performing
Truncate.
Example:-
BEGIN TRAN
TRUNCATE TABLE tranTest
SELECT * FROM tranTest
ROLLBACK
SELECT * FROM tranTest
(ii) Delete does not reset identity of table. (ii). Truncate reset identity of table.
(iii) It locks the table row (Row Level Lock). (iii). It locks the entire table (Table Level Lock)
2. (iv) Its DML(Data Manipulation Language)
command.
(iv) Its DDL(Data Definition Language)
command.
(v) We can use WHERE to filter data to delete. (v) We can't use WHERE clause with it.
(vi) Trigger is fired. (vi) Trigger is not fired while truncate.
(vii) Syntax :
1) DELETE FROM table_name ;
2) DELETE FROM table_name WHERE
example_column_id IN (1,2,3) ;
(vii) Syntax :
1) TRUNCATE TABLE table_name;
Difference between ROWID and ROWNUM ?
ROWID ROWNUM
(i) ROWID is nothing but Physical memory
allocation.
(i) ROWNUM is nothing but the sequence which
is allocated to that data retrieval bunch.
(ii) ROWID is permanent to that row which
identifies the address of that row.
(ii) ROWNUM is temporarily allocated sequence
to the rows.
(iii) ROWID is 16 digit Hexadecimal number
which is uniquely identifies the rows.
(iii) ROWNUM is numeric sequence number
allocated to that row temporarily.
(iv) ROWID returns PHYSICAL ADDRESS of
that row.
(iv) ROWNUM returns the sequence number to
that row.
(v) ROWID is automatically generated unique id of
a row and it is generated at the time of insertion of
row.
(v) ROWNUM is an dynamic value automatically
retrieved along with select statement output.
(vi) ROWID is the fastest means of accessing
data.
(vi) ROWNUM is not related to access of data.
Difference between COUNT and COUNT(*) ?
COUNT(COLUMN_NAME
)
COUNT(*)
(i) Both are aggregate function or group function. (i) Both are aggregate function or group function.
(ii) Counts the entries in a column - ignoring null
values.
(ii) Counts the rows in table - including null
values.
Difference between GROUP BY and ORDER BY?
GROUP BY ORDER BY
(i) Group By is used to form the Group of the set
of the tuples.
(i) Order By is used to arrange the data obtained
as a result of a query in Sorted form.
(ii) Attribute under Aggregate function can not be
in Group By clause.
(ii) Attribute under aggregate can be in Order By
Clause.
(iii) Done on the ground of similarity among
attribute values.
(iii) Done on the ground of ascending order and
descending order.
3. Difference between WHERE and HAVING?
WHERE HAVING
(i) Implemented in row operations. (i) Implemented in column operations.
(ii) Applied to Single row. (ii) Applied to summarized row or groups.
(iii) It only fetches the particular data from
particular rows according to the condition.
(iii) At first, complete data is fetched then
separated according to the condition.
(iv) Cannot appear in WHERE clause. (iv) Can appear in HAVING clause.
(v) SELECT and other statements such as
UPDATE, DELETE or either one of them.
(v) Can't be used without a SELECT statement.
(vi) Act as Pre-filter. (vi) Act as Post-filter.
(vii) GROUP BY comes after WHERE. (vii) GROUP BY comes before HAVING.
Difference between RANK and DENSE_RANK?
RANK DENSE_RANK
(i) Both are analytical function. Skip sequence (i) Both are analytical function. Doesn’t skip
sequence
(ii) If there is any repeated rank it will skip the
next rank.
(ii) If there is any repeated rank it will not skip the
next rank.
(iii) Example :-
Rank
1
1
3
1 is repeated 2 times so It will skip the 2nd
rank
and it will print 3 as next rank.
(iii) Example :-
Dense_Rank
1
1
2
It will not skip any values, It will print in the same
order.
Difference between LEAD and LAG?
LEAD LAG
(i) Both are analytical function. (i) Both are analytical function.
(ii) It will fetch the next value of current row. (ii) It will fetch the previous value of current row.
(iii) Example :-
Lead
10000 , 20000
20000 , 30000
30000 , null
(iii) Example :-
Lag
10000 , null
20000 , 10000
30000 , 20000
Difference between AGGREGATE and ANALYTICAL?
AGGREGATE ANALYTICAL
It is used to give cumulative set of data It is used to analysis set of data
(i) Aggregate function is Single row output. (i) Analytical function is Multi rows output.
4. (ii) we can group data by using group by clause &
filter by having clause
We can group data by using partition by
keyword.
(iii) It will return single value. (iii) It will return more than one value.
(iv) Only one column is specified by using group
clause.
(iv)All columns is specified by using partition
clause.
Column present in select clause should be present
in group clause
Column present in select clause may or may be
present in group clause
slower faster.
(vii) Here we use Sum, Avg, Count, Min, Max (vii) Here we use Rank, Dense_rank, Lead, Lag,
Row_number, First_value and Last_value.
Different between NVL, NVL2 and Coalesce?
NVL NVL2 COALESCE
(i) 2 arguments. (i) 3 arguments. (I) 2 or more arguments.
(ii) If the first argument is null
then display the second argument
else display the first argument.
(ii) If the first argument is null
then display the third argument
else display the second
argument.
(ii) It returns the first not null able
column from the arguments list.
(iii) Syntax :- NVL(1,2) (iii) Syntax :- NVL2(1,2,3) (iii) Syntax :- COALESCE(1,2)
Different between GTT and WITH Clause?
GTT WITH Clause
(i) The main difference is that the temporary table
is a stored table.
(i) It will not saves in database.
(ii) Views can create in GTT (ii) Views cannot create in WITH Clause.
(iii) Index can be create in GTT (iii) Index cannot create in WITH Clause.
Difference between Replace and Translate?
REPLACE TRANSALATE
(i) Replaces entire string at a time. (i) Replaces character one-to-one basis.
(ii) Returns string if no match found. (ii) Returns null if no match found.
(iii) Syntax :- Replace ('Oracle','Ora',123 ) (iii) Syntax:- Translate ('Oracle','Oae',123 )
Difference between INSTR and SUBSTR?
INSTR SUBSTR
(i) The INSTR function provides character
position in a pattern of string.
(i) The SUBSTR function returns a specific
portion of a string.
ii) INSTR is used to find the position of any
particular character in a word which returns
numeric value.
(ii) SUBSTR is used to extract a set of
characters from a string by specifying the
character starting position and end position
and length of characters to be fetched.
5. (iii) INSTR returns numeric. (iii) SUBSTR returns string.
(iv) Syntax :- INSTR('HELLO','E') (iv) Syntax :- SUBSTR('HELLO',2,3)
Difference between View and Materialized View?
VIEW MATERIALIZED VIEW
(i) A View is never stored it is only displayed. (i) A Materialized View is stored on the disk.
(ii) View is the virtual table formed from one or
more base tables or views.
(ii) Materialized view is a physical copy of the
base table.
(iii) View is updated each time the virtual table
(View) is used.
(iii) Materialized View has to be updated manually
or using triggers.
(iv) Slow processing. (iv) Fast processing.
(v) View do not require memory space. (v) Materialized View utilizes memory space.
(vi) No need log file. (vi) It will maintain log files.
(vii) Only select query has saved in data
dictionary table.
(vii) It will saves the output of the select query in
data dictionary table.
(viii) Syntax :-
Create View V As
(viii) Syntax :-
Create Materialized View V Build [clause]
Refresh [clause] On [Trigger] As
Difference between Simple view and Complex View?
SIMPLE VIEW COMPLEX VIEW
(i) Contains only one single base table or is
created from only one table.
(i) Contains more than one base tables or is
created from more than one tables.
(ii) We cannot use group functions like MAX(),
COUNT(), etc.
(ii) We can use group functions.
(iii) Does not contain groups of data. (iii) It can contain groups of data.
(iv) DML operations could be performed through
a simple view.
(iv) DML operations could not always be
performed through a complex view.
(v) INSERT, DELETE and UPDATE are directly
possible on a simple view.
(v) We cannot apply INSERT, DELETE and
UPDATE on complex view directly.
(vi) Simple view does not contain group by,
distinct, pseudo column like ROWNUM, columns
defined by expressions.
(vi) It can contain group by, distinct, pseudo
column like ROWNUM, columns defined by
expressions.
(vii) Does not include NOT NULL columns from
base tables.
(vii) NOTNULL columns that are not selected by
simple view can be included in complex view.
Different between CASE and DECODE?
CASE DECODE
(i) Complies with ANSII SQL (i) Oracle proprietary.
(ii) Can works with logical operators other than
' = ' operator.
(ii) Works only in ' = ' like operator
6. (iii) Can works with predicates and searchable
queries.
(iii) Expressions are scalar values only.
(iv) Needs data consistency. (iv) Data consistency no needed.
(v) NULL = NULL, returns FALSE (v) NULL = NULL, returns TRUE
(vi) Can be used in PL/SQL block and SQL
statements.
(vi) Can be used in SQL Statements.
(vii) Can be Used in parameters while calling a
procedure.
(vii) Cannot be Used in parameters while calling
a procedure.
Difference between IN and EXITS?
IN EXISTS
The answer of IN can be TRUE or FALSE or NULL The answer of EXISTS can be TRUE or FALSE
IN compares values between parent query and sub-query
EXISTS cannot compare values between parent query
and sub-query
IN is used as multiple OR operator
It can be used to determine if any values are returned or
not
IN is faster than EXISTS if sub-query result is less EXISTS is faster than IN if sub-query result is large
NULL cannot be compared using IN condition NULL can be compared using EXISTS condition
In operator test for the particular value in the sub query. Exist operator is a Boolean operator.so it works more
efficient and faster than IN operator as it will be faster
to process Boolean value rather than processing
values itself.
In operator scan all the values inside the IN block. EXISTS quit after 1st occurrence.
In case of a big number of list from the select list IN
operator should not be used.
The exists shall be used with the proper condition this
will definitely make the query optimal query.
'IN' can be used on sub-queries as well as with values. Whereas 'EXISTS' can only be used on sub-queries.
IN picks the list of matching values. EXISTS returns the Boolean values like true or false.
Exists is faster than in.
Different between Primary key and Foreign key?
Primary key Foreign key
It is used to uniquely identify data in the table It is used to maintain relationship between tables
It can’t be null It can accept the null values
7. Two or more rows can’t have same primary key It can carry duplicate value for a foreign key
attribute
Primary has clustered index By default, It is not clustered index
Primary key constraint can be defined on temporary
table
It can’t be defined on temporary tables
Difference between CLOB and BLOB?
Blob Clob
The full form of Blob is Binary Large Object. The full form of Clob is Character Large Object.
This is used to store large binary data. This is used to store large textual data.
This stores values in the form of binary streams. This stores values in the form of character streams.
Using this you can store files like text files, PDF
documents, word documents etc.
Using this you can stores files like videos, images, gifs,
and audio files.
Difference between Delete Truncate and Drop?
DELETE TRUNCATE DROP
Deletes some or all rows of a table Deletes all rows of a table Removesall rows and also the
table definition, including indexes,
triggers, grants, storage
parameters
DML DDL DDL
Commit required Commit not required Commit not required
Uncommitteddeletes can be rolled
back
Cannot be rolled back – once
truncated, gone forever
A droppedtable can be reinstated
from the recycle bin
Filter criteriabe specifiedvia
WHERE clause
No filter criteriaallowed, removes
all rows
No filter criteriaallowed, removes
all rows
DELETE triggers fired No triggers fired No triggers fired
DELETE can be slow especially if
the table has many triggers,
indexes, and other dependencies
TRUNCATEis fast for deleting all
rows, evenmore than dropping
and recreatingthe table using
DROP
DROP may not be as fast as
TRUNCATE, asdropping and re-
creating the table requiresyouto
re-grant object privileges, re-create
indexes, constraints, etc.
DELETE ANY TABLEallows you
to delete rows from any table of
any schema.
DROP ANY TABLEsystem
privilege.
DROP ANY TABLEsystem
privilege.
DELETE privilege on a specific
table can be granted to another
user or role.
TRUNCATEprivilege ona specific
table cannot be granted to another
user or role.
DROP ANY privilege ona specific
table cannot be granted to another
user or role.
Difference between Equi join & Outer join?
Equi Joins Outer Joins
Oracle Equi Joins retrieves the matching column values
of the multiple tables. The join condition or the
comparison operator present in the WHERE clause of the
select statement.
Another type of joins is an outer join which returns a
result of an inner join plus all the rows from one table
for which the join condition is not true.
SELECT column [ , column ] FROM t1 , t2
where t1.column = t2.column;
SELECT column [ , column ] FROM t1
LEFT | RIGHT | FULL [OUTER] JOIN t2
8. ON t1.column = t2.column;
The Oracle Equijoin returns the intersect records
of t1 and t2 as a result.
There are three types of outer join as given below:
Left Outer Joins (also called as Left Joins)
Right Outer Joins (also called as Right Joins)
Full Outer Joins (also called as Full Joins)
Difference between Inner,Left outer, Right outer and Full outer joins?
Inner Join
Or Simple Join
Left Outer Join Right Outer Join Full Outer Join
Inner joins join the
multiple tables and return
those rows for which the
join condition is true. The
inner join is the most
common join among the
types of join.
The Left Outer Join
returns contain all rows
from the LEFT table (
according to the specified
in ON condition) and
from the other table
only those rows where the
joined condition is true.
The RIGHT OUTER
JOIN returns contain all
rows from the RIGHT
table (according to the
specified in ON
condition) and from the
other table only those
rows where the joined
condition is true.
The Full Outer Join
returns contain all rows
from the LEFT table and
RIGHT table with null in
fields where the join
condition is not true.
SELECT column [ ,
column ] FROM t1
INNER JOIN t2
ON t1.column =
t2.column;
SELECT column [ ,
column ] FROM t1
LEFT [OUTER] JOIN t2
ON t1.column =
t2.column;
SELECT column [ ,
column ] FROM t1
RIGHT [OUTER] JOIN
t2
ON t1.column =
t2.column;
SELECT column [ ,
column ] FROM t1
FULL [OUTER] JOIN t2
ON t1.column =
t2.column;
The Oracle INNER JOIN
returns the intersect
records of t1 and t2 as a
result.
The Oracle LEFT
OUTER JOIN returns all
the records from t1 and
the intersect records
of t1 and t2 as a result.
The Oracle RIGHT
OUTER JOIN returns all
the records from t2 and
the intersect records
of t1 and t2 as a result.
The Oracle FULL
OUTER JOIN returns all
the records from t1 and t2
tables as a result.
Difference between Union And Union All
Union Union All
UNION:only keeps unique records UNION ALL:keeps all records,including duplicates
It combines the result set from multiple tables
and returns distinct records into a single result set.
It combines the result set from multiple tables
and returns all records into a single result set.
Following is the basic syntax of UNION operator:
SELECT column_list FROM table1
UNION
SELECT column_list FROM table2;
Following is the basic syntax of UNION ALL
operator:
SELECT column_list FROM table1
UNION ALL
SELECT column_list FROM table2;
It has a default feature to eliminate the duplicate rows
from the output.
It has no feature to eliminate the duplicate rows
from the output.
Its performance is slow because it takes time to find
and then remove duplicate records.
Its performance is fast because it does not
eliminate the duplicate rows.
Most database users prefer to use this operator. Most database users do not prefer to use this
operator.
Difference between Intersect & Minus
Intersect Minus
Intersect Set operator is used to fetch the common
records from 2 different tables .
Minus Operator is used to fetch the records from first
table which eliminates common records.
It returns only the rows returned from both queries It returns unique rows selected by first query but not the
rows selected from second query
9. SELECT city_name FROM CUST_DATA
INTERSECT
SELECT city_name FROM SUPP_DATA;
SELECT city_name FROM CUST_DATA
MINUS
SELECT city_name FROM SUPP_DATA;
For Performance tuning Intersect operator is not
preferable as it takes time to fetch duplicate records
Minus operator is preferable operator in Performance
tuning.
Difference between Count(*) & Count(1)
Count(*) Count(1)
The COUNT(*) function counts the total rows in the
table, including the NULL values.
COUNT(*) and COUNT(1) are identical.
SELECT COUNT(*) AS number_of_rows
FROM orders;
SELECT COUNT(1) AS number_of_rows
FROM orders;
count(*) counts the rowid count(1) counts the 1 as the number of rows.
Difference between Count(*) & Count(column name)
Count(*) Count(column name)
COUNT(*) will count all the rows in the table,
including NULL values.
COUNT(column name) will count all the rows in the
specified column while excluding NULL values.
count(*) counts no of rows produced by query. count(1) the no of 1 values this is because the database
can often count rows by accessing an index.
Difference between PIVOT and UNPIVOT and Reverse PIVOT
PIVOT UNPIVOT Reverse PIVOT
The PIVOT statement is used to
convert table rows into columns
The UNPIVOT operator converts
columns back to rows.
Reversing a PIVOT statement refers
to the process of applying
the UNPIVOT operator to the
already PIVOTED dataset in order
to retrieve the original dataset.
Difference between Primary Key and Unique key
Primary Key Unique key
Used to serve as a unique identifier for each row in a
table.
Uniquely determines a row which isn’t primary
key.
Cannot accept NULL values. Can accept one NULL value.
Only one primary key More than one unique key
Creates clustered index Creates non-clustered index
Difference between clustered and a non-clustered index
Clustered Index Non-Clustered Index
A clustered index is a special type of index that reorders
the way records in the table are physically stored.
Therefore table can have only one clustered index. The
leaf nodes of a clustered index contain the data pages.
A Non-clustered index is a special type of index in
which the logical order of the index does not match the
physical stored order of the rows on disk.
Difference between a view and a synonym
10. Synonym View
Synonym is just a second name of table used for
multiple link of database. synonym can be on view.
View can be created with many tables, and with virtual
columns and with conditions.
Difference between GTT & Views
Global temporary tables GTT Views
Global temporary tables in Oracle are permanent
database objects that store data on disk and visible to all
sessions.
An Oracle VIEW, in essence,is a virtual table that does
not physically exist. Rather, it is created by a query
joining one or more tables.
Difference between B-Tree,Bitmap and Functional Index?
B-Tree Index Bitmap Index Functional Index
B-Tree index is an index that is
created on columns that contain
very unique values
Bitmap is a method of indexing,
offering performance benefits and
storage savings
Function-based indexes allow you to
create an index based on a function or
expression.
B-Tree works best with many
distinct indexed values
Bitmap works best with many
distinct indexed values
CREATE INDEXidx_emp_id
ON employee (id);
CREATE BITMAP INDEX
idx_emp_gender
ON employee(gender);
CREATE INDEXindex_name
ON
table_name(function(column_name));
High Cardinality,Less Duplicates Low Cardinality, More Duplicates
Difference between OLTP & OLAP?
OLTP OLAP
It is an online transactional system and manages
database modification.
It is an online data retrieving and data analysis
system.
Insert, Update,Delete information from the
database.
Extract data for analyzing that helps in decision
making.
OLTP and its transactions are the original source
of data.
Different OLTPs database becomes the source of
data for OLAP.
OLTP has short transactions. OLAP has long transactions.
The processing time of a transaction is
comparatively less in OLTP.
The processing time of a transaction is
comparatively more in OLAP.
Simpler queries. Complex queries.
Tables in OLTP database are normalized (3NF). Tables in OLAP database are not normalized.
OLTP database must maintain data integrity
constraint.
OLAP database does not get frequently modified.
Hence,data integrity is not affected.
Difference between Nested & Co-related sub query?
Nested Query Sub Query Correlated Query Sub Query
In Nested Query, Inner query runs first, and only once.
Outer query is executed with result from Inner
query.Hence,Inner query is used in execution of Outer
query.
In Correlated Query, Outer query executes first and for
every Outer query row Inner query is executed. Hence,
Inner query uses values from Outer query.
Bottom up approach i.e. Inner query runs first, and only
once. Outer query is executed with result from Inner
Top to Down Approach i.e. Outer query executes first
and for every Outer query row Inner query is executed.
11. query.
Inner query execution is not dependent on Outer query. Inner query is dependent on Outer query.
Performs better than Correlated Query but is slower than
Join Operation.
Performs slower than both Nested Query and Join
operations as for every outer query inner query is
executed.
Difference between Union All and Full Outer Join?
Union All Full Outer Join
Union is vertical - rows from table1 followed by rows
from table2 (distinct for union, all for union all) & both
table must have same number of columns with
compatible datatypes.
Full outer join is horizontal. Number of rows in full
outer join is max of table1 and table 2 rows and number
of columns = table1 columns + table2 columns.
It is faster It is slower compared to Union all
Difference between Associate array,Nested table array and V array?
Associate array or Index by table Nested table array V array
Unbounded i.e. the number of
elements it can hold is not pre-
defined
Unbounded i.e. the number of
elements it can hold is not pre-
defined
Bounded i.e. holds a declared
number of elements, though this
number can be changed at runtime
Can be arbitrary numbers or strings.
Need not be sequential.
Sequential numbers, starting from
one
Sequential numbers, starting from
one
Index by tables can be used in
PL/SQL programs only, cannot be
stored in the database.
Can be stored in the database using
equivalent SQL types, and
manipulated through SQL.
Can be stored in the database using
equivalent SQL types, and
manipulated through SQL (but with
less ease than nested tables)
Works as key-value pairs.
e.g. Salaries of employees can be
stored with unique employee
numbers used as subscripts
sal(102) := 2000;
Similar to one-column database
tables.
Oracle stores the nested table data
in no particular order. But when you
retrieve the nested table into a
PL/SQL variable, the rows are given
consecutive subscripts starting at 1.
Standard subscripting syntax e.g.
color(3) is the 3rd color in varray
color
Most flexible. Size can increase/
decrease dynamically.
Elements can be added to any
position in the list and deleted from
any position.
Almost like index-by tables, except
that subscript values are not as
flexible. Deletions are possible from
non-contiguous positions.
Not very flexible. You must retrieve
and update all the elements of the
varray at the same time.
Hash tables Sets and bags Arrays
Difference between Nested table array and V array?
Nested table array V array
Nested tables are unbounded. VARRAYs are always bounded (varying arrays have a
limited number of entries)
Initially dense but can become sparse through deletions. Never sparse.
Order is not preserved When you store and retrieve a VARRAY,its element
order is preserved
Can be indexed Cannot be indexed
Difference between Cursor and Ref Cursor
Cursor Ref Cursor
A cursor is really any SQL statement that runs DML
(select, insert, update, delete) on your database.
A ref cursor is a pointer to a result set. This is
normally used to open a query on the database server,
then leave it up to the client to fetch the result it needs.