You are on page 1of 3

Oracle Interview Questions

The oralce inteview questions are classified into

SQL Interview Questions


PL/SQL Interview Questions

SQL Interview Questions:

1. Write a query to find the highest salary earned by an employee in each


department and also the number of employees who earn the highest salary?

SELECT DEPARTMENT_ID,
MAX(SALARY) HIGHEST_SALARY,
COUNT(1) KEEP(DENSE_RANK LAST ORDER BY SALARY) CNT_HIGH_SAL
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID;

2. Write a query to get the top 2 employees who are earning the highest salary in
each department?

SELECT DEPARTMENT_ID,
EMPLOYEE_ID,
SALARY
FROM
(
SELECT DEPARTMENT_ID,
EMPLOYEE_ID,
SALARY,
ROW_NUMBER() OVER(PARTITION BY DEPARTMENT_ID ORDER BY SALARY DESC ) R
FROM EMPLOYEES
) A
WHERE R <= 2;

3. Write a query to delete the duplicate records from employees table?

DELETE FROM EMPLOYEES


WHERE ROWID NOT IN
(SELECT MAX(ROWID) FROM EMPLOYEES GROUP BY EMPLOYEE_ID);

4. Write a query to find the employees who are earning more than the average salary
in their department?
SELECT EMPLOYEE_ID,
SALARY
FROM EMPLOYEES E_O
WHERE SALARY >
( SELECT AVG(SALARY) FROM EMPLOYEES E_I
WHERE E_I.DEPARTMENT_ID = E_O.DEPARTMENT_ID );

5. How do you display the current date in oracle?

SELECT SYSDATE FROM DUAL;

6. What is a correlated Query?

It is a form of sub query, where the sub query uses the values from the outer query
in its WHERE clause. The sub query runs for each row processed in the outer query.
Question 4 is an example for a correlated sub query.

PL/SQL Interview Questions:

1. What is a cursor?

A cursor is a reference to the system memory when an SQL statement is executed. A


cursor contains the information about the select statement and the rows accessed by
it.

2. What is implicit cursor and explicit cursor?

Implicit Cursors: Implicit cursors are created by default when DML statements like
INSERT, UPDATE and DELETE are executed in PL/SQL objects.
Explicit Cursors: Explicit cursors must be created by you when executing the select
statements.

3. What are the attributes of a cursor?

Cursor attributes are:

%FOUND : Returns true if a DML or SELECT statement affects at least one row.
%NOTFOUND: Returns true if a DML or SELECT statement does not affect at least one
row.
%ROWCOUNT: Returns the number of rows affected by the DML or SELECT statement.
%ISOPEN: Returns true if a cursor is in open state.
%BULK_ROWCOUNT: Similar to %ROWCOUNT, except it is used in bulk operations.
4. What is a private and public procedure?

Public procedure: In a package, the signature of the procedure is specified in the


package specification. This procedure can be called outside of the package.
Private procedure: For private procedure, there won�t be any signature in the
package specification. So, these procedures can be called only inside the package
and cannot be called outside of the package.

5. Create a sample delete trigger on employees table?

CREATE OR REPLACE TRIGGER EMPLOYEES_AD"


AFTER DELETE ON EMPLOYEES
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
INSERT INTO
employees_changes (employee_id,
change_date
)
VALUES (:OLD.photo_tag_id,
SYSDATE
);
END;

6. What is the difference between a procedure and a function?

A function returns a value. However a procedure does not return


a value.

You might also like