Sunteți pe pagina 1din 7

Oracle SQL and PL/SQL TEST

(Time: 60 minutes)
I Oracle Questions

1. What is the DUAL table? How can we determine the date of the system using the DUAL
table?
The Dual table is special one row and one column table. The table has a single
varchar2(1) column
called Dummy.
SELECT sysdate FROM DUAL ;

2. What is a JOIN and how many join types exist?


A join is a query that combines rows from two or more tables, views, or materialized views.
There are 4 different types of Oracle joins:

Oracle
Oracle
Oracle
Oracle

INNER JOIN ( simple join)


LEFT OUTER JOIN (LEFT JOIN)
RIGHT OUTER JOIN (RIGHT JOIN)
FULL OUTER JOIN ( FULL JOIN)

3. Explain difference between UNION and UNION ALL? When is it used? Write an example
using either one.
The union operator returns results from all the participating queries after eliminating duplications
The union all operator returns results from all the participating queries including the duplicate rows.
Note: number of columns and column data type returned by the query must match however the column
name can be different.
For e.g
Select first_name,last_name from emp
union
select first_name, to_char(age) from emp1;

4. What is an UPDATE? Write an example.


UPDATE statement is used to update existing records in a table.
UPDATE customers SET last_name = ANDY where customer_id= 50;

5. What are Synonyms in Oracle?


A synonym is an alternative name for objects such as tables,views,sequences,stored procedures and other
database objects.
Synonym is used to hide the owner of the object.
It work like a mirror of the object.
It is depend on object.
We can possible to create synonym of synonym.
Synonym are two types
a)private
b)public

6. What does COMMIT stand for? In which cases this has to be applied? What is ROLLBACK?
COMMIT: You can see any changes you have made during the transaction by querying the modified tables, but
other users cannot see the changes. After you commit the transaction, the changes are visible to other users.
ROLLBACK:

You can roll back (undo) any changes made during the transaction with

the ROLLBACK statement .

7. Give an example of NVL and explain why it is necessary to use in comparison?


EX: Select NVL(commission,0) from sales;
Using NVL function we can substitute a value at the place of Null Values.The substituted value then
temporarily replaces the NULL values in your calculations or expression.
That way,we do comparison easily.
Note: The substituted values only replaces the NULL value temporarily for session and does not affect the
value stored in the table.

II PL/SQL

8. What are the two parts of a package in PL/SQL? Specify the importance of creating
packages in PL/SQL.
PL/SQL packages are schema objects that groups logically related PL/SQL types, variables and subprograms.
A package will have two mandatory parts:
a) Package specification b)Package body or definition
Importance of Packages
1)Packages improve the performance of functions and procedures
2)Packages allow declare global variables
3)All the procedures and functions relating to a specific sub-system are in one program unit. This is good
design practice and easy to manage.
4)Security :Defining private procedures in the package body which can only be used by the package.
5)Hidden Implementation: Packages let you share your interface information in the package specification and
hide the implementation details in the package body.
6)Modularity: Packages encapsulate logically related variables, constants,subprograms,cursors and exceptions
in named PL/sql Modules.

9. What is the difference between a PROCEDURE and a FUNCTION?


Procedure:
1)It may or may not return value.
2)It has parameters IN,Out and INOUT.
3) Procedure are mainly implemented for business logic.
4)It cannot be used in expression directly.
5)It may or may not used RETURN keyword.
Function:
1)It should return a value.
2) It has only IN parameter.
3) Functions are for calculations.
4)It can be used in expression directly
5)Must used Return Keyword.

10. What is a cursor? Why Cursor is required?

A cursor is a pointer to a private SQL area that stores information about the processing of a SELECT or
data manipulation language (DML) statement (INSERT, UPDATE, DELETE, or MERGE).
Must used Return Keyword.
It required for row wise validation or in other way you can perform operation on each row.

11. When is SELECT INTO statement used? Write an example.


The SELECT INTO statement retrieves data from one or more database tables, and assigns the selected
values to variables
Declare
v_sal number(10);
begin
select salary into v_sal from emp where emp_id=10;

12. What is an Exception? What are the types of Exception?


Exception is the error handling part of PL/SQL block.
There are two types of Exceptions.

System-defined exceptions

User-defined exceptions

13. Explain what is meant by DYNAMIC SQL in PL/SQL and why it is sometimes required to
use?

III - SQL

We have the next tables:


4

EMPLOYEES
Employee_id
Last_name
First_name
Salary
Commission_pct
Department_id

DEPARTMENTS
Department_id
Department_name
Manager_id
Location_id

14. Create a query to display the employee numbers and last names of all employees who
earn more than the average salary. Sort the results in ascending order of salary.

15. Display the last name, department number and department_name of all employees
whose department location id is 1700.
select EMPLOYEES.Last_name,DEPARTMENTS.Department_id,DEPARTMENT.Department_name
from EMPLOYEES,DEPARTMENTS where Location_id=1700.

16. View the current value for the DEPT_DEPTID_SEQ sequence.


select DEPT_DEPTID_SEQ.currval from dual;

17. Insert a new department named Support in location ID 2500 using dept_deptid_seq
sequence.
insert into DEPARTMENTS ( Department_id,Department_name,Location_id)
values(dept_deptid_seq.nextval, Support ,2500);

18. Allow all users on the system to query data from Alices DEPARTMENTS table.

19. Change the last name of employee 3 to Dexter.


update EMPPLOYEES set Last_name=Dexter where Employee_id=3;

20.
21. Show how many employees have a name that ends with an n.
Select * from EMPLOYEES where First_name like %n;

IV Bonus questions (if time permits)

21. Explain and advantage of using %TYPE instead of declare data types on variables in
PL/SQL?

22. Write an example of DECODE in a SELECT statement using three states in a table called
STATES and column named STATECODE as an example OH=Ohio, IN=Indiana,
KY=Kentucky. Make sure to handle if some other state is in the column.
SELECT DECODE() FROM STATES;

23. What is a Sequence in Oracle? What is NEXTVAL? Write an example.

24. What is a MERGE? Write the syntax.

25. How many types of cursors exist in PL/SQL? Shortly explain, when each one of them is
used.
6

S-ar putea să vă placă și