Oracle PLSQL

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Question: What are the wildcards used for pattern matching.

Answer: _ for single character substitution and % for multi-character substitution.

Question: How can I hide a particular table name of our schema?


Answer: you can hide the table name by creating synonyms.

e.g) you can create a synonym y for table x

create synonym y for x;

Question: When we give SELECT * FROM EMP; How does oracle respond:
Answer: When u give SELECT * FROM EMP;

the server check all the data in the EMP file and it displays the data of the EMP file

Question: What is the use of CASCADE CONSTRAINTS?


Answer: When this clause is used with the DROP command, a parent table can be dropped even when a
child table exists.

Question: There are 2 tables, Employee and Department. There are few records in employee table, for
which, the department is not assigned. The output of the query should contain all th employees names
and their corresponding departments, if the department is assigned otherwise employee names and null
value in the place department name. What is the query?
Answer: What you want to use here is called a left outer join with Employee table on the left side. A left
outer join as the name says picks up all the records from the left table and based on the joint column
picks the matching records from the right table and in case there are no matching records in the right
table, it shows null for the selected columns of the right table. E.g. in this query which uses the key-word
LEFT OUTER JOIN. Syntax though varies across databases. In DB2/UDB it uses the key word LEFT
OUTER JOIN, in case of Oracle the connector is Employee_table.Dept_id *= Dept_table.Dept_id

SQL Server/Sybase :

Employee_table.Dept_id(+) = Dept_table.Dept_id

Question: on index
why u need indexing? Where that is stored
and what u mean by schema object?
For what purpose we are using view
Answer: We can?t create an Index on Index. Index is stored in user_index table. Every object that has
been created on Schema is Schema Object like Table, View etc. If we want to share the particular data to
various users we have to use the virtual table for the Base table...So that is a view.

Question: How to store directory structure in a database?


Answer: We can do it by the following command: create or replace directory as 'c: \tmp'
Question: Why does the following command give a compilation error?
DROP TABLE &TABLE_NAME;
Answer: Variable names should start with an alphabet. Here the table name starts with an '&' symbol.

Question: Difference between VARCHAR and VARCHAR2?


Answer: Varchar means fixed length character data (size) i.e., min size-1 and max-2000

Varchar2 means variable length character data i.e., min-1 to max-4000

Question: Which command displays the SQL command in the SQL buffer, and then executes it
Answer: You set the LIST or L command to get the recent one from SQL Buffer

Question: Which system table contains information on constraints on all the tables created?
Answer: USER_CONSTRAINTS.

Question: How do I write a program which will run a SQL query and mail the results to a group?
Answer: Use DBMS_JOB for scheduling a program job and DBMS_MAIL to send the results through
email.

Question: There is an Eno. & gender in a table. Eno. has primary key and gender has a check constraints
for the values 'M' and 'F'.
While inserting the data into the table M was misspelled as F and F as M.
What is the update?
Answer: update <TableName> set gender=

case where gender='F' Then 'M'

where gender='M' Then 'F'

Question: What the difference between UNION and UNIONALL?


Answer: union will return the distinct rows in two select s, while union all return all rows.

Question: How can we backup the sql files & what is SAP?
Answer: You can backup the sql files through backup utilities or some backup command in sql. SAP is
ERP software for the organization to integrate the software.

Question: What is the difference between TRUNCATE and DELETE commands?


Answer: TRUNCATE is a DDL command whereas DELETE is a DML command. Hence DELETE
operation can be rolled back, but TRUNCATE operation cannot be rolled back.
WHERE clause can be used with DELETE and not with TRUNCATE.

Question: State true or false. !=, <>, ^= all denote the same operation.
Answer: True.

Question: State true or false. EXISTS, SOME, ANY are operators in SQL.
Answer: True.

Question: What will be the output of the following query?


SELECT REPLACE (TRANSLATE (LTRIM (RTRIM ('!! ATHEN!!','!'), '!'), 'AN', '**'),'*','TROUBLE') FROM
DUAL;
Answer: TROUBLETHETROUBLE.

Question: What is the advantage to use trigger in your PL?


Answer: Triggers are fired implicitly on the tables/views on which they are created. There are various
advantages of using a trigger. Some of them are:

- Suppose we need to validate a DML statement (insert/Update/Delete) that modifies a table then we can
write a trigger on the table that gets fired implicitly whenever DML statement is executed on that table.

- Another reason of using triggers can be for automatic updation of one or more tables whenever a
DML/DDL statement is executed for the table on which the trigger is created.

- Triggers can be used to enforce constraints. For eg: Any insert/update/ Delete statements should not be
allowed on a particular table after office hours. For enforcing this constraint Triggers should be used.

- Triggers can be used to publish information about database events to subscribers. Database event can
be a system event like Database startup or shutdown or it can be a user even like User login in or user
logoff.

Question: How write a SQL statement to query the result set and display row as columns and columns as
row?
Answer: TRANSFORM Count (Roll_no) AS Count of Roll_no
SELECT Academic_Status
FROM tbl_enr_status
GROUP BY Academic_Status
PIVOT Curnt_status;

Question: Cursor Syntax brief history


Answer: To retrieve data with SQL one row at a time you need to use cursor processing. Not all relational
databases support this, but many do. Here I show this in Oracle with PL/SQL, which is Procedural
Language SQL .Cursor processing is done in several steps:1. Define the rows you want to retrieve. This
is called declaring the cursor.2. Open the cursor. This activates the cursor and loads the data. Note that
declaring the cursor doesn't load data, opening the cursor does.3. Fetch the data into variables.4. Close
the cursor.

Question: What is the data type of the surrogate key?


Answer: Data type of the surrogate key is either integer or numeric or number

Question: How to write a sql statement to find the first occurrence of a non zero value?
Answer: There is a slight chance the column "a" has a value of 0 which is not null. In that case, you?ll
loose the information. There is another way of searching the first not null value of a column:
select column_name from table_name where column_name is not null and rownum<2;

Question: What is normalazation, types with e.g.\'s. _ with queries of all types
Answer: There are 5 normal forms. It is necessary for any database to be in the third normal form to
maintain referential integrity and non-redundancy.

First Normal Form: Every field of a table (row, col) must contain an atomic value
Second Normal Form: All columns of a table must depend entirely on the primary key column.
Third Normal Form: All columns of a table must depend on all columns of a composite primary key.
Fourth Normal Form: A table must not contain two or more independent multi-valued facts. This normal
form is often avoided for maintenance reasons.
Fifth Normal Form: is about symmetric dependencies.
Each normal form assumes that the table is already in the earlier normal form.

Question: Given an unnormalized table with columns:


Answer: The query will be: delete from tabname where rowid not in (select max (rowid) from tabname
group by name) Here tabname is the table name.

Question: How to find second maximum value from a table?


Answer: select max (field1) from tname1 where field1= (select max (field1) from tname1 where
field1<(select max(field1) from tname1);

Field1- Salary field

Tname= Table name.

Question: What is the advantage of specifying WITH GRANT OPTION in the GRANT command?
Answer: The privilege receiver can further grant the privileges he/she has obtained from the owner to any
other user.

Question: What is the main difference between the IN and EXISTS clause in sub queries??
Answer: The main difference between the IN and EXISTS predicate in sub query is the way in which the
query gets executed.

IN -- The inner query is executed first and the list of values obtained as its result is used by the outer
query. The inner query is executed for only once.

EXISTS -- The first row from the outer query is selected, then the inner query is executed and, the outer
query output uses this result for checking. This process of inner query execution repeats as many no .of
times as there are outer query rows. That is, if there are ten rows that can result from outer query, the
inner query is executed that many no. of times.

Question: TRUNCATE TABLE EMP;


DELETE FROM EMP;
Will the outputs of the above two commands differ
Answer: The difference is that the TRUNCATE call cannot be rolled back and all memory space for that
table is released back to the server. TRUNCATE is much faster than DELETE and in both cases only the
table data is removed, not the table structure.

Question: What is table space?


Answer: Table-space is a physical concept. It has pages where the records of the database are stored
with a logical perception of tables. So table space contains tables.

Question: How to find out the 10th highest salary in SQL query?
Answer: Table - Tbl_Test_Salary
Column - int_salary
select max (int_salary)
from Tbl_Test_Salary
where int_salary in
(select top 10 int_Salary from Tbl_Test_Salary order by int_salary)

Question: Which command executes the contents of a specified file?


Answer: START <filename> or @<filename>.

Question: What is the difference between SQL and SQL SERVER?


Answer: SQL Server is an RDBMS just like oracle, DB2 from Microsoft
whereas
Structured Query Language (SQL), pronounced "sequel", is a language that provides an interface to
relational database systems. It was developed by IBM in the 1970s for use in System R. SQL is a de facto
standard, as well as an ISO and ANSI standard. SQL is used to perform various operations on RDBMS.

Question: What is the difference between Single row sub-Query and Scalar Sub-Query?
Answer: SINGLE ROW SUBQUERY RETURNS A VALUE WHICH IS USED BY WHERE CLAUSE,
WHEREAS SCALAR SUBQUERY IS A SELECT STATEMENT USED IN COLUMN LIST CAN BE
THOUGHT OF AS AN INLINE FUNCTION IN SELECT COLUMN LIST.

Question: What does the following query do?


Answer: SELECT SAL + NVL (COMM, 0) FROM EMP;

It gives the added value of sal and comm for each employee in the emp table.

NVL (null value) replaces null with 0.

Question: How to find second maximum value from a table?


Answer: select max (field1) from tname1 where field1= (select max (field1) from tname1 where field1<
(select max (field1) from tname1);
Field1- Salary field
Tname= Table name.

Question: I have a table with duplicate names in it. Write me a query which returns only duplicate rows
with number of times they are repeated.
Answer: SELECT COL1 FROM TAB1
WHERE COL1 IN
(SELECT MAX (COL1) FROM TAB1
GROUP BY COL1
HAVING COUNT (COL1) > 1)

Question: How to find out the database name from SQL*PLUS command prompt?
Answer: Select * from global_name;
This will give the data base name which u r currently connected to.....

Question: How to display duplicate rows in a table?


Answer: select * from emp

group by (empid)

having count (empid)>1


Question: What is the value of comm and sal after executing the following query if the initial value of ?sal?
is 10000
UPDATE EMP SET SAL = SAL + 1000, COMM = SAL*0.1;
Answer: sal = 11000, comm = 1000.

Question: 1) What is difference between Oracle and MS Access?


2) What are disadvantages in Oracle and MS Access?
2) What are features & advantages in Oracle and MS Access?
Answer: Oracle's features for distributed transactions, materialized views and replication are not available
with MS Access. These features enable Oracle to efficiently store data for multinational companies across
the globe. Also these features increase scalability of applications based on Oracle.

Oracle Technical Interview Questions Answered - Part2

By James Koopmann

This is the second part of the two part series on helping you answer those tough questions
that you might experience in your quest for an Oracle DBA position.

The Oracle Technical Interview can be quite daunting. You never quite know what to study
for and how to prepare. I am fully aware of this, as I have received many emails since my
original article on interview questions was released. While these questions are only
guidelines as to what should and more than likely will be asked, I hope that you find some
comfort in the review of them. As always, do not just memorize the answers, as there are
jewels to be found in the quest of figuring out the answer from the question. As always,
remember that as you go through the article, it is not enough to know the answer to a
particular question; you must try to put yourself in an interview situation and experience
answering the question for yourself. Therefore, after you have gone through the questions
and answers read the question again and then answer it with your own words. As always,
good luck, and cheers.

Technical - Oracle
Last time, we answered questions 1 thru 20 of the technical part of the interview. Here are
the next 30 in this section. Depending on the mood of the interview and your ability to
elaborate on the answer, try to give some insight that you know more than just the simple
answer to some of these questions. Also, be sensitive to the interviewer getting tired of you
talking too much. Well here they are.

21.  How would you determine the time zone under which a database was
operating?

select DBTIMEZONE from dual;

22.  Explain the use of setting GLOBAL_NAMES equal to TRUE.


Setting GLOBAL_NAMES dictates how you might connect to a database. This variable is
either TRUE or FALSE and if it is set to TRUE it enforces database links to have the same
name as the remote database to which they are linking.

23.  What command would you use to encrypt a PL/SQL application?

WRAP

24.  Explain the difference between a FUNCTION, PROCEDURE and PACKAGE.

A function and procedure are the same in that they are intended to be a collection of
PL/SQL code that carries a single task. While a procedure does not have to return any
values to the calling application, a function will return a single value. A package on the other
hand is a collection of functions and procedures that are grouped together based on their
commonality to a business function or application.

25.  Explain the use of table functions.

Table functions are designed to return a set of rows through PL/SQL logic but are intended
to be used as a normal table or view in a SQL statement. They are also used to pipeline
information in an ETL process.

26.  Name three advisory statistics you can collect.

Buffer Cache Advice, Segment Level Statistics, & Timed Statistics

27.  Where in the Oracle directory tree structure are audit traces placed?

In unix $ORACLE_HOME/rdbms/audit, in Windows the event viewer

28.  Explain materialized views and how they are used.

Materialized views are objects that are reduced sets of information that have been
summarized, grouped, or aggregated from base tables. They are typically used in data
warehouse or decision support systems.

29.  When a user process fails, what background process cleans up after it?

PMON

30.  What background process refreshes materialized views?

The Job Queue Processes.

31.  How would you determine what sessions are connected and what resources
they are waiting for?

Use of V$SESSION and V$SESSION_WAIT

32.  Describe what redo logs are.


Redo logs are logical and physical structures that are designed to hold all the changes made
to a database and are intended to aid in the recovery of a database.

33.  How would you force a log switch?

ALTER SYSTEM SWITCH LOGFILE;

34.  Give two methods you could use to determine what DDL changes have been
made.

You could use Logminer or Streams

35.  What does coalescing a tablespace do?

Coalescing is only valid for dictionary-managed tablespaces and de-fragments space by


combining neighboring free extents into large single extents.

36.  What is the difference between a TEMPORARY tablespace and a PERMANENT


tablespace?

A temporary tablespace is used for temporary objects such as sort structures while
permanent tablespaces are used to store those objects meant to be used as the true objects
of the database.

37.  Name a tablespace automatically created when you create a database.

The SYSTEM tablespace.

38.  When creating a user, what permissions must you grant to allow them to
connect to the database?

Grant the CONNECT to the user.

39.  How do you add a data file to a tablespace?

ALTER TABLESPACE <tablespace_name> ADD DATAFILE <datafile_name> SIZE <size>

40.  How do you resize a data file?

ALTER DATABASE DATAFILE <datafile_name> RESIZE <new_size>;

41.  What view would you use to look at the size of a data file?

DBA_DATA_FILES

42.  What view would you use to determine free space in a tablespace?

DBA_FREE_SPACE

43.  How would you determine who has added a row to a table?
Turn on fine grain auditing for the table.

44.  How can you rebuild an index?

ALTER INDEX <index_name> REBUILD;

45.  Explain what partitioning is and what its benefit is.

Partitioning is a method of taking large tables and indexes and splitting them into smaller,
more manageable pieces.

46.  You have just compiled a PL/SQL package but got errors, how would you view
the errors?

SHOW ERRORS

47.  How can you gather statistics on a table?

The ANALYZE command.

48.  How can you enable a trace for a session?

Use the DBMS_SESSION.SET_SQL_TRACE or

Use ALTER SESSION SET SQL_TRACE = TRUE;

49.  What is the difference between the SQL*Loader and IMPORT utilities?

These two Oracle utilities are used for loading data into the database. The difference is that
the import utility relies on the data being produced by another Oracle utility EXPORT while
the SQL*Loader utility allows data to be loaded that has been produced by other utilities
from different data sources just so long as it conforms to ASCII formatted or delimited files.

50.  Name two files used for network connection to a database.

TNSNAMES.ORA and SQLNET.ORA

Technical - UNIX
Every DBA should know something about the operating system that the database will be
running on. The questions here are related to UNIX but you should equally be able to
answer questions related to common Windows environments.

1.  How do you list the files in an UNIX directory while also showing hidden files?

ls -ltra

2.  How do you execute a UNIX command in the background?


Use the "&"

3.  What UNIX command will control the default file permissions when files are
created?

Umask

4.  Explain the read, write, and execute permissions on a UNIX directory.

Read allows you to see and list the directory contents.

Write allows you to create, edit and delete files and subdirectories in the directory.

Execute gives you the previous read/write permissions plus allows you to change into the
directory and execute programs or shells from the directory.

5.  the difference between a soft link and a hard link?

A symbolic (soft) linked file and the targeted file can be located on the same or different file
system while for a hard link they must be located on the same file system.

6.  Give the command to display space usage on the UNIX file system.

df -lk

7.  Explain iostat, vmstat and netstat.

Iostat reports on terminal, disk and tape I/O activity.

Vmstat reports on virtual memory statistics for processes, disk, tape and CPU activity.

Netstat reports on the contents of network data structures.

8.  How would you change all occurrences of a value using VI?

Use :%s/<old>/<new>/g

9.  Give two UNIX kernel parameters that effect an Oracle install

SHMMAX & SHMMNI

10.  Briefly, how do you install Oracle software on UNIX.

Basically, set up disks, kernel parameters, and run orainst.

I hope that these interview questions were not too hard. Remember these are "core" DBA
questions and not necessarily related to the Oracle options that you may encounter in some
interviews. Take a close look at the requirements for any job and try to extract questions
that interviewers may ask from manuals and real life experiences. For instance, if they are
looking for a DBA to run their databases in RAC environments, you should try to determine
what hardware and software they are using BEFORE you get to the interview. This would
allow you to brush up on particular environments and not be caught off-guard. Good luck!

You might also like