Oracle Practice 2
Oracle Practice 2
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;
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.
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),
Null?
Type
DEPTNO
DNAME
LOC
NOT NULL
NUMBER(2)
VARCHAR2(14)
VARCHAR2(13)
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)
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);
BCD
A
B
A
AD
C
AE
BCE
D
B
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.