You are on page 1of 7

3rd highest salary of employee

SQL> select *
2 from
3 (
4 select ename
5 ,sal
6 ,rank() over (order by sal desc) ranking
7 from emp
8 )
9 where ranking = 4 -- Replace 4 with any value of N

HOW TO DELETE DUPLICATES IN ORACLE?

DELETE FROM
table_name A
WHERE
a.rowid >
ANY (
SELECT
B.rowid
FROM
table_name B
WHERE
A.col1 = B.col1
AND
A.col2 = B.col2
);

update Table1 set "col" = (case "col" when 'male' then 'female'
else 'male' end);

declare
cursor cur_gender is
select * from gender
for update;
begin
for v_gen in cur_gender
loop
update gender
set sex = case v_gen.sex when 'male' then 'female' else 'male' end
where current of cur_gender;
end loop;
end;
/

1.WHAT IS INDEX AND WHAT ARE THE TYPES OF INDEX?

Indexes of sql server are similar to the indexes in books, When there are thousands
of records in a table, retrieving information will take a long time, therefore indexes
are created on columns which are accessed frequently, so that the data can be
retrieved quickly.

An Index is a physical structure containing pointers to the data.

These indexes can be created on a single column or combination of columns, when


an Index is created it first sorts the data and it assigns a ROW ID for each row.

The main types of indexes are:


1.B-TREE INDEXES-
These indexes created on unique data, this is the default index

2.BIT MAP INDEXES-these are created on duplicate data of the column.

3.FUNCTION BASED INDEXES


These indexes created on the functions on the column, cause if we use oracle
functions on the indexed columns, the indexes wont work.

2.WHAT IS VIEW?

View isasql statement which is stored in the database with an alias name.

Whenever we run view, sql executes in back end. Viewsare used to restrict the data
access of table.

A View is like a virtual table ,it does not physically exist, rather it created by a query
joining one or more tables.

A view can be created from one or more tables, which depends on the written sql
query to create view.

3.WHAT IS MATERIALISED VIEW?


Materialized view stores the result set of sql statements.
How does thesematerialized views work?
When we run materialized views for the first time it hits the base tables first and
stores the output into the log tables. From the next run onwards it hits the log tables
instead of base tables.
We use the theseMaterialised views in scenarios where we have complex aggregate
queries.

4.WHAT IS INLINE VIEW?


Basically inline view is a query inside a query, which is known as sub query but with
a twist.
An inline view is a SELECT statement in the FROM cause of another SELECT
statement.
These are commonly used to simplify complex queries by removing Join operations
and integrating several SQL statements in a single query/
Ex: SELECT A.lastname, A.salary,A.department_id,B.maxsal FROM
employees A,
(SELECT department_id,MAX(salary),maxsal FROM
employees
GROUP BY department_id ) ,B
WHERE A.department_id=B.department_id AND
A.salary=b.maxsal ;

5.WHATS THE DIFFERNCES BETWEEN SUB QUERY AND CORELATED


QUERY?
A query inside the query is called SUBQUERY, when we run sub query, the inner
query executed first and runs only once and the output is used as data source for the
outer query.
whereas ,
In the CORELATED SUBQUERY the outer query will executed first and for every
row of outer query ,inner query will get executed. So the inner query will executed as
many times as no of rows in the result of the outer query.
The outer query output can use the inner query output for comparison.
this means inner query and outer query depend on each other.

Ex: SELECT cust_name,dept_id FROM customer WHERE cust_name IN (SELECT


cust_name FROM dept
WHERE cust_id = dept.dept_id) ;

6.WHAT IS THE DIFFERENCE BETWEEN ROWID AND ROW NUMBER?

ROWID ROW NUMBER

It is the physical address of the rows. Row number is the sequential number
allocated to each returned row during
Row id is automatically generated query execution.
unique id of a row and it is generated
at the time of insertion of row Row number is a dynamic value
automatically retrieved along with
select statement output.

7.WHAT IS CURSOR AND WHAT ARE THE TYPES OF CURSORS?


In order to process the sql statement oracle will allocate an area of memory is know
as context area.
A Cursor is a handle or pointer to the context area.

TYPES OF CURSORS
1.Implicit cursors 2.explicit cursors

Implicit cursors are the default cursors of the procedures.


The oracle server uses the implicit cursors to parse and execute our sql statements.
Where as the explicit cursors, the programmer explicitly declares in procedures and
manually calls them.
STEPS TO DEFINE THE CURSOR:
The 4 pl/sql steps necessary to for explicit cursor processing are as follows

1.Declare the cursor


2.Open the cursor for query
3.fetch the result into pl/sql variables.
4.close the cursor.

8.WHAT IS THE DIFFERENCE BETWEEN STORED PROCEDURE AND


FUNCTIONS?

Stored procedure Function

1.Stored procedure may or may not 1.functions should returns the values.
returns values.
2.function can be called from
2. These cannot be used in a select select/where/having clause
/where/having clauses.
3.these can be executed using
3.stored procedure can be run EXECUTE or EXEC command.
independently. (These are part of sql commands)

4.These can call user-defined 4.function cannot call the stored


functions. procedures.

5.these can have Input and output 5.functions have only i/p parameters.
parameters.

9.WHAT ARE THE DIFFERENCE BETWEEN DELETE AND TRUNCATE?


DELETE
DML Command
This is DDL command
DELETE command can delete all the
rows if no condition is specified (or) it Truncate command deletes all the
can deletes only the rows from the rows from the table
table, based on condition given in
WHERE clause. We cannot rollback once we truncate
the table.
We can Roll back after delete.
This command cannot work on the
It can work on a table that is part of table that is part of the cluster. We
cluster. have to truncate whole cluster or use
either DROP command.
It can work outside the users schema
(as long as user has delete privilege Table can be truncated in ones own
on the object). schema only.

It does not free the space containing It will free the space containing the
the table. table.

TRUNCATE

10.HOW TO FIND DUPLICATES IN ORACLE?

SELECT column 1,column 2,COUNT(*) FROM table


GROUP BY table HAVING COUNT (*)>1 ;

11.HOW TO DELETE DUPLICATES IN ORACLE?

DELETE FROM
table_name A
WHERE
a.rowid >
ANY (
SELECT
B.rowid
FROM
table_name B
WHERE
A.col1 = B.col1
AND
A.col2 = B.col2
);

12.QUERY TO FIND AN EMPLOYEES CORRESPONDING MANAGERS?

Using a sub query

SQL> SELECT ename, emp no,


(SELECT ename FROM emp WHERE emp no = mgr) as manager,mgr ;

(OR)
Using self join

SELECT e.ename,e.empno,m.ename as manager,e.mgr


FROM empe,emp.m
WHERE e.mgr=M.emp no

13.WHAT IS SURROGATE KEY?


A Surrogate key by definition means an artificial key, which is running integer whose
sole purpose is to identify a dimension row uniquely.

Surrogate is a substitution for the natural primary key in the dimension tables.
Surrogate key is a numeric number that uniquely identifies records in the dimension
table.
It is also know as artificial or identity key in data warehouse.
It is useful because the natural primary key can change and this makes updates
more difficult.

14.WHAT IS THE DIFFERENCE BETWEEN PRIMARY KEY AND UNIQUE KEY?


Primary key:
It is an attribute in table, which uniquely identifies a record (row).
Unique key:
it is a key which stores unique values(no duplicates)for the particular column.

1.Unique key allows null values BUT Primary key does not allow null values.
2.We can have multiple unique keys in a table but primary key is one and only one.
3.Primary key in itself is a unique key.
4.Primary can be referenced to other table as foreign key.

15.CAN WE IMPLEMENT DML COMMANDS IN VIEW?


DML commands can be possible if view has only one base table, if the view is based
on columns from one or more table then its not possible.

But with some restrictions, it can possible to insert/update/delete from a base table
using view.

1.If a view is defined by a query that contains SET or DISTINCT operators, a


GROUP BY clause, or a group function, then rows cannot be inserted into, updated
in, or deleted from the base tables using the view.

2 For views with “WITH CHECK” options enabled, its important to keep in mind that
the data begin inserted qualifies in the WHERE clause of the view.

3.if a NOT NULL column that does not have DEFAULT clause is omitted from the
view, then a row can be inserted.

4.if the view was created by using an expression ,such as DECODE ,then rows can
not be inserted into or updated in the base table using the view.
16.QUERY TO FIND THE TOP N th SALARIES OF EMLOYEES?
SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP n salary
FROM employee
ORDER BY salary DESC ) a
ORDER BY salary

17.WHAT ARE THE DIFFERENT TYPES OF JOINS IN ORACLE?

The purpose of the join is to combine the data across tables.


A join is actually performed by the WHERE clause, which combines the specified
rows of tables.
If a join involves in more than two tables then oracle joins first two tables based on
the joins condition and then compares the result with next table and so on.

TYPES:
1 Equi join
2 Non-equi join
3 Self join
4 Natural join
5 cross join
6 outer join
Left outer
Right outer
Full outer
7 Inner join
8 Using clause
9 On clause

You might also like