Sunteți pe pagina 1din 12

1) How to display row number with records?

ANS: Select rownum, ename from emp; 2) How to view version information in Oracle? ANS: Select banner from v$version; 3) How to find the second highest salary in emp table? ANS: select min(sal) from emp a where 1 = (select count(*) from emp b where a.sal < b.sal) ; 4) How to delete the duplicate rows from a table? ANS: create table t1 ( col1 int, col2 int, col3 char(1) ); insert into t1 values(1,50, a); insert into t1 values(1,50, b); insert into t1 values(1,89, x); insert into t1 values(1,89, y); insert into t1 values(1,89, z); select * from t1; Col1 Col2 Col2 1 50 a 1 50 b 2 89 x 2 89 y 2 89 z delete from T1 where rowid <> ( select max(rowid) from t1 b where b.col1 = t1.col1 and b.col2 = t1.col2 ) 3 rows deleted. select * from t1; Col1 Col2 Col2 1 50 a 2 89 z

5) How to select a row using indexes? ANS: You have to specify the indexed columns in the WHERE clause of query. 6) How to select the first 5 characters of FIRSTNAME column of EMP table? ANS: select substr(firstname,1,5) from emp 7) What's the difference between a primary key and a unique key? ANS: Primary key does not allow nulls Unique key allow nulls. 8) What is a transaction and ACID? ANS: Transaction - A transaction is a logical unit of work. It must be commited or rolled back. ACID Atomicity, Consistency, Isolation and Duralbility, these are properties of a transaction. 9) How to display number value in words? ANS: select sal, (to_char(to_date(sal,'j'), 'jsp')) from emp; 10) What is candidate key, alternate key, composite key. ANS: Candidate Key: A candidate key is one that can identify each row of a table uniquely. Generally a candidate key becomes the primary key of the table. Alternate Key: If the table has more than one candidate key, one of them will become the primary key, and the rest are called alternate keys. Composite Key: A key formed by combining at least two or more columns is called composite key. 11) What are SQLCODE and SQLERRM and why are they important for PL/SQL developers? ANS: SQLCODE returns the current database error number. These error numbers are all negative, except NO_DATA_FOUND, which returns +100. SQLERRM returns the textual error message.. These are used in exception handling. 12) Explain the usage of WHERE CURRENT OF clause in cursors ? ANS: It refers to the latest row fetched from a cursor in an update and delete statement.

13) What are two virtual tables available during database trigger execution ? ANS: The table columns are referred as OLD.column_name and NEW.column_name. For INSERT only TRIGGERS NEW.column_name values ARE only available. For UPDATE only TRIGERS OLD.column_name NEW.column_name values ARE only available. For DELETE only TRIGGERS OLD.column_name values ARE only available.v 14) Is it possible to use Transaction control Statements such a ROLLBACK or COMMIT in Database Trigger ? Why ? ANS: It is not possible.,because of the side effect to transactions. You can use them indirectly by calling procedures or functions . 15) How will you stop an infinite loop without closing the program? ANS: Use the EXIT statement.This will stop the looping and proceed to the next executable statement. or EXIT WHEN condition 16) How do we display the column values of a table ? ANS: select cname from col where col 1 and tname 'emp' then you will get first clomun name of emp table; ex.
for in 1..2 loop select cname into cname1 from col where col i and tname 'emp'; cursor ec is select cname1 from emp; end loop;

17) Describe the difference between a procedure, function and anonymous pl/sql block ? ANS: Candidate should mention use of DECLARE statement, a function must return a value while a procedure doesn't have to. 18) What is a mutating table error and how can you get around it? ANS: This happens with triggers. It occurs because the trigger is trying to update a row it is currently using. The usual fix involves either use of views or temporary tables so the database is selecting from one while updating the other. 19) What packages (if any) has Oracle provided for use by developers? ANS: Oracle provides the DBMS_ series of packages. There are many which developers should be aware of such as DBMS_SQL, DBMS_PIPE, DBMS_TRANSACTION,DBMS_LOCK, DBMS_ALERT, DBMS_OUTPUT,

DBMS_JOB,DBMS_UTILITY, DBMS_DDL, UTL_FILE. If they can mention a few of these and describe how they used them, even better 20) Describe the use of PL/SQL tables ANS: PL/SQL tables are scalar arrays that can be referenced by a binary integer. They can be used to hold values for use in later queries or calculations. In Oracle 8 they will be able to be of the %ROWTYPE designation, or RECORD. 21) When is a declare statement needed ? ANS: The DECLARE statement is used in PL/SQL anonymous blocks such as with stand alone, non-stored PL/SQL procedures. It must come first in a PL/SQL stand alone file if it is used 22) In what order should a open/fetch/loop set of commands in a PL/SQL block be implemented if you use the NOTFOUND cursor variable in the exit when statement? Why? ANS: OPEN then FETCH then LOOP followed by the exit when. If not specified in this order will result in the final return being done twice because of the way the %NOTFOUND is handled by PL/SQL 23) What are SQLCODE and SQLERRM and why are they important for PL/SQL developers? ANS: SQLCODE returns the value of the error number for the last error encountered. The SQLERRM returns the actual error message for the last error encountered. They can be used in exception handling to report, or, store in an error log table, the error that occurred in the code. These are especially useful for the WHEN OTHERS exception. 24) How can you generate debugging output from PL/SQL? ANS: DBMS_OUTPUT SHOW_ERRORS UTL_FILE 25) What are the types of triggers? ANS: Statement Level, Row Level, Instead of Trigger 26) What is the difference between explicit cursor and implicit cursor? ANS: When a single insert, delete or update statement is executed within PL/SQL program then oracle creates an implicit cursor for the same, executes the statement, and closes the cursor. You can check the result of execution using SQL%ROWCOUNT function.

Explicit cursors are created programmatically. The cursor type variable is declared and associated with SQL query. The program then opens a cursor, fetches column information into variables or record type variable, and closes cursor after all records are fetched. To check whether cursor is open or not use function SQL %ISOPEN and to check whether there are any records to be fetched from the cursor use function SQL%FOUND. 27) Why does a query in Oracle run faster when ROWID is used as a part of the where clause? ANS: ROWID is the logical address of a row - it is not a physical column. It is composed of file number, data block number and row number within data block. Therefore I/O time is minimized retrieving the row, resulting in a faster query. 28) You want to view top 50 rows from Oracle table. How do I this? ANS: Use ROWNUM, the pseudo column in where clause as follows: Where rownum < 51 29) how can a function retun more than one value in oracle with proper example? ANS: Using Ref cursor and pipelines 30) What is difference b/w stored procedures and application procedures,stored function and application function ? ANS: Stored procedures are subprogrammes stored in the database and can be called &executee multiple times wherein an application procedure is the one being used for a particular application same is the way for function. 31) Explian rowid,rownum?what are the psoducolumns we have? ANS: ROWID - Hexa decimal number each and every row having unique.Used in searching. ROWNUM - It is a integer number also unique for sorting Normally TOP N Analysys. Examples for Psudo columns are ROWNUM,SYSDATE,ROWID,LEVEL,USER etc

32) How we can create a table in PL/SQL block. insert records into it??? is it possible by some procedure or function?? please give example ? ANS: CREATE OR REPLACE PROCEDURE ddl_create_proc (p_table_name IN
VARCHAR2) AS l_stmt VARCHAR2(200);

BEGIN DBMS_OUTPUT.put_line('STARTING '); l_stmt := 'create table '|| p_table_name || ' as (select * from emp )'; execute IMMEDIATE l_stmt; DBMS_OUTPUT.put_line('end '); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.put_line('exception '||SQLERRM || 'message'||sqlcode); END;

33) How to avoid using cursors? What to use instead of cursor and in what c cases to do so? ANS: Use sbuquery in the for loop below
For emprec in (select * from emp) loop dbms_output.put_line(emprec.empno); end loop; no exit statement needed implicit open,fetch,close occurs

34) ANS:

Difference between a cursor and reference cursor ? REF CURSOR have a return type and it as 2 type Strongly Typed Cursor and Weakly Typed Cursor but Cursor doesn't have return type Ex:
TYPE ref_type_name IS REF CURSOR RETURN return_type; return_type represents a record in the database DECLARE TYPE EmpCurType IS REF CURSOR RETURN emp%ROWTYPE;

35) Insert all the rows into another table using table emp ? ANS: declare
TYPE NumTab IS TABLE OF emp%ROWTYPE; a1 NumTab; begin select * bulk collect into a1 from emp; forall i in a1.first..a1.last insert into test values a1(i); end;

36) View and delete duplicate rows ? ANS: select * from test t where rowid > ( select min(rowid) from test b where
b.empno=t.empno) DELETE: delete * from test t where rowid > ( select min(rowid) from test b where b.empno=t.empno)

37)
ANS:

What is correlated query ?


If a sub query has a Reference with the value or values of Main Query , It is called as Correlated Sub query. SELECT * FROM EMP E WHERE E.SAL>( SELECT AVG(SAL) FROM EMP F WHERE E.DEPTNO= F.DEPTNO);

38)
ANS:

What is autonomous transaction?


An autonomous transaction is an independent transaction that is initiated by another transaction (the parent transaction). An autonomous transaction can modify data and commit or rollback independent of the state of the parent transaction. The autonomous transaction must commit or roll back before the autonomous transaction is ended and the parent transaction continues. PROCEDURE test_autonomous IS PRAGMA AUTONOMOUS_TRANSACTION; BEGIN insert .... commit; END test_autonomous;

39) How can i insert data into a table with 3 columns using FORALL?
ANS: Suppose the table is emp have the following columns 1. empname varchar 2. empid number 3. sal number declare type name_typ is table of emp.empname%type; type id_typ is table of emp.empid%type; type sal_typ is table of emp.sal%type; name_tab name_typ; id_tab id_typ; sal_tab sal_typ; begin name_tab := name_typ('ramit','rohan'); id_tab := id_typ(10,20); sal_tab := sal_typ(21000,22000); for all i in name_tab.first .. name_tab.last insert into emp values(name_tab(i),id_tab(i),sal_tab(i)); end;

40)
ANS:

Real time applications of nullif?


Nullif basically allows you to compare 2 expressions of same datatype and return null if both are equal. else it would return the first expression.eg:

select nullif(10,10) from dual

41) How to get the third quarter of employee details from emp?
ANS: select * from emp where rownum<=(select round((count(*)/4) *3) from emp) minus select * from emp where rownum<=(select round((count(*)/4)*2) from emp)

42) What is the different between Stored Procedure and Procedure?


ANS: Two are same.Once the procedure is created and compiled ,it is stored in database permanently.That's why it is called as stored procedure.

43)
ANS:

HOW CAN I FIND MAX SAL ,ENAME FROM EMP TABLE?


select Sal, name from emp where sal=(select max(sal) from emp);

44)
ANS:

Difference between CHAR and VARCHAR2 ?


In char it allocates the memory space as static where as in varchar2 it allocates the memory space as dynamic

45) When the mutating error will comes? and how it will be resolved?
ANS: Mutating error in Trigger:When programmer create trigger and give table name abc and in body if programmer is using same table abc for selecting,updating,deleting,inserting then mutation occur. ex.:create or replace trigger xyz after update on abc for each row referencing :OLD as OLD :NEW as NEW begin select max(salary) from abc; update abc set location_id=:NEW.location_id where dept_id=105; end; To overcome the above problem we need to create a autonamous trasaction trigger..

46) Can we use SQL%ISOPEN in implicit cursors? Does this attribute works properly in Implicit Cursors?
Ans: Implicit cursors: SQL%ISOPEN always returns FALSE, indicating that the implicit cursor has been closed. I hope below example gives you very fair idea. SQL> BEGIN 2 UPDATE employee 3 SET salary = salary *2 4 WHERE id = '01';

5 6 IF not SQL%ISOPEN THEN 7 DBMS_OUTPUT.PUT_LINE('closed'); 8 END IF; 9 END; 10 / Closed PL/SQL procedure successfully completed.

47)
ANS:

What is Composite index. how to create it?


Composite index is the index created on multiple columns of a table. A maximum of 16 columns can be used as a composite index. It can be created at table level only. Unique, Primary Key and Foreign Key can be composite keys. Ex: Create table emp (eno number,ename varchar(5),dob date,sal number, dno number,Primary Key(eno, ename) -- Composite Index

48)
ANS:

Why use cursors?


Mainly cursor is used to process the multiple records from database.

49) With out using count() function. How to the find total number of rows in a t table?
ANS: select sum(1) from emp select max(rownum) from emp

50) Is UNION ALL and Natural Join does the same operation and are same ?
ANS: Natural join is the same as an equi join on (emp.deptno = dept.deptno). Also, no more than two tables can be joined using this method. So, it is best to avoid natural joins as far as possible UNION ALL query allows you to combine the result sets of 2 or more "select" queries. It returns all rows (even if the row exists in more than one of the "select" statements. within the UNION ALL query must have the same number of fields in the result sets with similar data types.

51)
ANS:

display 1 to 10 numbers using one select statement ?


select level from dual connect by level<=10 Use sequence. automatically increments.

52)
ANS:

Insert 1 to 10000 values in table ?


declare i number; begin for i in 1..10000 LOOP insert into test(empno) values (i); dbms_output.put_line(i);

END LOOP; end ; /

53) How One can easily select all even, odd, or Nth rows from a table using SQL queries?
ANS: Odd number of records: select * from emp where (rowid,1) in (select rowid,mod(rownum,2) from emp); Output:1 3 5 Even number of records: select * from emp where (rowid,0) in (select rowid,mod(rownum,2) from emp) Output:2 4 6 For nth number, Example we r considering number n=10 select * from emp where (rowid,1) in (select rowid,rownum/:N) from emp)

54) What are the differences among these table level lock modes - IN SHARE MODE, IN SHARE UPDATE MODE,IN EXCLUSIVE MODE ?
ANS: In share mode : this mode is for read only on entire table.we can not make changes to table. any user can have a lock in share mode at same time. In share update mode : this mode is used to lock the selected rows for update. this mode acquires lock on selected rows only not entire table.other user can have lock on other rows on the same table but not on the rows you have locked.

In exclusive mode : this acquires lock on entire table.another user can not have any lock on that table.

55)
ANS:

Eliminate duplicate without using roenum and not ?


VIEW DUPLICATE ROWS SELECT * FROM TEST A WHERE ROWID > (SELECT min(rowid) FROM TEST B WHERE A.empno = B.empno); DELETE DELETE * FROM emp1 A WHERE ROWID > (SELECT min(rowid) FROM emp1 B WHERE A.empno = B.empno);

56) how to create user in sql and how to set password for that?
ANS: create user <username> IDENTIFIED BY password;

57)
ANS:

Do view contain data?


No, View is a logical table associated with a query. It will not store any data in it.Only the query with which the view is created is stored in the database.

58) Write one update command to update seqno field of a table on the basis of row number ?
ANS: You can update the seqno which is having the rownum=1 only. for eg: If you have a table dept in scott user, you can try the following command:update dept set deptno=90 where rownum=1;

System Table Description


ALL_ARGUMENTS ALL_CATALOG ALL_COL_COMMENTS ALL_CONSTRAINTS ALL_CONS_COLUMNS ALL_DB_LINKS ALL_ERRORS ALL_INDEXES ALL_IND_COLUMNS ALL_LOBS ALL_OBJECTS ALL_OBJECT_TABLES ALL_SEQUENCES ALL_SNAPSHOTS ALL_SOURCE Arguments in object accessible to the user All tables, views, synonyms, sequences accessible to the user Comments on columns of accessible tables and views Constraint definitions on accessible tables Information about accessible columns in constraint definitions Database links accessible to the user Current errors on stored objects that user is allowed to create Descriptions of indexes on tables accessible to the user COLUMNs comprising INDEXes on accessible TABLES Description of LOBs contained in tables accessible to the user Objects accessible to the user

Description of all object tables accessible to the user Description of SEQUENCEs accessible to the user Snapshots the user can access Current source on stored objects that user is allowed to create ALL_SYNONYMS All synonyms accessible to the user ALL_TABLES Description of relational tables accessible to the user ALL_TAB_COLUMNS Columns of user's tables, views and clusters ALL_TAB_COL_STATISTICS Columns of user's tables, views and clusters ALL_TAB_COMMENTS Comments on tables and views accessible to the user ALL_TRIGGERS Triggers accessible to the current user ALL_TRIGGER_COLS Column usage in user's triggers or in triggers on user's tables ALL_TYPES Description of types accessible to the user ALL_UPDATABLE_COLUMNS Description of all updatable columns ALL_USERS Information about all users of the database ALL_VIEWS Description of views accessible to the user

DATABASE_COMPATIBLE_LEVEL DBA_DB_LINKS DBA_ERRORS DBA_OBJECTS DBA_ROLES DBA_ROLE_PRIVS DBA_SOURCE DBA_TABLESPACES DBA_DEPENDENCIES DBA_TAB_PRIVS DBA_TRIGGERS DBA_TS_QUOTAS DBA_USERS DBA_VIEWS DICTIONARY DICT_COLUMNS GLOBAL_NAME

Database compatible parameter set via init.ora All database links in the database Current errors on all stored objects in the database All objects in the database All Roles which exist in the database Roles granted to users and roles Source of all stored objects in the database Description of all tablespaces Gives the relation about the depedencies of all objects All grants on objects in the database All triggers in the database Tablespace quotas for all users Information about all users of the database Description of all views in the database Description of data dictionary tables and views Description of columns in data dictionary tables and views global database name

NLS_DATABASE_PARAMETERS Permanent NLS parameters of the database NLS_INSTANCE_PARAMETERS NLS parameters of the instance NLS_SESSION_PARAMETERS NLS parameters of the user session PRODUCT_COMPONENT_VERSION ROLE_TAB_PRIVS SESSION_PRIVS SESSION_ROLES SYSTEM_PRIVILEGE_MAP TABLE_PRIVILEGES the TABLE_PRIVILEGE_MAP numbers to Version and status information for component products Table privileges granted to roles Privileges which the user currently has set Roles which the user currently has enabled. Description table for privilege type codes. Maps privilege type numbers to type names Grants on objects for which the user is the grantor, grantee, owner, or an enabled role or PUBLIC is grantee Description table for privilege (auditing option) type codes. Maps privilege (auditing option) type type names

60)

3rd highest sal

select min(sal) from (select sal from emp order by sal desc) where rownum <= 3

61)

2nd Highest sal

select max(sal) from emp where sal<(select max(sal) from emp) select * from emp a where 2=(select count(distinct sal) from emp b where a.sal<=b.sal)

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