Sunteți pe pagina 1din 13

Oracle Interview Questions and Answers : SQL

1.
To see current user name
Sql> show user;
2.
Change SQL prompt name
SQL> set sqlprompt Manimara >
Manimara >
Manimara >
3.
Switch to DOS prompt
SQL> host
4.
How do I eliminate the duplicate rows ?
SQL> delete from table_name where rowid not in (select max(rowid) from table group by
duplicate_values_field_name);
or
SQL> delete duplicate_values_field_name dv from table_name ta where rowid <(select min(rowid) from
table_name tb where ta.dv=tb.dv);
Example.
Table Emp
Empno Ename
101
Scott
102
Jiyo
103
Millor
104
Jiyo
105
Smith
delete ename from emp a where rowid < ( select min(rowid) from emp b where a.ename = b.ename);
The output like,
Empno Ename
101
Scott
102
Millor
103
Jiyo
104
Smith
5.
How do I display row number with records?
To achive this use rownum pseudocolumn with query, like SQL> SQL> select rownum, ename from emp;
Output:
1
Scott
2
Millor
3
Jiyo
4
Smith
6.
Display the records between two range
select rownum, empno, ename from emp where rowid in
(select rowid from emp where rownum <=&upto
minus
select rowid from emp where rownum<&Start);
Enter value for upto: 10
Enter value for Start: 7
ROWNUM EMPNO ENAME
--------- --------- ---------1
7782 CLARK

2
3
4

7788 SCOTT
7839 KING
7844 TURNER

7.
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?
SQL> select nvl(to_char(comm.),'NA') from emp;
Output :
NVL(TO_CHAR(COMM),'NA')
----------------------NA
300
500
NA
1400
NA
NA
8.
Oracle cursor : Implicit & Explicit cursors
Oracle uses work areas called private SQL areas to create SQL statements.
PL/SQL construct to identify each and every work are used, is called as Cursor.
For SQL queries returning a single row, PL/SQL declares all implicit cursors.
For queries that returning more than one row, the cursor needs to be explicitly declared.
9.
Explicit Cursor attributes
There are four cursor attributes used in Oracle
cursor_name%Found, cursor_name%NOTFOUND, cursor_name%ROWCOUNT,
cursor_name%ISOPEN
10. Implicit Cursor attributes
Same as explicit cursor but prefixed by the word SQL
SQL%Found, SQL%NOTFOUND, SQL%ROWCOUNT, SQL%ISOPEN
Tips : 1. Here SQL%ISOPEN is false, because oracle automatically closed the implicit cursor after
executing SQL statements.
: 2. All are Boolean attributes.
11. Find out nth highest salary from emp table
SELECT DISTINCT (a.sal) FROM EMP A WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM
EMP B WHERE a.sal<=b.sal);
Enter value for n: 2
SAL
--------3700
12. To view installed Oracle version information
SQL> select banner from v$version;

13. Display the number value in Words


SQL> select sal, (to_char(to_date(sal,'j'), 'jsp'))
from emp;
the output like,
SAL (TO_CHAR(TO_DATE(SAL,'J'),'JSP'))
--------- ----------------------------------------------------800 eight hundred
1600 one thousand six hundred
1250 one thousand two hundred fifty
If you want to add some text like,
Rs. Three Thousand only.
SQL> select sal "Salary ",
(' Rs. '|| (to_char(to_date(sal,'j'), 'Jsp'))|| ' only.'))
"Sal in Words" from emp
/
Salary Sal in Words
------- -----------------------------------------------------800 Rs. Eight Hundred only.
1600 Rs. One Thousand Six Hundred only.
1250 Rs. One Thousand Two Hundred Fifty only.
14. 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
15. Which date function returns number value?
months_between
16. Any three PL/SQL Exceptions?
Too_many_rows, No_Data_Found, Value_Error, Zero_Error, Others
17. What are PL/SQL Cursor Exceptions?
Cursor_Already_Open, Invalid_Cursor
18. Other way to replace query result null value with a text
SQL> Set NULL N/A
to reset SQL> Set NULL
19. What are the more common pseudo-columns?
SYSDATE, USER , UID, CURVAL, NEXTVAL, ROWID, ROWNUM
20. What is the output of SIGN function?
1 for positive value,
0 for Zero,
-1 for Negative value.

21. What is the maximum number of triggers, can apply to a single table?
12 triggers.
Oracle Database SQL Expert
Sample Questions
View answers below
Sample questions are provided solely to familiarize candidates with the multiple-choice format and
writing style of questions that will be found on the exam. Sample questions may not cover the full
spectrum of difficulty that is covered by the exam questions. Success on the sample questions does
not predict success on the exam.
1. View the Exhibit and examine the structure of the EMP and SALGRADE tables. You want to
display the names of all employees whose salaries belong to GRADE 5. Which SQL statements
give the required output? (Choose all that apply)
A. SELECT ename
FROM emp JOIN salgrade
USING (sal BETWEEN losal AND hisal) AND grade = 5;
B. SELECT ename
FROM emp e JOIN salgrade s
ON (e.sal BETWEEN s.losal AND s.hisal AND s.grade = 5);
C. SELECT ename
FROM emp e JOIN salgrade s
ON (e.sal BETWEEN s.losal AND s.hisal) AND s.grade = 5;
D. SELECT ename
FROM emp e JOIN salgrade s
ON (e.sal BETWEEN s.losal AND s.hisal) WHERE s.grade=5;
E. SELECT ename
FROM emp e JOIN salgrade s
WHERE e.sal BETWEEN s.losal AND s.hisal AND s.grade = 5;
1a "Exhibit"
EMP
Name

Null?

Type

EMPNO
ENAME
JOB
HIREDATE
SAL
DEPTNO

NOT NULL

NUMBER(4)
VARCHAR2(10)
VARCHAR2(9)
DATE
NUMBER(7,2)
NUMBER(2)

Null?

Type

SALGRADE
Name
GRADE
LOSAL
HISAL

NUMBER
NUMBER
NUMBER

2. View the Exhibit and examine the structure of the DEPARTMENTS and LOCATIONS tables. You
want to display all the cities and the corresponding departments in them, if any. Which query
would give you the required output?
A. SELECT location_id LOC, city, department_id DEPT
FROM locations LEFT OUTER JOIN departments
USING (location_id);
B. SELECT location_id LOC, city, department_id DEPT
FROM locations RIGHT OUTER JOIN departments
USING (location_id);
C. SELECT l.location_id LOC, l.city, d.department_id DEPT
FROM locations l LEFT OUTER JOIN departments d
USING (location_id);
D. SELECT l.location_id LOC, l.city, d.department_id DEPT
FROM locations l FULL OUTER JOIN departments d
USING (location_id);
2a "Exhibit"
DEPARTMENTS
Name

Null?

Type

DEPARTMENT_ID
DEPARTMENT_NAME
MANAGER_ID
LOCATION_ID

NOT NULL
NOT NULL

NUMBER(4)
VARCHAR2(30)
NUMBER(6)
NUMBER(4)

Null?

Type

LOCATIONS
Name
LOCATION_ID
STREET_ADDRESS
POSTAL_CODE
CITY
STATE_PROVINCE
COUNTRY_ID

NOT NULL

NOT NULL

NUMBER(4)
VARCHAR2(40)
VARCHAR2(12)
VARCHAR2(30)
VARCHAR2(25)
CHAR(2)

3. View the Exhibit and examine the structure of the EMPLOYEES and DEPARTMENTS tables.
You want to display the last names and hire dates of all latest hires in their respective
departments in the location ID 1700. You issue the following query:
SQL>SELECT last_name, hire_date
FROM employees
WHERE (department_id, hire_date) IN
(SELECT department_id, MAX(hire_date)
FROM employees JOIN departments
USING(department_id)
WHERE location_id = 1700
GROUP BY department_id);
What is the outcome?

A.
B.
C.
D.

It executes but does not give the correct result


It executes successfully and gives the correct result
It generates an error because of the pairwise comparison
It generates an error because the GROUP BY clause cannot be used with table joins in a
subquery
3a "Exhibit"
EMPLOYEES
Name

Null?

Type

EMPLOYEE_ID
FIRST_NAME
LAST_NAME
HIRE_DATE
JOB_ID
SALARY
DEPARTMENT_ID

NOT NULL
NOT NULL
NOT NULL
NOT NULL

NUMBER(6)
VARCHAR2(20)
VARCHAR2(25)
DATE
VARCHAR2(10)
NUMBER(8, 2)
NUMBER(4)

Name

Null?

Type

DEPARTMENT_ID
DEPARTMENT_NAME
MANAGER_ID
LOCATION_ID

NOT NULL
NOT NULL

NUMBER(4)
VARCHAR2(30)
NUMBER(6)
NUMBER(4)

DEPARTMENTS

4. View the Exhibit and examine the structure of the LOCATIONS and DEPARTMENTS tables. You
need to display all those cities that have only one department. Which query gives the correct
output?
A. SELECT location_id, city
FROM locations l
WHERE 1 = (SELECT COUNT(*)
FROM departments
WHERE location_id = l.location_id);
B. SELECT location_id, city
FROM locations WHERE EXISTS (SELECT COUNT(*)
FROM departments
GROUP BY location_id HAVING COUNT(*) = 1);
C. SELECT location_id, city
FROM locations WHERE
1 = (SELECT COUNT(*) FROM departments
GROUP BY location_id);
D. SELECT l.location_id, city
FROM locations l JOIN departments d ON (l.location_id = d.location_id)
WHERE EXISTS (SELECT COUNT(*)
FROM departments d
WHERE l.location_id =d.location_id);

E. 4a "Exhibit"
LOCATIONS
Name

Null?

Type

LOCATION_ID
STREET_ADDRESS
POSTAL_CODE
CITY
STATE_PROVINCE
COUNTRY_ID

NOT NULL

NUMBER(4)
VARCHAR2(40)
VARCHAR2(12)
VARCHAR2(30)
VARCHAR2(25)
CHAR(2)

Name

Null?

Type

DEPARTMENT_ID
DEPARTMENT_NAME
MANAGER_ID
LOCATION_ID

NOT NULL
NOT NULL

NUMBER(4)
VARCHAR2(30)
NUMBER(6)
NUMBER(4)

DEPARTMENTS

5. View the Exhibit and examine the structure of the EMP table. You want to display the names and
salaries of only those employees who earn the highest salaries in their departments. Which two
SQL statements give the required output? (Choose two.)
A. SELECT ename, sal
FROM emp e
WHERE sal = (SELECT MAX(sal)
FROM emp
WHERE deptno = e.deptno);
B. SELECT ename, sal
FROM emp
WHERE sal = ALL (SELECT MAX(sal)
FROM emp
GROUP BY deptno);
C. SELECT ename, sal
FROM emp e
WHERE EXISTS (SELECT MAX(sal)
FROM emp WHERE deptno = e.deptno);
D. SELECT ename, sal
FROM emp
NATURAL JOIN (SELECT deptno, MAX(sal) sal
FROM emp
GROUP BY deptno);
6. Evaluate the following SQL statement:
(Note that the numbers 2,3 etc in the SQL statement are line numbers and not part of the syntax)
SQL> CREATE TABLE product
2 (prod_id NUMBER(3),
3 prod_name VARCHAR2(25),
4 qty NUMBER(7,2),
5 price NUMBER(10,2),

6 CONSTRAINT prod_id_pk PRIMARY KEY(prod_id),


7 CONSTRAINT prod_name_uq UNIQUE (prod_name),
8 CONSTRAINT price_nn NOT NULL (price));
What is the outcome of executing this command?
A.
B.
C.
D.

It generates an error at line 6.


It generates an error at line 7.
It generates an error at line 8.
It executes successfully and creates the PRODUCTS table.

7. Examine the structure of the DEPT table:


Name

Null?

Type

DEPTNO
DNAME
LOC

NOT NULL

NUMBER(2)
VARCHAR2(14)
VARCHAR2(13)

You successfully execute the following SQL statement:


SQL>CREATE TABLE emp
(emp_no NUMBER(3) PRIMARY KEY,
emp_name VARCHAR2(25) UNIQUE,
job_id VARCHAR2(10) NOT NULL,
deptno NUMBER(2) REFERENCES dept(deptno),
salary NUMBER(10,2) CHECK (salary > 0));
For which columns would an index be generated automatically? (Choose all that apply)
A.
B.
C.
D.
E.

EMP_NO
SALARY
JOB_ID
DEPT_NO
EMP_NAME

8. View the Exhibit and examine the structure of the EMP table belonging to the user SCOTT. The
EMP table contains the details of all the current employees in your organization.
EMPNO is the PRIMARY KEY.
User SCOTT has created an ENAME_IDX index on the ENAME column and an EMP_VW view that
displays the ENAME and SALARY columns.
The recyclebin is enabled in the database. SCOTT executes the following command:
SQL> DROP TABLE emp;
Which details would be stored in the recycle bin? (Choose all that apply)
A.
B.
C.
D.
E.

EMP_VW
ENAME_IDX
The PRIMARY KEY constraint
Only the structure of the EMP table
Structure and data of the EMP table
8a "Exhibit

EMP
Name

Null?

Type

EMPNO
ENAME
HIREDATE
SAL
DEPTNO

NOT NULL

NUMBER(4)
VARCHAR2(10)
DATE
NUMBER(7,2)
NUMBER(2)

9. Examine the data in the DOCNO column of the DOC_DETAILS table:

DOCNO
123-456-7890
233-67-90876
45-789-23456
You need to extract the digits between the hyphens as follows:

SUBSTR
456
67
789
Which SQL statement gives the required result?
A. SELECT REGEXP_SUBSTR(docno,'-[^-]+') "SUBSTR" FROM doc_details;
B. SELECT REGEXP_SUBSTR(docno,'^-[^-]+-')"SUBSTR"
FROM doc_details;
C. SELECT REGEXP_SUBSTR(docno,'-[^-]+',2) "SUBSTR"
FROM doc_details;
D. SELECT REGEXP_SUBSTR(docno, '[^-]+',1,2) "SUBSTR"
FROM doc_details;
10. View the Exhibit and examine a sample of the data existing in the STORES table.
You need to generate a report that shows the following details:
1) The total QTY_SOLD of each product in each region of each country.
2) The total QTY_SOLD of all products in each region of each country.
3) The total QTY_SOLD of all products in each country.
Which SQL statement gives the required output?
A. SELECT country_id, region, prod_no, SUM(qty_sold)
FROM stores
GROUP BY CUBE(country_id,region,prod_no);
B. SELECT country_id, region, prod_no, SUM(qty_sold)
FROM stores
GROUP BY ROLLUP(country_id,region,prod_no);

C. SELECT country, region, prod_no, SUM(qty_sold)


FROM stores
GROUP BY GROUPING SETS(country_id,region),prod_no);
D. SELECT country, region, prod_no, SUM(qty_sold), GROUPING(country_id) G1,
GROUPING(region) G2, GROUPING(prod_no)
FROM stores
GROUP BY CUBE (country_id,region,prod_no);
Answers
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

BCD
A
B
A
AD
C
AE
BCE
D
B

1) What is oracle database?


Oracle Database is a relational database management system (RDBMS) which is used to store and
retrieve the large amounts of data. Oracle Database had physical and logical structures. Logical
structures and physical structures are separated from each other

2) What is schema?
A user account and its associated data including tables, views, indexes, clusters, sequences,procedures,
functions, triggers,packages and database links is known as Oracle schema. System, SCOTT etc are
default schema's. We can create a new Schema/User. But we can't drop default database schema's.

3) What is a Tablespace?
Oracle use Tablespace for logical data Storage. Physically, data will get stored in Datafiles. Datafiles will
be connected to tablespace. A tablespace can have multiple datafiles. A tablespace can have objects
from different schema's and a schema can have multiple tablespace's. Database creates "SYSTEM
tablespace" by default during database creation. It contains read only data dictionary tables which
contains the information about the database.

Also Read Basic to Advanced Oracle SQL Query Interview Question and Answers
4) What is a Control File ?
Control file is a binary file which stores Database name, associated data files, redo files, DB creation time
and current log sequence number. Without control file database cannot be started and can hamper data
recovery.

5) Define data blocks ?


Data Blocks are the base unit of logical database space. Each data block represents a specific number of
bytes of database space on a disk
6) What is an Extent ?
Extent is a collection of Continuous data blocks, which is used for storing a specific type of information.
Are you looking for frequently asked Database Interview Questions and Answers? Click here
7) What is a Segment ?
A segment is a collection of extends which is used for storing a specific data structure and resides in the
same tablespace.

8) What is Rollback Segment ?


Database contain one or more Rollback Segments to roll back transactions and data recovery.
9) What are the different type of Segments ?
Data Segment(for storing User Data), Index Segment (for storing index), Rollback Segment and
Temporary Segment.
10) What is a Redo Log ?
Redo Log files is a collection of 2 or more pre-allocated files, which is used in data recovery. Whenever a
change is made to the database, change info gets stored in redo files. In case of a database crash, we
can used redo files for data recovery.
11) What is a table Cluster ?
Table Cluster is a group of related tables that share common columns are store related data in the same
block.
12) What is a cluster Key ?
The common column or group of columns associated with the clustered tables is called cluster Key.
Advantage of using cluster key is that the common columns will be stored only once.
13) What is a synonym?
Synonym is the alias name for a table, view, sequence or program unit.

14) What are the two types of Synonyms?


Two types of Synonyms are Private and Public. A private synonym can be accessed by its owner only,
where as the public synonym can be accesses by any DB user.
15) What is System Global Area (SGA) ?
The System Global Area (SGA) is a part of system memory which is allocated to all process belonging to
oracle instance. We can allocate memory to SGA by modifying Oracle initialization parameters like
shared_pool_size, include db_cache_size and log_buffer.
16) What is a shared pool?
Shared pool is one of the most important part of SGA. Shared pool is used by oracle to handle identical
queries, which enables it to execute only once thus by improving performance. Shared Pool depends on
db_cache_size parameter.

17) What is Program Global Area (PGA)?


Program Global Area is the non shared memory used by oracle that contain data and control information
of server process.

18) What is dictionary cache ?


Oracle Data directory contains meta data about the tables owned by SYSTEM and SYS schema's. Proper
sizing of data directory cache allows fast retrieval of data from data dictionary.

19) What is Database Buffer Cache ?


Database buffer cache is used by SGA to hold blocks of data read from data files. Each buffer can hold
one database block.

20) What is a cursor ?


When a DML statements like INSERT, UPDATE, DELETE, or MERGE is executed or when SELECT
query is executed, the information (statement and the rows of data accessed by it) about the same will be
stored in private SQL area. Cursor is a pointer to this private SQL area.

21) Explain the two type of Cursors ?


Two types of cursors are Implicit Cursor and Explicit Cursor. Implicit Cursors are created when SELECT
which returns one row, INSERT, UPDATE and DELETE statements are executed. Explicit Cursors are
user defined cursors which get created when SELECT statement return more than one row.

22) What is a Query Record Group?


A query record group is a record group that has an associated SELECT statement. Columns in query
record group derive their default names, data types, had lengths from the database columns referenced
in the SELECT statement. Records in query record group are the rows retrieved by the query associated
with that record group.

23) What is row chaining?


When sizes of a row exceed size of data block, data for the row is stored in a chain of data block reserved
for that segment. This is called row chaining

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