You are on page 1of 17

1

Oracle Interview Q & A


1. Views provide many advantages, like: A view is a logical table which makes a complex query easy.We can even create a complex view by joining two tables. (a) They restrict access to the whole data, because they display only selective columns. (b) They can be used to make complex queries easy. A user can use a simple query on a view to display data from multiple tables, without having the knowledge of how to join tables in queries. (c) Different views can be created from the same data as per the requirements of different types of use groups. A view does not contain any data of its own, but is like a window through which data from other tables can be viewed and changed. A view does not contain any data of its own, but is like a window through which data from other tables can be viewed and changed. 2. What is an Oracle index? An Index is a tree structure that allows direct access to a row in a table. Indexes can be classified based on their logical design or on their physical implementation. The Logical classification groups indexes from an application perspective, while the physical classification is derived from the way the indexes are stored. 3. What is database link? A database link is a pointer in the local database that allows you to access on a remote database. 4. What are the types of database links? Oracle allows you to create private, public, and global database links.

Private Database Link: You can create a private database link in a specific schema of a database. Only the owner of a private database link or PL/SQL subprograms in the schema can

2
use a private database link to access data and database objects in the corresponding remote database. Public Database Link : You can create a public database link for a database. All users and PL/SQL subprograms in the database can use a public database link to access data and database objects in the corresponding remote database. Global Database Link - When an Oracle network uses Oracle Names, the names servers in the system automatically create and manage global database links for every Oracle database in the network. All users and PL/SQL subprograms in any database can use a global database link to access data and database objects in the corresponding remote database. A private database link is more secure than a public or global link, because only the owner of the private link, or subprograms within the same schema, can use the private link to access the specified remote database. When many users require an access path to a remote Oracle database, an administrator can create a single public database link for all users in a database. When an Oracle network uses Oracle Names, an administrator can conveniently manage global database links for all databases in the system. Database link management is centralized and simple. 5. What is data block? Block is the smallest unit of storage in the logical structure of the database where actual table rows are stored. 6. What is an Index ? How it is implemented in Oracle Database ? An index is a database structure used by the server to have direct access of a row in a table. An index is automatically created when a unique of primary key constraint clause is specified in create table common (Ver 7.0) 7. What is sql? what is the difference between sql and pl/sql? SQL: is a structured query language which subdivided in to 5 categories like DDL, DML, DQL, DCL, TCL

3
PLSQL: is a programming language structured query language which consists of procedure, function, triggers, collection.

select * from emp a where 3=(select distinct(count(b.salary))from emp b where a.salary<=b.salary);


1)Select the 2nd row in the table? 2)Select the row in between 3rd to 10th row? 3)Select even number in emp table?

1) select * from emp where rowid in(select decode(rownum,2,rowid) from emp); EMP_NO EMP_NAME 101 RAJ SALARY 50000 --------- ------------------------- ---------

2) select * from emp where rowid in(select decode(rownum,3,rowid,10,rowid) from emp); EMP_NO EMP_NAME 102 A S KALA 109 SUDHA 30000 SALARY -------- ------------------------- ---------

3) select * from emp where rowid in(select decode(mod(rownum,2),0,rowid) from emp); EMP_NO EMP_NAME 101 RAJ 103 JESLIN FANTA MALAR 105 MURUGU 107 SARABOOT 109 SUDHA 111 SAVI
6 rows selected.

SALARY 50000 300000 500000 550000 30000

--------- ------------------------- ---------

SQL> select * from emp; EMP_NO EMP_NAME 100 SELVA 101 RAJ 102 A S KALA 103 JESLIN FANTA MALAR 104 ANITA 105 MURUGU 106 SRIVATSAN 107 SARABOOT 108 KARTHI SIR 109 SUDHA 110 MERCHI 111 SAVI 12 rows selected. 8. Char & Varchar difference? CHAR pads blank spaces to the maximum length. VARCHAR2 does not pad blank spaces. For CHAR it is 255 and 2000 for VARCHAR2. 400000 500000 450000 550000 600000 30000 40000 40 30 30 40 20 30 SALARY 21000 50000 10 300000 20 DEPT_NO M

--------- ------------------------- --------- --------- ----

9. What will the Output for this Coding? Declare Cursor c1 is select * from emp FORUPDATE; Z c1%rowtype; Begin Open C1; Fetch c1 into Z; Commit; Fetch c1 in to Z; ?

By declaring this cursor we can update the table emp through z,means wo not need to write table name for updation,it may be only by "z". selecting in FOR UPDATE mode locks the result set of rows in update mode, which means that row cannot be updated or deleted until a commit or rollback is issued which will release the row(s).

10. The procedure executes successfully. COMMIT will not close the cursor. declare b boolean; cursor c is select * from emp for update; rec c%rowtype; begin open c; fetch c into rec; dbms_output.put_line('NAME is'||rec.ename); commit; if c%isopen then dbms_output.put_line('Cursor is not closed '||c%rowcount); end if; fetch c into rec; dbms_output.put_line('NAME is'||rec.ename); end; O/P NAME isSMITH Cursor is not closed 1 NAME isALLEN 11. What is the fastest way of accessing a row in a table? Using ROWID.CONSTRAINTS 12. What is ROWID? ROWID is a pseudo column attached to each row of a table. It is 18 character long, blockno, rownumber are the components of ROWID. 13. Explain UNION, MINUS, UNION ALL and INTERSECT? INTERSECT returns all distinct rows selected by both queries.MINUS - returns all distinct rows selected by the first query but not by the second.UNION - returns all distinct rows selected by either queryUNION ALL - returns all rows selected by either query, including all duplicates. 14. Difference between SUBSTR and INSTR? INSTR (String1,String2(n,(m)),INSTR returns the position of the mth occurrence of the string 2 instring1. The search begins from nth position of string1.SUBSTR (String1 n,m)SUBSTR returns a character string of size m in string1, starting from nth position of string1. 15. What is correlated sub-query?

6
Correlated sub query is a sub query which has reference to the main query. 16. Explain CONNECT BY PRIOR? Retrieves rows in hierarchical order.e.g. select empno, ename from emp where. 17. What is difference between TRUNCATE & DELETE? TRUNCATE commits after deleting entire table i.e., can not be rolled back. Database triggers do not fire on TRUNCATEDELETE allows the filtered deletion. Deleted records can be rolled back or committed.Database triggers fire on DELETE. 18. What is a transaction? It is a operation on database / table. which has to be confirmed after completion. It may be single sql stm or multiple stm 19. What are the types of SQL statement? Data definition Language Data Manipulation language data Control Language Senssion Control System Control 20. How to get/select the nth row from the table ? How to select first n rows ,last n rows from a table? nth salary select salary from table_name a where &n=(select count(salary) from table_name b where a.salary<=b.salary); n salaries select salary from table_name a where &n>=(select count(salary) from table_name b where a.salary<=b.salary); To select LAST n rows:

SELECT * FROM emp a WHERE &n >= (SELECT COUNT(EMP_NO) FROM emp b WHERE a.ROWID<=b.ROWID); Ex: EMP_NO EMP_NAME SALARY DEPT_NO ------- ------------------------- --------- --------- -

7
109 SUDHA 110 MERCHI 111 SAVI
To select FIRST n rows:

30000 40000

20 30 40

SELECT * FROM emp a WHERE &n >= (SELECT COUNT(EMP_NO) FROM emp b WHERE a.ROWID>=b.ROWID); EMP_NO EMP_NAME 100 SELVA 101 RAJ 102 A S KALA
How do i replace a comma (,) with a blank in a select statement? No answer available. If you have the answer, then send it to us. We will display your answer after the approval. 21. Implicit Cursor attributes? SQL%FOUND,SQL%NOT FOUND, SQL%IS OPEN, SQL%ROW COUNT 22. Explicit Cursor attributes? %IS OPEN,%FOUND,%NOT FOUND,%ROW COUNT 23. How do I eliminate the duplicate rows? Use the DISTINCT keyword right after SELECT... i.e. SELECT DISTINCT customername FROM customer select * from emp e where rownum=(select max(rownum) from emp ee where e.empno=ee.empno) 24. Display the records between two range I know the nvl function only allows the same data type(ie. number or char or date Nvl(comm, 0)), if commission is null then the text Not Applicable want to display, instead of blank space. How do I write the query? You can use the decode function for the above requirement. Please find the query as below: select ename,decode(nvl(comm,0),0,'Not Applicable',comm) from scott.emp; Ex:

SALARY 21000 50000

DEPT_NO

------ ------------------------- --------- --------- -

10

8
SQL> select EMP_NAME,decode(nvl(COMMISSION,0),0,'Not Applicable',COMMISSION)from emp; EMP_NAME SELVA RAJ A S KALA JESLIN FANTA MALAR ANITA MURUGU SRIVATSAN SARABOOT KARTHI SIR SUDHA MERCHI SAVI 12 rows selected. 25. How do I display row number with records? SELECT rownum,table_name.* FROM table_name; Ex: SQL> select rownum,emp.* from emp; ROWNUM 1 2 3 4 5 6 7 8 9 10 11 12 EMP_NO 100 101 102 A S 103 104 105 106 107 108 109 110 111 EMP_NAME SELVA RAJ KALA JESLIN FANTA MALAR ANITA MURUGU SRIVATSAN SARABOOT KARTHI SIR SUDHA MERCHI SAVI 300000 400000 500000 450000 550000 600000 30000 40000 30 30 40 20 30 40 20 SALARY 21000 50000 10 DEPT_NO 10500 1000 1700 Not Applicable Not Applicable Not Applicable 1750 Not Applicable DECODE(NVL(COMMISSION,0),0,'NOTAPPLICABL 5000 5660 7000 Not Applicable

------------------------- ----------------------------------------

--------- --------- ------------------------- --------- --------- --

12 rows selected. 26. How do I eliminate the duplicate rows? Use the DISTINCT keyword right after SELECT... i.e. SELECT DISTINCT customername FROM customer select * from emp e where rownum=(select max(rownum) from emp ee where e.empno=ee.empno); Ex: SQL> select * from emp e where rownum=(select MIN(rownum) from emp ee where e.EMP_NO=ee.EMP_NO); EMP_NO EMP_NAME SALARY DEPT_NO MGR_ID JOB

HIRE_DATE COMMISSION GROSS_SALARY --------- ------------------------- --------- --------- --------- -------------------- --------- ---------------------100 SELVA 21000 ENGINEER 25-MAR-08 5000

27. If a view on a single base table is manipulated will the changes be reflected on the base table? if view on based on a single table then u can execute any DML directly on it and can see the changes in the base table and if view is based on join of 2 tables then only one base table can be modified at one time so in order to make changes in both the tables use instead of triggers. 28. What are the advantages of VIEW? of view: 1. Restricts the access to particular columns and rows of the base tables. 2. Hide the data complexity. 3. Can access the data for two different base tables with out performing a join. 4. Can display the data in different form from the base tables.(i.e. In the column names can can be changed with effecting the column names of the base tables).

29. How to access the current value and next value from a sequence? Would like to give you a small example to use the sequence.currval and sequence.nextval create sequence seq_name. start with 1 minvalue 1

10
maxvalue 999 increment by 1 nocycle insert into table_name (sno,name) values (seqname.nextval,'abc'); select seqname.currval from dual; 30. What is CYCLE/NO CYCLE in a Sequence? CYCLE specifies that the sequence continue to generate values after reaching either maximum or minimum value. After pan-ascending sequence reaches its maximum value, it generates its minimum value. After a descending sequence reaches its minimum, it generates its maximum. NO CYCLE specifies that the sequence cannot generate more values after reaching its maximum or minimum value. 31. Where the integrity constraints are stored in data dictionary? The integrity constraints are stored in USER_CONSTRAINTS. 32. What is the maximum SIZE allowed for each type? The Max Size is upto 32. 33. How will you activate/deactivate integrity constraints? The integrity constraints can be enabled or disabled by ALTER TABLE ENABLE constraint/DISABLE constraint. 34. To view installed Oracle version information? select * from v$version; 35. Display the number value in Words? select sal, (to_char(to_date(sal,'j'), 'jsp')) from emp; Ex: SQL> SELECT Emp_No,Emp_Name,Salary,(to_char(to_date(Salary,'j'), 'jsp')) FROM emp; EMP_NO EMP_NAME 100 SELVA 101 RAJ 102 A S KALA 103 JESLIN FANTA MALAR 104 ANITA 105 MURUGU 300000 three hundred thousand 400000 four hundred thousand 500000 five hundred thousand SALARY (TO_CHAR(TO_DATE(SALARY,'J'),'JSP')) 21000 twenty-one thousand 50000 fifty thousand

--------- ------------------------- --------- ------------------------------------

11
106 SRIVATSAN 107 SARABOOT 108 KARTHI SIR 109 SUDHA 110 MERCHI 111 SAVI 12 rows selected. 36. Display Odd/ Even number of records? Odd number of records: select * from emp where (rowid,1) in (select rowid, mod(rownum,2) from emp); 1 3 5 Even number of records: select * from emp where (rowid,0) in (select rowid, mod(rownum,2) from emp) 2 4 6 37. Which date function returns number value? Months_between. This date function takes 2 valid dates and returns number of months in between them. Difference between NO DATA FOUND and %NOTFOUND? NO DATA FOUND is an exception raised only for the SELECT....INTO statements when the where clause of the query does not match any rows. When the where clause of the explicit cursor does not match any rows the %NOTFOUND attribute is set to TRUE instead. NO_DATA_FOUND is pre defind exception of oracle. when we can't get any data from the table for any query. In that case we will get error. %NOT FOUND is one of the attribute of explicit cursor. when can't get any data from explicit cursor in that case %NOT FOUND will returns true otherwise it returns false. What is a cursor for loop? Cursor For Loop is a loop where oracle implicitly declares a loop variable, the loop index that of the same record type as the cursor's record. 450000 four hundred fifty thousand 550000 five hundred fifty thousand 600000 six hundred thousand 30000 thirty thousand 40000 forty thousand

12
When we use for loops,there is no need to declare explicit cursor. Only case of for loop,cursor is implicitly open and after fetching the data cursor is implicitly closed. Example given below SET SERVEROUTPUT ON BEGIN FOR emp_record IN (SELECT last_name,department_id FROM employees) LOOP IF emp_record.department_id=80 THEN DBMS_OUTPUT.PUT_LINE('employees'||'emp_record.last_name'||'works for sales department'); END IF; END LOOP;--implicit cursor closed END; Difference between procedure and function? Functions are named PL/SQL blocks that return a value and can be called with arguments procedure a named block that can be called with parameter. A procedure all is a PL/SQL statement by itself, while a Function call is called as part of an expression. What are different modes of parameters used in functions and procedures? -IN -OUT -INOUT

PL / SQL
1. What is trigger,cursor,functions in pl-sql and we need sample programs about it? Trigger is an event driven PL/SQL block. Event may be any DML transaction. Cursor is a stored select statement for that current session. It will not be stored in the database, it is a logical component. Function is a set of PL/SQL statements or a PL/SQL block, which performs an operation and must return a value. Cursor cursor is a private sql work area. Every sql statement executed by oracle server has an individual cursor associated with it. Cursor are two types 1.Implicit cursor

13
2.Explicit cursor Implicit cursor: Implicit cursors are declared by pl/sql implicitly at the time of DML statement and select statement in pl/sql including queries that returns single row. cursor have four attributes 1. SQL%ROWCOUNT 2. SQL%ISOPEN 3. SQL%NOTFOUND 4. SQL%FOUND SQL%ROWCOUNT-Basically it returns number. means number of rows affected by present sql statement. SQL%ISOPEN-Always evalutes false because implicit cursor automatically closed after execution of sql statement. SQL%FOUND-Always evalutes true because one or more rows are affected by recent sql statement SQL%NOTFOUND-Always evalutes true when no rows are affected by present sql statement. example of explicit cursor: DECLARE v_empno employees.employee_id%TYPE; v_name employees.last_name%TYPE; CURSOR emp_cur IS SELECT employee_id,last_name FROM employees BEGIN OPEN emp_cur; LOOP FETCH emp_cur INTO v_empno,v_name ; EXIT WHEN emp_cur%ROWCOUNT>10 OR emp_cur%NOTFOUND; DBMS_OUTPUT.PUT_LINE('employee_id:'||TO_CHAR(v_empno) || 'employee_name:'||'v_name'); END LOOP; CLOSE emp_cur; END;

14

Trigger:-Trigger is pl/sql block or procedure that is associated with table,view,schema and database. Execute immidiately when particular event take place. there are two types of trigger 1.Application trigger:fires automatically when event occurs with particular application. 2.Database trigger:Fires when data such as DML oparation occured at that time. DML triggers are two types 1.Statementlevel trigger 2.Rowlevel trigger statement level trigger-statement level trigger means trigger body execute once for the triggering event.this is default.A statement level trigger fire once even no rows are affected at all. Row level- Trigger body execute once for each row affected by triggering event.if no rows are affected in that case trigger body not executed. trigger example CREATE OR REPLACE TRIGGER secure_emp BEFORE INSERT ON employees BEGIN TO_CHAR(SYSDATE,'DY')IN('SUN','SAT') OR TO_CHAR((SYSDATE,'HH24:MI')NOT BETWEEN '08:00' AND '18:00')THEN RAISE_APPLICATION_ERROR(-20253,'u may insert employee information at business hrs'); END; 2. In pl/sql functions what is use of out parameter even though we have return statement. With out parameters you can get the more than one out values in the calling program. It is recommended not to use out parameters in functions. If you need more than one out values then use procedures instead of functions. We can't use OUT paramter in function.We must have to use RETURN to pass values out of function otherwise use procs. 3. How we can create a table through procedure ? You can create table from procedure using Execute immediate command. create procedure p1 is begin EXECUTE IMMEDIATE 'CREATE TABLE temp AS SELECT * FROM emp ' ;

15
END; / 4. State the difference between implicit and explicit cursor's. Implicit Cursor are declared and used by the oracle internally. whereas the explicit cursors are declared and used by the user. more over implicitly cursors are no need to declare oracle creates and process and closes autometically. the explicit cursor should be declared and closed by the user. Implict cursor can be used to handle single record (i.e) the select query used should not yield more than one row. if u have handle more than one record then Explict cursor should be used.

5. What are the two parts of a procedure ? Procedure Specification and Procedure Body. 6. What is difference between a PROCEDURE & FUNCTION ? A FUNCTION is always returns a value using the return statement. A PROCEDURE may return one or more values through parameters or may not return at all. A function can be called from sql statements and queries while procedure can be called in a begin end block only. In case of function,it must have return type. In case of procedure,it may or may n't have return type. In case of function, only it takes IN parameters IN case of procedure only it take IN,OUT,INOUT parameters. 7. What is a stored procedure ? A stored procedure is a sequence of statements that perform specific function. A stored procedure is a named pl/sql block which performs an action.It is stored in the database as a schema object and can be repeatedly executed.It can be invoked, parameterised and nested. 8. What is Raise_application_error ? Raise_application_error is a procedure of package DBMS_STANDARD which allows to issue an user_defined error messages from stored sub-program or database trigger. RAISE_APPLICATION_ERROR is a procedure of package DBMS which allows to issue user_defined error message. 9. Name the tables where characteristics of Package, procedure and functions are stored ?

16
User_objects, User_Source and User_error. 10. What are two parts of package ? The two parts of package are PACKAGE SPECIFICATION & PACKAGE BODY. Package Specification contains declarations that are global to the packages and local to the schema. Package Body contains actual procedures and local declaration of the procedures and cursor declarations. package has two parts: 1.Package specification 2.Package body In the specification,where we declare variable,function,procedure that is global to the package and local to the schema. package body contains the defination of the function,procedure.we can also declare private function and procedure which is not accessble out side the package. 11. What are advantages fo Stored Procedures? Extensibility,Modularity, Reusability, Maintainability and one time compilation. 12. Difference between DBMS & RDBMS? DBMS stands for Database Management System which is a general term for a set of software dedicated to controlling the storage of data. RDMBS stand for Relational DataBase Management System. This is the most common form of DBMS. Invented by E.F. Codd, the only way to view the data is as a set of tables. Because there can be relationships between the tables, people often assume that is what the word "relational" means. Not so. Codd was a mathematician and the word "relational" is a mathematical term from the science of set theory. It means, roughly, "based on tables".

Program 1 SET SERVEROUTPUT ON BEGIN FOR emp_record IN (SELECT last_name,department_id FROM employees) LOOP IF emp_record.department_id=80 THEN DBMS_OUTPUT.PUT_LINE('employees'||'emp_record.last_name'||'works for sales department'); END IF;

17
END LOOP;--implicit cursor closed END; SQL> / employees SARABOOT works for sales department PL/SQL procedure successfully completed. Program 2

SQL> DECLARE v_empno emp.EMP_NO%TYPE; v_name emp.EMP_NAME%TYPE; CURSOR emp_cur IS SELECT EMP_NO,EMP_NAME FROM emp; BEGIN OPEN emp_cur; DBMS_OUTPUT.PUT_LINE('EMPLOYEE ID'||' '|| 'EMPLOYEE NAME'); DBMS_OUTPUT.PUT_LINE('--------------------------------------------'); LOOP FETCH emp_cur INTO v_empno,v_name ; EXIT WHEN emp_cur%ROWCOUNT>4 OR emp_cur%NOTFOUND; DBMS_OUTPUT.PUT_LINE(TO_CHAR(v_empno)||' '|| v_name); END LOOP; END; / EMPLOYEE ID EMPLOYEE NAME --------------------------------------------100 SELVAA 101 RAJ 102 A S KALA 103 JESLIN FANTA MALAR PL/SQL procedure successfully completed.

You might also like