You are on page 1of 12

SQL Important Difference

1. Difference between char, varchar and varchar2 data types.


S.N. Char Varchar Varchar2
CHAR should be used for storing fix VARCHAR should be used for storing variable VARCHAR2 should be used for storing variable
1
length character strings. character strings including NULL. character strings excluding NULL.
Varchar2 will not occupy any space/memory for
2 Not specific for NULL. Varchar will occupy space for NULL values also.
NULL values.
Ex - CHAR(5) is fixed length, right padded Ex - VARCHAR(5) is fixed length, right padded
3 Ex - VARCHAR2(5) is variable length.
with spaces. with null.
The data type char can store 1- 2000 The data type varchar can store 1- 2000 The data type varchar2 can store 1- 4000
4
bytes/characters (i.e. 2 KB). bytes/characters (i.e. 2 KB). bytes/characters (i.e. 4 KB).
VARCHAR is reserved by Oracle to support
VARCHAR2 does not distinguish between a
5 Not reserved for NULL. distinction between NULL and empty string in
NULL and empty string.
future.
This is faster since all the strings are
This is slower since variable/dynamic logic is This is slower since variable/dynamic logic is
6 stored at a specified position from each
present here. present here.
other.
7 No specific Varchar is of ANSI SQL standard. Varchar2 is of Oracle standard.

2. Difference between DBMS and File System.


S.N. DBMS File System
DBMS is a collection of data. In DBMS, the user is not required to File system is a collection of data. In this system, the user has to write the
1
write the procedures. procedures for managing the database.
2 DBMS gives an abstract view of data that hides the details. File system provides the detail of data representation and storage of data.

DBMS provides a crash recovery mechanism, i.e., DBMS protects the File system doesn't have a crash mechanism, i.e., if the system crashes while
3
user from the system failure. entering some data, then the content of the file will lost.

4 DBMS provides a good protection mechanism. It is very difficult to protect a file under the file system.
DBMS contains a wide variety of sophisticated techniques to store
5 File system can't efficiently store and retrieve the data.
and retrieve the data.
In the File system, concurrent access has many problems like redirecting
DBMS takes care of Concurrent access of data using some form of
6 the file while other deleting some information or updating some
locking.
information.
7 Redundancy is control in DBMS Redundancy not control in file system
8 Unauthorized access is restricted in DBMS Not in the file system
9 DBMS provide back up and recovery Data lost in file system can't be recovered.
10 DBMS provide multiple user interfaces Data is isolated in file system.
A database management system coordinates both the physical and
11 file-processing system coordinates only the physical access.
the logical access to the data
A database management system is designed to allow flexible access A file-processing system is designed to allow predetermined access to data
12
to data (i.e. queries) (i.e. compiled programs).

A database management system is designed to coordinate multiple A file-processing system is usually designed to allow one or more
13
users accessing the same data at the same time. programs to access different data files at the same time.

3. What are the differences between DDL and DML.


S.N. Key DDL DML
1 Full Form DDL stands for Data Definition Language. DML stands for Data Manipulation Language.
DDL statements are used to create Database, Schema,
2 Usage DML statement is used to Insert, Update or Delete the records.
Constraints, Users, Tables, etc.
3 Alter Here Structure modification can be done. Here Data modification can be done.
4 Classification DDL has no further classification. DML is further classified into Procedural and Non-Procedural DML.
5 Commands CREATE, DROP, RENAME, TRUNCATE, ALTER INSERT, UPDATE, DELETE, MERGE
6 TCL Uses COMMIT & ROLLBACK not used in DDL. COMMIT & ROLLBACK used in DDL.
Auto All DML Statements are Temporary and need to do Commit to
7 All DDL Statements are Permanent and Auto Commit.
Commit make permanent.
8 Performance DDL is Faster. DDL is Slower.
9 Impact DDL Statements affect the whole Table. DML Statements affect one or more Rows.
10 Nature DDL is Declarative. DML is Imperative.
4. What are the differences between Alias and Synonym.
S.N. Alias Synonym
A synonym is an alternative name for objects such as tables, views,
1 Alias is just another/nick name of a table/column at run time.
sequences, stored procedures and other database objects.
2 It creates as temporary while executing query. It creates as permanently.
Syntax/Ex : SELECT * from EMP e Syntax/Ex : CREATE OR REPLACE SYNONYM DEPT FOR DEPARTMENT; Here
3
where e.empno=7788; Here e is the Alias of EMP. DEPT is the synonym of DEPARTMENT.
4 Basically it is used in big quries for easy understand and readability. Basically it is used while simultaneous operation for same table.
There are two types of synonyms : Private and Public.
5 No different types of Alias.
a) Private Synonym : Only its owner can access a private synonym.
b) public Synonym : Any database user can access a public synonym.
6 An alias is a logical pointer to an alternate table/column name. A Synonum is a physical pointer to an alternate table name.
7 Alias can not be dropped because it is logical representation. Synonym can be dropped because it is physically present.
Below views are used for Synonym:
8 No views are present for Alias.
a) DBA_SYNONYMS describes all synonyms in the database.
b) USER_SYNONYMS describes the synonyms owned by the current user.

5. Differences between ROWID and ROWNUM.


S.N. Rowid Rownum
Logical value of the rows allocated to each returned row during query
1 Physical address/value of the rows while inserting the records.
execution.
2 Rowid is permanent. Rownum is temporary.
3 Rowid is 16-bit hexadecimal. Rownum is numeric.
4 Rowid gives address of rows or records. Rownum gives count of records.
Rowid is automatically generated unique id of a row and it is Rownum is an dynamic value automatically retrieved along with select
5
generated at the time of insertion of row. statement output.
6 ROWID is the fastest means of accessing data. It is slower as compared to Rowid.
7 They are unique identifiers for the any row in a table. It represents the sequential order in which Oracle has retrieved the row.
6. Differenciate between Synonym and View.
S.N. Synonym View
1 Synonym is a alternate physical table of base table. View is a virtual and Logical table of base table.
2 It can be created for table, view, sequence or program unit. It can be created on a table or another view.
3 It is stored as a table in data dictionary. It is stored as a query in data dictionary.
4 Set of rows and columns can't be restricted. Set of rows and columns can be restricted.
It does not contain data of its own and always takes data from its base
5 It stores data of its own and doesn't take data from its base table.
table.
6 It takes memory. It doesn't take memory.
7 Public and Private access can be given. Public and Private access can't be given.
Synonym can be created using single table, view, sequence or
8 View can be created using multiple tables.
index.

7. Differenciate between Non-Materialised View and Materialised View.


S.N. Non-Materialised View Materialised View
View is just a named query. It is also Called as Non-Materialised
1 Materialised View stores data physically and get updated periodically.
View. It doesn't store anything.
2 The query pulls data from the underlying table. While querying MV, it gives data from MV only.
3 A table on a disk that contains the result set of a query. MV itself keep the data.
4 The Performance or Speed is less compared to MV. The Performance or Speed is more compared to View.
Views are generally used when data is to be accessed infrequently Materialized Views are used when data is to be accessed frequently and
5
and data in table get updated on frequent basis. data in table not get updated on frequent basis.

8. Differences between Drop, Truncate and Delete.


S.N. Drop Truncate Delete
1 Drop is a DDL Statement. Truncate is a DDL Statement. Delete is a DML Statement.
Syntax : TRUNCATE <Object Type> <Object
2 Syntax : DROP <Object Type> <Object Name> Syntax : Delete FROM <Object Name>
Name>
3 Object Type is used in Syntax. Object Type is used in Syntax. From Keyword is used in Syntax.
Truncate remove the Data only as
Drop permanently removes both the Data as Delete remove the Data only as temporary, the
4 permanent, the Table structure remains
well as the Table Structure. Table structure remains same.
same.
5 Only Object and Column deletion is possible. Object and Column deletion is not possible. Object and Column deletion is not possible.
6 Data/Record deletion is not possible. Data/Record deletion is possible. Data/Record deletion is possible.
7 Rollback/Redo not possible. Rollback/Redo not possible. Rollback/Redo possible.
It issues a COMMIT before it acts and another It issues a COMMIT before it acts and Commit can be performed after DML
8 COMMIT afterward so no rollback of the another COMMIT afterward so no rollback operation to make the transaction as
transaction is possible. of the transaction is possible. permanent.
It is Auto Commit (Because all DDL Statements It is Auto Commit (Because all DDL It is not Auto Commit (Because all DML
9
are Auto Commit). Statements are Auto Commit). Statements are not Auto Commit).
10 No row-level locks are taken. No row-level locks are taken. They take locks on rows.
Drop statement can’t remove specific rows Truncate statement can’t remove specific Delete statement can remove the specific rows
11
filtered by where clause. rows filtered by where clause. filtered by where clause.
12 Same Object can't use after Drop. Same Object can re-use after Truncate. Same Object can re-use after Delete.
Index is not used and hence operation is very
13 Index is used and hence operation is faster. Index is used and hence operation is faster.
slower.
They do not require segments in the UNDO They do not require segments in the UNDO They require segments in the UNDO
14
tablespace. tablespace. tablespace.
Memory are not de-allocated from the table to
15 All memory are de-allocated from the table. All memory are de-allocated from the table.
retains all of its original blocks if required.
16 Here Log file is not generated. Here Log file is not generated. Here Log file is generated.
17 It does not activate the Triggers. It does not activate the Triggers. It can activate the Triggers.
Operation can be done only for Row level with
Operation can be done only for Row level
18 Operation can be done in Table & Column level. All data.
with All data.

9. Differences between Stored Procedure and Function in Oracle.


S.N. Stored Procedure Function
Stored Procedure return value is optional; i.e. It can return zero or n
1 Function must return a value.
values (Max 1024).
A stored procedure is a set of Structured Query Language (SQL)
statements with an assigned name, which are stored in a relational A Function is a sub-program that returns a single value. You must
2
database management system as a group, so it can be reused and declare and define a function before invoking it.
shared by multiple programs.
3 Procedures cannot be called from Function. Functions can be called from Procedure.
4 Procedure cannot call in select statement. Function can call in select statement.
5 Procedure can perform one or more tasks. Function performs a specific task.
A Function must be part of an executable statement, as it cannot be
6 Procedure represents an independent executable statement.
executed independently.
7 Procedure are normally used for executing business logic. Function are normally used for computation.
8 Stored procedure supports deferred name resolution. Function won’t support.
9 Stored Procedure returns always integer value by default zero. Function returns type could be scalar or table or table value.
10 Stored procedure is pre-compiled execution plan. Function are not.
11 Procedures can have input/output parameters. Functions can have only input parameters for it.
Ex : CREATE OR REPLACE PROCEDURE my_proc Ex : CREATE OR REPLACE FUNCTION my_func
12
(p_name IN VARCHAR2 := 'John') as begin ... end (p_name IN VARCHAR2 := 'John') return varchar2 as begin ... end

10. Differenciate between UNION and UNION ALL.


S.N. Union Union ALL
Union Set operator is used to fetch the records from two or more Union all Set operator is used to fetch the records from two or more
1
different tables which eliminates the duplicate records. different tables which does not eliminates the duplicate records.
Syntax Syntax
Select col1,col2…from table1 Select col1,col2…from table1
2
Union Union ALL
Select col1,col2…from table2; Select col1,col2…from table2;
3 Output is sorted by default. Output is not sorted by default.
For Performance tuning Union operator is not preferable as it takes
4 Union all is preferable operator in Performance tuning.
time to eliminate duplicate records.
The union operator is slower than union all because it removes Union all is faster than union because it does not have sorting program
5
duplicates from the data. running inside.

11. Differenciate between Subquery and Join.


S.N. Subquery Join
1 Subqueries is used to return either a scalar (single) value or a row set. Joins are used to return rows.
2 A subquery is used to run a separate query within the main query. Join can't be used to run a separate query from the whole query.
3 It is used in FROM, WHERE or HAVING clause. It is used in the FROM and WHERE clause.
4 While dealing with less tables, SUBQUERY is good. While dealing with more tables, JOIN is good.
Sub-query is used where you have a very simple scenario and query is
5 JOINs are used where you have a complex and large queries.
not too lengthy.
When data is needed for the NOT EXISTS and EXISTS conditions, you
6 The conditions NOT EXISTS and EXISTS are not useful for JOIN.
can use sub-query.
Subquery scan the complete table to apply. So SUB-QUERY is slower
7 JOIN scan the partial table to apply. So JOIN is faster than SUB-QUERY.
than JOIN.

12. Differenciate between Join and Union.


S.N. Join Union
A join is used for displaying columns with the same or different names The UNION set operator is used for combining data from different
1
from different tables. tables which have columns with the same datatype.
Unions combine data into new rows. If two tables are “unioned”
Joins combine data into new columns. If two tables are joined together,
together, then the data from the first table is in one set of rows, and
2 then the data from the first table is shown in one set of column
the data from the second table in another set. The rows are in the same
alongside the second table’s column in the same row.
result.
Only the required columns can be mentioned from both table in SELECT
3 The number of columns must be the same for both SELECT statements.
statements.
The columns sequence and data type from both the queries should be
4 Here this concept is not required.
same.
5 One SELECT statement used for multiple tables. Multiple SELECT statement used for multiple tables.
JOIN is applicable when the two involved relations have at least one UNION is applicable when the number of columns present in query are
6
common attribute. same and the corresponding attributes has the same domain.

13. Differenciate between Where and Having.


S.N. Where Having
WHERE clause is used while fetching data (rows) from table before HAVING clause is used to filter summarized data from table after
1
grouping the data. grouping the data.
2 Where Clause is used before group by Clause. Having Clause is used after group by Clause.
3 Aggregate functions can't use in Where Clause. Aggregate functions only use in Having Clause.
4 Implemented in Row operations. Implemented in Column operations.
5 It applied to Single row. It applied to Summarized row or groups.
6 Used with SELECT and other statements such as UPDATE, DELETE, etc. Can't be used without a SELECT statement.
7 Act as Pre-filter. Act as Post-filter.
14. Differenciate between Case and Decode.
S.N. Case Decode
1 CASE is a expression. DECODE is a function.
2 CASE is very clear. Decode is somewhat obscure.
3 CASE is capable of other logical comparisons such as < > etc. DECODE performs an equality check only.
4 CASE can work with predicates and subqueries in searchable form. DECODE works with expressions that are scalar values only.
5 CASE can be an efficient substitute for IF-THEN-ELSE in PL/SQL. DECODE can work as a function inside SQL only.
6 CASE expects datatype consistency. DECODE does not expect datatype consistency.
7 CASE is ANSI standard. DECODE is Oracle standard.
8 CASE query is larger than DECODE. DECODE query is shorter than CASE.
CASE query is faster than DECODE since DECODE is a function which
9 DECODE query is slower than CASE.
takes time to load.
10 In Case we can use range (e.g. 2000 to 4000) also. In decode we can't use for range (e.g. 2000 to 4000).
CASE can be used any where even as a parametr of a
11 DECODE can be used only inside SQL statement.
function/procedure
12 CASE considers two nulls to be different. DECODE considers two nulls to be equivalent.

15. Differenciate between GROUP BY and PARTITION BY.


S.N. group by partition by
GROUP BY statement is used in conjunction with the aggregate PARTITION BY statement is used in conjunction with the aggregate
1 functions to group the result-set by one or more columns with single functions to group the result-set by one or more columns with multiple
row per group. rows per group.
syntax
syntax
SELECT expression1, expression2, ... expression_n,
SELECT expression1, expression2, ... expression_n,
aggregate_function (aggregate_expression)
aggregate_function (aggregate_expression)
2 FROM tables
FROM tables
WHERE conditions
WHERE conditions
aggregate_function ( expression ) OVER (
GROUP BY expression1, expression2, ... expression_n;
PARTITION BY expression1, expression2, … order_clause)
PARTITION BY clause can be mentioned multiple times in a single
3 GROUP BY clause can be mentioned one time in a single query.
query.
Reduces the no. of records. Number of Rows displayed in number of No. of records will not be reduced.. All Rows if Table displayed along
4
groups formed. with group result.
In select we need to use only columns which are used in group by
5 In select we can use N no. of columns without any restrictions.
clause.
6 We can use aggregate functions in SELECT Clause. We can use aggregate functions in PARTITIOn BY Clause only.

We can use where clause in filter condition apart from partition


7 In filter condition we need to use having clause instead of where clause.
column.

8 OVER clause is not used here. We can use the SQL PARTITION BY clause with the OVER clause to
specify the column on which we need to perform aggregation.

16. Differenciate between Non-Correlated Subquery and Correlated Subquery.


S.N. Non-Correlated Subquery Correlated Subquery
1 The inner query will get executed first. The outer query will get executed first.
The inner query is executed as many times based on number of rows in
2 The inner query is executed only once.
the result of the outer query.
3 The output of the inner query used by the outer query. The outer query output can use the inner query output for comparison.
The inner query and outer query dependent on each other to generate
4 The inner query is not dependent on outer query.
the final result set.
5 Non-Correlated subqueries are faster than Correlated Subqueries. Correlated subqueries are slower the Non-Correlated Subqueries.
Example
Example SELECT cust_name,dept_id
SELECT cust_name, dept_no FROM Cust
6 FROM Customer WHERE cust_name in
WHERE cust_name IN (SELECT cust_name
(SELECT cust_name FROM Customer); FROM dept
WHERE cust.dept_id=dept.dept_id);

17. Differenciate between NVL and COALESCE.


S.N. NVL COALESCE
1 NVL accepts only 2 and NVL2 accepts only 3 arguments. COALESCE can take multiple arguments.
2 NVL evaluates both the arguments. COALESCE stops at first occurrence of a non-Null value.
NVL does a implicit datatype conversion based on the first argument
3 COALESCE expects all arguments to be of same datatype.
given to it.
4 NVL is Oracle specific. COALESCE is ANSI standard.
Syntax
Syntax
5 a) NVL (expr1, expr2)
COALESCE (expr_1, expr_2, ... expr_n)
b) NVL2 (expr1, expr2, expr3)
Example
Example a) SELECT salary, NVL(commission_pct, 0),
a) SELECT COALESCE('A','B','C','D')RESULT FROM DUAL; (salary*12) + (salary*12*NVL(commission_pct, 0))
6 b) SELECT last_name, annual_salary FROM employees;
COALESCE(commission_pct, salary, 10) comm b) SELECT last_name, salary, commission_pct,
FROM employees ORDER BY commission_pct; NVL2(commission_pct, 'SAL+COMM', 'SAL')
income FROM employees;

18. Differenciate between Primary Key and Surrogate Key.


S.N. Primary Key Surrogate Key
A surrogate key is any column or set of columns that can be declared
1 A primary key is a special constraint on a column or set of columns.
as the primary key instead of a "real" or natural key.
2 A table may have only one Primary key. A table may have more than one Surrogate key.
3 Primary keys are used in OLTP. Surrogate keys are used in OLAP.
4 It is used to maintain Unique records in OLTP Database. It is used to maintain Unique records in OLAP Database.
5 Primary keys hold some business meaning. Surrogate does not hold any business meaning.
6 Primary key may contain numeric as well as non-numeric values. Surrogate keys contain only numeric values.
7 Primary Key values belongs to Table or Business data. Primary Key values not belongs to Table or Business data.
8 It generated/entered by User/Application. It generated by the System/Backend.
9 It is a visible key. It is invisible key for the user or the application.
10 It resemble table row. It resembles database entity.

19. Differenciate between Primary Key and Unique Key.


S.N. Comparison Primary Key Unique Key
There can be only one Primary key defined for one or
There can be only one or more Unique key defined for one or
1 Number multiple column(s) at a time in a table. In case of multiple
multiple column(s) at a time or individually in a table.
columns, it is called as Composite key.
2 Null Value Primary key can not be NULL. A Unique key can have one or more null values.
Unique key can not be a unique identifier of a record because it
Unique Primary Key is a unique identifier of a record in database
3 can contain null and hence we may not be able to identify a
Identifier table.
record in a unique way.
4 Changes It is not recommended to change a Primary key. A Unique key can be changed much easily.
A Unique key is used to prevent/restrict the duplicate and null
5 Usage Primary Key is used to identify a particular row in a table.
values in a column.
6 Index Creates Clustered Index. Creates Non-Clustered Index.
Primary Key is represented using "Primary Key" Constraint Unique Key is represented using "Unique" Constraint while
7 Constraint
while creation. creation.

CREATE TABLE Employee CREATE TABLE Employee


( (
ID int PRIMARY KEY, ID int UNIQUE,
8 Example
Name varchar(255), Name varchar(255) NOT NULL,
City varchar(150) City varchar(150)
); );

20. Differenciate between Primary Key and Foreign Key.


S.N. Comparison Primary Key Foreign Key
A primary key is a column or group of columns in a relational A foreign key is a column or group of columns in a relational
1 Definition database table which is used to ensure data in the specific database table that provides a link between data in multiple
column is unique and non-null. tables.

It refers the field in a table which is the primary key of another


2 Identification It uniquely identifies a record in the relational database table.
table.
3 Range Only one primary key is allowed in a table. More than one foreign key are allowed in a table.
4 Constraint It is a combination of UNIQUE and Not Null Constraints. It is having no combination of any Constraints.
5 Duplicate It can't contain Duplicate values. It can contain duplicate values.
6 Null It does not allow NULL values. It can also contain NULL values.
7 Parent-Child Its value cannot be deleted from the parent table. Its value can be deleted from the child table.
Temporary Constraint cannot be defined on the local or global temporary
8 Constraint can be implicitly defined on the temporary tables.
Table tables.
The primary key is a clustered index, and data in the DBMS
A foreign key cannot automatically create an index, clustered, or
9 Index table are physically organized in the sequence of the
non-clustered.
clustered index.

Insertion There is no limitation in inserting the values into the table While inserting any value in the foreign key table, ensure that
10
Limitation column. the value is present into a column of a primary key.

Before you delete a primary key value, make sure that value is You can delete a value from foreign key column without
Deletion
11 not still present in the referencing foreign key column of bothering, whether that value is present in referenced primary
Limitation
referencing table. key column of referenced relation.

***************************

You might also like