Oracle SQL Interview Questions and Answers

What is SQL?

Structured Query Language (SQL) is a language that provides an interface to relational database systems.
The proper pronunciation of SQL is "sequel". SQL was developed by IBM in the 1970.
Today, SQL is accepted as the standard RDBMS language.

Difference between TRUNCATE, DELETE and DROP commands?

DELETE command is used to remove some or all rows from a table. A WHERE clause can be used to only remove some rows. If no WHERE condition is specified, all rows will be removed. After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it. Note that this operation will cause all DELETE triggers on the table to fire.

TRUNCATE removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUNCATE is faster and doesn't use as much undo space as a DELETE.

DROP command removes a table from the database. All the tables' rows, indexes and privileges will also be removed. No DML triggers will be fired. The operation cannot be rolled back.

DROP and TRUNCATE are DDL commands, whereas DELETE is a DML command. Therefore DELETE operations can be rolled back (undone), while DROP and TRUNCATE operations cannot be rolled back.

DELETE will not free up used space within a table. This means that repeated DELETE commands will severely fragment the table and queries will have to navigate this "free space" in order to retrieve rows.

How does one eliminate duplicate rows from a table?

To remove duplicate rows of data, use the following statement: it will remove duplicate rows from a table and leaving only unique records in the table:

Delete from table_name where rowid not in (select max(rowid) from table group by duplicate_values_field_name);

What the difference between UNION and UNION ALL?

Both UNION and UNION ALL is used to combine results of two SELECT queries.
The main difference between them is that, Union will remove the duplicate rows from the result set while Union all doesn't.
The performance of UNION ALL will be better than UNION, since UNION requires the server to do additional work of removing duplicates.

select * from T1
/*union example*/
select * from T1
select * from T1 
​/*union all example*/
select * from T1
union all
select * from T1

How to Change a SQL prompt name?

SQL> set prompt oracle

Find out nth highest salary from emp table?

With the help of correlated sub-query, row_number(), dense_rank() and rank() , we can find nth highest salary -

SELECT * FROM Employee order by 2
Ram    3000
Tom    3000
Vinod    4000
Venky    5000
Manoj    7000

/*correlated sub-query example */
  FROM Employee A
               FROM Employee B
              WHERE a.salary <= b.salary);

N= 2 -- to identify 2nd highest salary

/*row_number example */
FROM Employee e)
WHERE rn = &N;
N= 3 -- to identify 3rd highest salary

/*correlated sub-query example */
  FROM (SELECT  name,
               DENSE_RANK() OVER(ORDER BY salary DESC) nth_highest_sal
          FROM Employee )
 WHERE nth_highest_sal = &n;

N= 3 -- to identify 3rd highest salary

/*rank example */
  FROM (SELECT  name,
                RANK() OVER(ORDER BY salary DESC) nth_highest_sal
          FROM Employee )
 WHERE nth_highest_sal = &n;

N= 3 -- to identify 3rd highest salary

What is the difference between CHAR, VARCHAR and VARCHAR2 data types?

CHAR, VARCHAR and VARCHAR2 are used to store the character string values -

CHAR should be used for storing fixed length character strings. String values will be space/blank padded before stored on disk. CHAR(n) will ALWAYS be n bytes long. If the string length is <n, it will be blank padded upon insert to ensure a length of n.

VARCHAR Currently VARCHAR behaves exactly the same as VARCHAR2. However, this type should not be used as it is reserved for future usage.

VARCHAR2 is used to store variable length character strings. The string value's length will be stored on disk with the value itself. VARCHAR2(n) will be 1 to n bytes long. If the string length is <n, it will not be blank padded.

What is difference between Co-related sub query and nested sub query?

Correlated sub query runs once for each row selected by the outer query. It contains a reference to a value from the row selected by the outer query.

Select e1.empname, e1.basicsal, e1.deptno
from emp e1
where e1.basicsal = (select max (basicsal)
from emp e2
where e2.deptno = e1.deptno)

Nested sub query runs only once for the entire nesting (outer) query. It does not contain any reference to the outer query row.

Select empname, basicsal, deptno
from emp
where (deptno, basicsal) in
(select deptno, max (basicsal)
from emp
group by deptno)

What are the more common pseudo-columns?

A pseudo-column behaves like a table column but it is not actual column of the table. We can select from pseudo-columns, but we cannot insert, update, or delete their values.


What is the difference between group by and order by?
Group by controls the presentation of the rows, Order by controls the presentation of the columns.
What keyword does an SQL SELECT statement use for a string search?
The LIKE keyword used for string searches. The % sign is used as a wildcard and _ for single.
What are various constraints / Integrity Constraints used in SQL?

Data integrity allows defining certain data quality requirements that the data in the database needs to meet.

Five type of constraints :-

Primary key uniquely identifies each record in the table.
Unique Key uniquely identifies each record in a database table. but may be null.
Foreign Key is a field (or fields) that points to the primary key of another table. The purpose of the foreign key is to ensure referential integrity of the data.
Not Null enforces a column to NOT accept NULL values
Check allows stating a minimum requirement for the value in a column.

What is difference between UNIQUE and PRIMARY KEY constraints?

Difference is that Primary Key have an implicit NOT NULL constraint while Unique Keys do not.

A table can have only one Primary Key whereas there can be any number of Unique Keys

When do you use WHERE clause and when do you use HAVING clause?
WHERE clause applies to the individual rows. HAVING clause is used when you want to specify a condition for a group function and it is written after GROUP BY clause.
What are the difference between DDL, DML and DCL commands?

DDL - Data Definition Language: statements used to define the database structure or schema. Some examples: 

CREATE - to create objects in the database 
ALTER - alters the structure of the database 
DROP - delete objects from the database 
TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed 
COMMENT - add comments to the data dictionary 
RENAME - rename an object 

DML - Data Manipulation Language: statements used for managing data within schema objects. Some examples: 

SELECT - retrieve data from the a database 
INSERT - insert data into a table 
UPDATE - updates existing data within a table 
DELETE - deletes all records from a table, the space for the records remain 
MERGE - UPSERT operation (insert or update) 
CALL - call a PL/SQL or Java subprogram 
EXPLAIN PLAN - explain access path to the data 
LOCK TABLE - controls concurrency 
DCL - Data Control Language. Some examples: 
GRANT - gives user's access privileges to database 
REVOKE - withdraw access privileges given with the GRANT command 

TCL - Transaction Control: statements used to manage the changes made by DML statements. It allows statements to be grouped together into logical transactions. 

COMMIT - save work done 
SAVEPOINT - identify a point in a transaction to which you can later roll back 
ROLLBACK - undo the modification I made since the last COMMIT 
SET TRANSACTION - Change transaction options like isolation level and what rollback segment to use 
SET ROLE - set the current active roles 
DML are not auto-commit. i.e. you can roll-back the operations, but DDL are auto-commit 

What are sequences? Explain with syntax?

A field in oracle can be kept as auto incremented by using sequence. it can be used to create a number sequence. 

For example

e_seq will cache up to 20 values for performance. Starts from one.


Latest Updates

Pipes and Cistern

Three taps A,B and C can fill a tank in 12, 15 and 20 hours respectively. If A is open all the time and B and C are open for one hour each alternatively, the tank will be full in?

Two pipes A and B can fill a tank in 6 hours and 4 hours respectively. If they are opened on alternate hours and if pipe A s opened first, in how many hours, the tank shall be full?

A large tanker can be filled by two pipes A and B in 60 min and 40 min respectively. How many minutes will it take to fill the tanker from empty state if B is used for first half of the time, and A and B fill it together for the other half?

A tank is filled by 3 pipes with uniform flow. The first two pipes operating simultaneously fill the tank in the same time during which the tank is filled by the third pipe alone. The 2nd pipe fills the tank 5 hours faster than first pipe and 4 hours slower than third pipe. The time required by first pipe is?

Two pipes A and B together can fill a cistern in 4 hours. Had they been opened separately, then B would have taken 6 hours more than A to fill the cistern. How much time will be taken by A to fill the cistern separately?

Two pipes A and B can fill a tank in 24 min and 32 min respectively. If both the pipes are opened simultaneously, after how much time B should be closed so that the tank is full in 18 min?

Two pipes A and B can fill a tank in 36 min and 45 min respectively. A water pipe C can empty the tank in 30 min. First A and B are opened. after 7 min, C is also opened. In how much time, the tank is full?

Two pipes can fill a cistern in 14 hours and 16 hours respectively. The pipes are opened simultaneously and it is found that due to leakage in the bottom it took 32 min more to fill the cistern. When the cistern is full, in what time will the leak empty it?

Bucket P has thrice the capacity as Bucket Q. It takes 60 turns for Bucket P to fill the empty drum. How many turns it will take for both the buckets P and Q, having each turn together to fill the empty drum?


In a class , 30 % of the students offered English, 20 % offered Hindi and 10 % offered Both.If a student is offered at random, what is the probability that he has offered English or Hindi?

In a lottery ,there are 10 prizes and 25 blanks.A lottery is drawn at random. what is the probability of getting a prize ?

Two dice are thrown simultaneously .what is the probability of getting two numbers whose product is even?

Two diced are tossed the probability that the total score is a prime number?

Two cards are drawn at random from a pack of 52 cards What is the probability that either both are black or both are queens?

Two dice are thrown together .What is the probability that the sum of the number on the two faces is divisible by 4 or 6?

A bag contains 6 white and 4 black balls .Two balls are drawn at random .Find the probability that they are of the same colour?

In a simultaneous throw of a pair of dice,find the probability of getting a total more than 7?

An unbiased die is tossed.Find the probability of getting a multiple of 3?

Latest Job Notifications

Banking Jobs

बैंकिंग कार्मिक चयन संस्थान Institute of Banking Personnel Selection(IBPS) - 7855 पद - 29/07/2022 अंतिम तिथि

SSC Recruitment

कर्मचारी चयन आयोग (एसएससी) Staff Selection Commission (SSC) - 1411 पद - 29/07/2022 अंतिम तिथि

Jobs in Rajasthan

राजस्थान अधीनस्थ और मंत्रिस्तरीय सेवा चयन बोर्ड Rajasthan Subordinate and Ministerial Services Selection Board (RSMSSB) - 5546 पद - 22-Jul-2022 अंतिम तिथि

Railway Recruitment

उत्तर मध्य रेलवे भर्ती प्रकोष्ठ Railway Recruitment Cell(NCR) - 1659 पद - 01/08/2022 अंतिम तिथि

Jobs in West Bengal

इंडियन पोस्ट सर्विस Indian Post Service - 2357 पद - 19/08/2021 अंतिम तिथि - पश्चिम बंगाल पोस्टल सर्कल

Banking Jobs

भारतीय स्टेट बैंक State Bank of India(SBI) - 6100 पद - 26/07/2021 अंतिम तिथि - CRPD/APPR/2021-22/10

SSC Recruitment

कर्मचारी चयन आयोग Staff Selection Commission(SSC) - 25271 पद - 31/08/2021 अंतिम तिथि

Jobs in Madhya Pradesh

राष्ट्रीय स्वास्थ्य मिशन मध्य प्रदेश National Health Mission MP(NRHM) - 5215 पद - 22/06/2021 अंतिम तिथि

Jobs in Punjab

पंजाब स्टेट पावर कॉर्पोरेशन लिमिटेड Punjab State Power Corporation Limited (PSPCL) - 2632 पद - 20/06/2021 अंतिम तिथि

Jobs in Chhattisgarh

स्वास्थ्य सेवा निदेशालय, छत्तीसगढ़ Department of Health & Family Welfare, Chhattisgarh - 267 पद - 26/06/2021 अंतिम तिथि