You are on page 1of 4

1) HOW TO DELETE DUPLICATE RECORDS FROM THE TABLE ?

When working in Oracle, you may find that some of your records have duplicates. You
can delete these duplicate rows by identifying them and using its RowID, or row
address. Before you begin, you should create a backup table in case you need to
reference them after you have deleted records.

1- Identifying your Duplicate

select column_name, count(column_name)


from table
group by column_name
having count (column_name) > 1;

2-

DELETE
from table_name
WHERE rowed not in
( select max(rowid) from table_name group by column_name);

Or

delete column_name, count(column_name)


from table
group by column_name
having count (column_name) > 1;

or

DELETE
from table_name a
WHERE rowed not in
( select max(rowid) from table_name b where a.column_name=b.column_name);

2) SELECT SECOND MAX FROM THE TABLE ?

Below is simple query to find the employee whose salary is highest.

select *from employee where salary=(select Max(salary) from


employee);

second max
WITH T AS
(
SELECT *
DENSE_RANK() OVER (ORDER BY Salary Desc) AS Rnk
FROM Employees
)
SELECT Name
FROM T
WHERE Rnk=3;

3rd max salary

WITH T AS
(
SELECT *
DENSE_RANK() OVER (ORDER BY Salary Desc) AS Rnk
FROM Employees
)
SELECT Name
FROM T
WHERE Rnk=3;

3) DEPARTMENT WISE SELECT MAX SALARY FROM THE TABLE ?

Use PARTITION BY DEPERTMENT_ID

WITH T AS
(
SELECT *
DENSE_RANK() OVER ( PARTITION BY DEPARTMENT_ID ORDER BY Salary
Desc) AS Rnk
FROM Employees
)
SELECT *
FROM T
WHERE Rnk=1;
OR

SELECT DEPARTMENT_ID , MAX(SALARY) FROM EMPLOYEES


GROUP BY DEPARTMEMT_ID;

SELECT * FROM EMPLOYEES WHERE (DEPARTMENT_ID,SALARY) IN


( SELECT DEPERTMENT_ID,MAX(SALARY))
FROM EMPLOYEES GROUP BY DEPARTMENT_ID;

4) ROWNUM, ROWID , ROW_NUMBER DIFF ?

SRNO ROW_NUMBER
ROW_ID ROW_NUM

ROWID is a ROWNUM is a pseudo ROW_NUMBER is a analytical


1
pseudo column column function.

ROWNUM is the sequential


Represents the ROW_NUMBER assigns a number
number, allocated to each
2 physical address to each row according to its
returned row during query
of rows. ordering within a group of rows.
execution.

Rowid is
3 ROWNUM is temporary ROW_NUMBER is temporary
permanent

ROWID is 18 ROW_NUMBER is a function that


4 ROWNUM is numeric
character string returns numeric value.

ROWID gives the


ROWNUM gives the count of ROW_NUMBER gives the rank of
5 address of rows or
records records.
records.
5) CASE AND DECODE
CASE DECODE

CASE is a Statement in SQL / PL SQL DECODE is a function in Oracle that can be used in SQL only

CASE is ANSI SQL-compliant DECODE is proprietary to Oracle.

CASE helps in building conditional logic DECODE also helps in building conditional logic

CASE can use comparison operators like ( IF > ) Decode cannot use comparison operators (like > ) but the Decode logic is based on Equality

CASE can be used as constructs in PL SQL blocks DECODE can be part of SQL statements only. Though the SQL can be used in PL SQL

CASE can handle predicates (like IN) and searchable subqueries DECODE Cannot handle Predicates and searches.

You can use Parameter as CASE statements to procedure calls Decode cannot handle parameters as procedure calls

CASE treats NULL statement differently


DECODE treats as NULL as NULL

CASE expects datatype consistency, DECODE does not expect datatype consistency

6) SYNONYMS
What are synonyms in Oracle SQL?
A synonym is an alias or friendly name for the database objects (such as tables,
views, stored procedures, functions, and packages).

CREATE SYNONYM synonym_name FOR object_name;

Oracle View
In Oracle, view is a virtual table that does not physically exist. It is stored in Oracle data
dictionary and do not store any data. It can be executed when called.

A view is created by a query joining one or more tables.

1. CREATE VIEW view_name AS


2. SELECT columns
3. FROM tables
4. WHERE conditions;

1. DROP VIEW view_name;

You might also like