Oracle Database

  Home  Oracle  Oracle Database


“Learn Oracle database programming with hundreds of Interview Questions and Answers and examples and get a better job as an Oracle DBA with this basic and advance Oracle Database Interview Questions and Answers guide”



430 Oracle Database Questions And Answers

141⟩ How To Write a Query with a Right Outer Join in Oracle?

If you want to query from two tables with a right outer join, you can use the RIGHT OUTER JOIN ... ON clause in the FROM clause. The following query returns output with a right outer join from two tables: departments and employees. The join condition is that the manager ID in the departments table equals to the employee ID in the employees table:

SQL> set NULL 'NULL'

SQL> SELECT d.department_name, e.first_name, e.last_name

2 FROM departments d RIGHT OUTER JOIN employees e

3 ON d.manager_id = e.employee_id;

DEPARTMENT_NAME   FIRST_NAME   LAST_NAME

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

Administration Jennifer Whalen

Marketing Michael Hartstein

Purchasing Den Raphaely

Human Resources Susan Mavris

Shipping Adam Fripp

IT Alexander Hunold

......

NULL Clara Vishney

NULL Jason Mallin

NULL Hazel Philtanker

NULL Nanette Cambrault

NULL Alana Walsh

NULL Karen Partners

NULL Bruce

 155 views

142⟩ How To Write a Query with a Full Outer Join in Oracle?

If you want to query from two tables with a full outer join, you can use the FULL OUTER JOIN ... ON clause in the FROM clause. The following query returns output with a full outer join from two tables: departments and employees. The join condition is that the manager ID in the departments table equals to the employee ID in the employees table:

SQL> set NULL 'NULL'

SQL> SELECT d.department_name, e.first_name, e.last_name

2 FROM departments d FULL OUTER JOIN employees e

3 ON d.manager_id = e.employee_id;

 138 views

143⟩ How To Write an Inner Join with the WHERE Clause in Oracle?

If you don't want to use the INNER JOIN ... ON clause to write an inner join, you can put the join condition in the WHERE clause as shown in the following query example:

SQL> SELECT d.department_name, e.first_name, e.last_name

2 FROM departments d, employees e

3 WHERE d.manager_id = e.employee_id;

DEPARTMENT_NAME   FIRST_NAME   LAST_NAME

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

Administration Jennifer Whalen

Marketing Michael Hartstein

Purchasing Den Raphaely

Human Resources Susan Mavris

Shipping Adam Fripp

IT Alexander Hunold

......

 136 views

144⟩ How To Write a Left Outer Join with the WHERE Clause in Oracle?

If you don't want to use the LEFT OUTER JOIN ... ON clause to write a left outer join, you can use a special criteria in the WHERE clause as "left_table.column = right_table.column(+)". The select statement below is an example of a left outer join written with the WHERE clause:

SQL> set NULL 'NULL'

SQL> SELECT d.department_name, e.first_name, e.last_name

2 FROM departments d, employees e

3 WHERE d.manager_id = e.employee_id(+);

DEPARTMENT_NAME   FIRST_NAME   LAST_NAME

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

Administration Jennifer Whalen

Marketing Michael Hartstein

Purchasing Den Raphaely

Human Resources Susan Mavris

Shipping Adam Fripp

IT Alexander Hunold

......

Treasury NULL NULL

Corporate Tax NULL NULL

Control And Credit NULL NULL

Shareholder Services NULL NULL

Benefits NULL NULL

Manufacturing NULL NULL

......

 153 views

145⟩ How To Name Query Output Columns in Oracle?

Each column in the query output has a default name. If you don't like the default name, you can specify a new name for any column in the query output by using the AS clause. The following statement shows you a good example:

SQL> SELECT department_id AS ID, MIN(salary) AS Low,

2 MAX(salary) AS High, AVG(salary) AS Average

3 FROM employees GROUP BY department_id

4 HAVING AVG(salary) < 5000;

  ID  LOW  HIGH AVERAGE

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

30 2500 11000 4150

50 2100 8200 3475.55556

10 4400 4400 4400

 151 views

146⟩ What Is a Subquery in Oracle?

A subquery is a SELECT statement used as part of the selection criteria of the main SELECT statement. The subquery specified in the WHERE clause will be evaluated repeated on each row of the selection base table. The output of the subquery will be used in the final evaluation of the criteria. Usually, subqueries are used in the following Boolean operations:

* "expression IN (subquery)"

* "expression NOT IN (subquery)"

* "EXISTS (subquery)"

* "NOT EXISTS (subquery)"

 134 views

147⟩ How To Use Subqueries with the IN Operator using Oracle?

A subquery can be used with the IN operator as "expression IN (subquery)". The subquery should return a single column with one or more rows to form a list of values to be used by the IN operation. The following tutorial exercise shows you how to use a subquery with the IN operator:

SQL> SELECT first_name, last_name FROM employees

2 WHERE department_id IN (

3 SELECT department_id FROM departments

4 WHERE location_id = 1700

5 );

FIRST_NAME   LAST_NAME

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

Steven King

Neena Kochhar

Lex De Haan

Nancy Greenberg

Daniel Faviet

John Chen

Ismael Sciarra

......

 143 views

148⟩ How To Use Subqueries with the EXISTS Operator in Oracle?

A subquery can be used with the EXISTS operator as "EXISTS (subquery)", which returns true if the subquery returns one or more rows. The following statement is a good example of "EXISTS (subquery)". It returns rows from employees table that there are rows existing in the departments table linked to the employees table with location_id = 1700.

SQL> SELECT first_name, last_name FROM employees e

2 WHERE EXISTS (

3 SELECT * FROM departments d

4 WHERE e.department_id = d.department_id

5 AND d.location_id = 1700

6 );

FIRST_NAME   LAST_NAME

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

Steven King

Neena Kochhar

Lex De Haan

Nancy Greenberg

Daniel Faviet

John Chen

Ismael Sciarra

......

 140 views

149⟩ How To Use Subqueries in the FROM clause in Oracle?

If you have a query returning many rows of data, and you want to perform another query on those rows, you can put the first query as a subquery in the FROM clause of the second query. The following statement shows you how to use a subquery as base table for the main query:

SQL> SELECT * FROM (

2 SELECT first_name, last_name, department_name

3 FROM employees e, departments d

4 WHERE e.department_id = d.department_id

5 ) WHERE department_name LIKE 'S%' ORDER BY last_name;

FIRST_NAME  LAST_NAME   DEPARTMENT_NAME

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

Ellen Abel Sales

Sundar Ande Sales

Mozhe Atkinson Shipping

Amit Banda Sales

Elizabeth Bates Sales

Sarah Bell Shipping

 133 views

150⟩ How To Start the Command-Line SQL*Plus?

f you Oracle server or client installed on your windows system, you can start the command-line SQL*Plus in two ways:

1. Click Start > All Programs > Oracle ... > Start SQL Command Line. The SQL*Plus command window will show up with a message like this:

SQL*Plus: Release 10.2.0.1.0 - Production on Tue ...

Copyright (c) 1982, 2005, Oracle. All rights reserved.

SQL>

2. Click Start > Run..., enter "cmd" and click OK. A Windows command window will show up. You can then use Windows commands to start the command-line SQL*Plus as shown in the tutorial exercise below:

>cd c:oraclexeapporacleproduct10.2.0server

>.insqlplus /nolog

SQL*Plus: Release 10.2.0.1.0 - Production on Tue ...

Copyright (c) 1982, 2005, Oracle. All rights reserved.

 138 views

151⟩ How To Count Groups Returned with the GROUP BY Clause in Oracle?

If you use the COUNT(*) function on groups returned with the GROUP BY clause, it will count the number of rows within each group, not the number of groups. If you want to count the number of groups, you can put the GROUP BY query into a subquery and apply the COUNT(*) function on the main query as shown in the following tutorial exercise:

SQL> SELECT first_name, COUNT(*) FROM employees

GROUP BY first_name HAVING COUNT(*) > 1;

FIRST_NAME   COUNT(*)

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

Peter 3

Michael 2

Steven 2

John 3

Julia 2

William 2

Karen 2

Kevin 2

......

SQL> SELECT COUNT(*) FROM (

SELECT first_name, COUNT(*) FROM employees

GROUP BY first_name HAVING COUNT(*) > 1

);

COUNT(*)

----------

13

 153 views

152⟩ How To Return Top 5 Rows in Oracle?

If you want the query to return only the first 5 rows, you can use the pseudo column called ROWNUM in the WHERE clause. ROWNUM contains the row number of each returning row from the query. The following statement returns the first 5 rows from the employees table:

SQL> SELECT employee_id, first_name, last_name

FROM employees WHERE ROWNUM <= 5;

EMPLOYEE_ID FIRST_NAME   LAST_NAME

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

100 Steven King

101 Neena Kochhar

102 Lex De Haan

103 Alexander Hunold

104 Bruce Ernst

 210 views

153⟩ What Is an Oracle Transaction?

A transaction is a logical unit of work requested by a user to be applied to the database objects. Oracle server introduces the transaction concept to allow users to group one or more SQL statements into a single transaction, so that the effects of all the SQL statements in a transaction can be either all committed (applied to the database) or all rolled back (undone from the database).

 138 views

154⟩ How To Start a New Transaction in Oracle?

There is no SQL statement to explicitly start a new transaction. Oracle server implicitly starts a new transaction with the following two conditions:

* The first executable statement of a new user session will automatically start a new transaction.

* The first executable statement after a previous transaction has been ended will automatically start a new transaction.

 151 views

155⟩ How To Get Help at the SQL Prompt?

Once SQL*Plus is started, you will get a SQL prompt like this: SQL>. This where you can enter commands for SQL*Plus to run.

To get help information at the SQL prompt, you can use the HELP command as shown in the following tutorial example:

SQL> HELP INDEX

Enter Help [topic] for help.

 @   COPY  PAUSE  SHUTDOWN

@@ DEFINE PRINT SPOOL

/ DEL PROMPT SQLPLUS

ACCEPT DESCRIBE QUIT START

APPEND DISCONNECT RECOVER STARTUP

ARCHIVE LOG EDIT REMARK STORE

ATTRIBUTE EXECUTE REPFOOTER TIMING

BREAK EXIT REPHEADER TTITLE

...

COMPUTE LIST SET XQUERY

CONNECT PASSWORD SHOW

SQL> HELP CONNECT

CONNECT

-------

 170 views

156⟩ How To End the Current Transaction in Oracle?

There are several ways the current transaction can be ended:

* Running the COMMIT statement will explicitly end the current transaction.

* Running the ROLLBACK statement will explicitly end the current transaction.

* Running any DDL statement will implicitly end the current transaction.

* Disconnecting a user session will implicitly end the current transaction.

* Killing a user session will implicitly end the current transaction.

 135 views

157⟩ How To Create an Oracle Testing Table?

If you want to practice DML statements, you should create a testing table as shown in the script below:

>cd (OracleXE home directory)

>.insqlplus /nolog

SQL> connect HR/globalguideline

Connected.

SQL> CREATE TABLE ggl_links (id NUMBER(4) PRIMARY KEY,

url VARCHAR2(16) NOT NULL,

notes VARCHAR2(16),

counts NUMBER(4),

created DATE DEFAULT (sysdate));

Table created.

You should keep this table for to practice other tutorial exercises presented in this collection.

 131 views

158⟩ How To Commit the Current Transaction in Oracle?

If you have used some DML statements updated some data objects, and you want to have the updates to be permanently recorded in the database, you can use the COMMIT statement. It will make all the database changes made in the current transaction become permanent and end the current transaction. The following tutorial exercise shows you how to use COMMIT statements:

SQL> connect HR/globalguideline

SQL> INSERT INTO ggl_links (url, id)

2 VALUES ('rendc.org', 101);

SQL> INSERT INTO ggl_links (url, id)

2 VALUES ('rendc.org/html', 110);

SQL> SELECT * FROM ggl_links;

  ID URL   NOTES  COUNTS CREATED

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

101 rendc.org 07-MAY-06

110 rendc.org/html 07-MAY-06

SQL> COMMIT;

Commit complete.

 131 views

159⟩ How To Rollback the Current Transaction in Oracle?

If you have used some DML statements updated some data objects, you find a problem with those updates, and you don't want those updates to be permanently recorded in the database, you can use the ROLLBACK statement. It will remove all the database changes made in the current transaction and end the current transaction. The following tutorial exercise shows you how to use ROLLBACK statements:

SQL> connect HR/globalguideline

SQL> INSERT INTO ggl_links (url, id)

2 VALUES ('google.com', 102);

SQL> INSERT INTO ggl_links (url, id)

3 VALUES ('myspace.com', 103);

SQL> SELECT * FROM ggl_links;

ID URL NOTES COUNTS CREATED

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

101 rendc.org 07-MAY-06

110 rendc.org/html 07-MAY-06

102 google.com 07-MAY-06

103 myspace.com 07-MAY-06

 140 views

160⟩ What Information Is Needed to Connect SQL*Plus an Oracle Server?

If you want to connect your SQL*Plus session to an Oracle server, you need to know the following information about this server:

* The network hostname, or IP address, of the Oracle server.

* The network port number where the Oracle server is listening for incoming connections.

* The name of the target database instance managed by the Oracle server.

* The name of your user account predefined on in the target database instance.

* The password of your user account predefined on in the target database instance.

 151 views