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

121⟩ How To Sort the Query Output in Oracle?

If you want the returning rows to be sorted, you can specify a sorting expression in the ORDER BY clause. The following select statement returns rows sorted by the values in the "manager_id" column:

SQL> SELECT * FROM departments ORDER BY manager_id;

DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID

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

90 Executive 100 1700

60 IT 103 1400

100 Finance 108 1700

30 Purchasing 114 1700

50 Shipping 121 1500

80 Sales 145 2500

10 Administration 200 1700

20 Marketing 201 1800

......

 146 views

122⟩ What To Do If the StartBD.bat Failed to Start the XE Instance?

If StartBD.bat failed to start the XE instance, you need to try to start the instance with other approaches to get detail error messages on why the instance can not be started.

One good approach to start the default instance is to use SQL*Plus. Here is how to use SQL*Plus to start the default instance in a command window:

>cd (OracleXE home directory)

>.instartdb

>.insqlplus

Enter user-name: SYSTEM

Enter password: globalguideline

ERROR:

ORA-01034: ORACLE not available

ORA-27101: shared memory realm does not exist

The first "cd" is to move the current directory the 10g XE home directory. The second command ".instartdb" is to make sure the TNS listener is running. The third command ".insqlplus" launches SQL*Plus. The error message "ORA-27101" tells you that there is a memory problem with the default instance.

So you can not use the normal login process to the server without a good instance. See other tips on how to log into a server without any instance.

 126 views

123⟩ Can the Query Output Be Sorted by Multiple Columns in Oracle?

You can specifying multiple columns in the ORDER BY clause as shown in the following example statement, which returns employees' salaries sorted by department and salary value:

SQL> SELECT department_id, first_name, last_name, salary

FROM employees ORDER BY department_id, salary;

DEPARTMENT_ID FIRST_NAME LAST_NAME   SALARY

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

10 Jennifer Whalen 4400

20 Pat Fay 6000

20 Michael Hartstein 13000

30 Karen Colmenares 2500

30 Guy Himuro 2600

30 Sigal Tobias 2800

30 Shelli Baida 2900

30 Alexander Khoo 3100

30 Den Raphaely 11000

40 Susan Mavris 6500

50 TJ Olson 2100

......

 132 views

124⟩ How To Sort Output in Descending Order in Oracle?

If you want to sort a column in descending order, you can specify the DESC keyword in the ORDER BY clause. The following SELECT statement first sorts the department in descending order, then sorts the salary in ascending order:

SQL> SELECT department_id, first_name, last_name, salary

FROM employees ORDER BY department_id DESC, salary;

DEPARTMENT_ID FIRST_NAME LAST_NAME   SALARY

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

Kimberely Grant 7000

110 William Gietz 8300

110 Shelley Higgins 12000

100 Luis Popp 6900

100 Ismael Sciarra 7700

100 Jose Manuel Urman 7800

100 John Chen 8200

100 Daniel Faviet 9000

......

 151 views

125⟩ How To Use SELECT Statement to Count the Number of Rows in Oracle?

If you want to count the number of rows, you can use the COUNT(*) function in the SELECT clause. The following select statement returns the number of rows in the "department" table:

SQL> SELECT COUNT(*) FROM departments;

COUNT(*)

----------

27

So there are 27 rows in the "departments" table.

 155 views

126⟩ Can SELECT Statements Be Used on Views in Oracle?

Select (query) statements can used on views in the same way as tables. The following tutorial exercise helps you creating a view and running a query statement on the view:

SQL> CREATE VIEW managed_dept AS

SELECT * FROM departments WHERE manager_id IS NOT NULL;

View created.

SQL> SELECT * FROM managed_dept WHERE location_id = 1700;

DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID

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

10 Administration 200 1700

30 Purchasing 114 1700

90 Executive 100 1700

100 Finance 108 1700

110 Accounting 205 1700

 136 views

127⟩ How To Filter Out Duplications in the Returning Rows using Oracle?

If there are duplications in the returning rows, and you want to remove the duplications, you can use the keyword DISTINCT or UNIQUE in the SELECT clause. The tutorial exercise below shows you that DISTINCT works on selected columns only:

SQL> CREATE TABLE ggl_team AS

SELECT first_name, last_name FROM employees

WHERE first_name = 'John';

Table created.

SQL> INSERT INTO ggl_team VALUES ('John', 'Chen');

SQL> INSERT INTO ggl_team VALUES ('James', 'Chen');

SQL> INSERT INTO ggl_team VALUES ('Peter', 'Chen');

SQL> INSERT INTO ggl_team VALUES ('John', 'Chen');

SQL> SELECT * FROM ggl_team;

FIRST_NAME   LAST_NAME

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

John Chen

John Russell

John Seo

John Chen

James Chen

Peter Chen

John Chen

SQL> SELECT DISTINCT * FROM ggl_team;

FIRST_NAME   LAST_NAME

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

Peter Chen

John Chen

James Chen

John Seo

John Russell

 145 views

128⟩ What Are Group Functions in Oracle?

Group functions are functions applied to a group of rows. Examples of group functions are:

* COUNT(*) - Returns the number of rows in the group.

* MIN(exp) - Returns the minimum value of the expression evaluated on each row of the group.

* MAX(exp) - Returns the maximum value of the expression evaluated on each row of the group.

* AVG(exp) - Returns the average value of the expression evaluated on each row of the group.

 142 views

129⟩ How To Use Group Functions in the SELECT Clause using Oracle?

If group functions are used in the SELECT clause, they will be used on the rows that meet the query selection criteria, the output of group functions will be returned as output of the query. The following select statement returns 4 values calculate by 4 group functions on all rows of the "departments" table:

SQL> SELECT COUNT(*), MIN(department_id),

2 MAX(department_id) FROM departments;

 COUNT(*) MIN(DEPARTMENT_ID) MAX(DEPARTMENT_ID)

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

27 10 270

 144 views

130⟩ Can Group Functions Be Mixed with Non-group Selection Fields?

If a group function is used in the SELECT clause, all other selection fields must be group level fields. Non-group fields can not be mixed with group fields in the SELECT clause. The script below gives you an example of invalid SELECT statements with group and non-gorup selection fields:

SQL> SELECT COUNT(*), department_id FROM departments;

ORA-00937: not a single-group group function

In this example, COUNT(*) is a group field and department_id is a non-group field.

 133 views

131⟩ How To Divide Query Output into Groups in Oracle?

You can divide query output into multiple groups with the GROUP BY clause. It allows you specify a column as the grouping criteria, so that rows with the same value in the column will be considered as a single group. When the GROUP BY clause is specified, the select statement can only be used to return group level information. The following script gives you a good GROUP BY example:

SQL> SELECT department_id, MIN(salary), MAX(salary),

2 AVG(salary) FROM employees GROUP BY department_id;

DEPARTMENT_ID MIN(SALARY) MAX(SALARY) AVG(SALARY)

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

100 6900 12000 8600

30 2500 11000 4150

7000 7000 7000

90 17000 24000 19333.3333

20 6000 13000 9500

70 10000 10000 10000

110 8300 12000 10150

50 2100 8200 3475.55556

......

 154 views

132⟩ How To Apply Filtering Criteria at Group Level in Oracle?

If you want to return only specific groups from the query, you can apply filtering criteria at the group level by using the HAVING clause inside the GROUP BY clause. The following script gives you a good HAVING example:

SQL> SELECT department_id, MIN(salary), MAX(salary),

2 AVG(salary) FROM employees GROUP BY department_id

3 HAVING AVG(salary) < 5000;

DEPARTMENT_ID MIN(SALARY) MAX(SALARY) AVG(SALARY)

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

30 2500 11000 4150

50 2100 8200 3475.55556

10 4400 4400 4400

 157 views

133⟩ How To Count Duplicated Values in a Column in Oracle?

If you have a column with duplicated values, and you want to know what are those duplicated values are and how many duplicates are there for each of those values, you can use the GROUP BY ... HAVING clause as shown in the following example. It returns how many duplicated first names in the employees table:

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

......

 142 views

134⟩ Can Multiple Columns Be Used in GROUP BY in Oracle?

You can use multiple columns in the GROUP BY clause as shown in the following example. It returns how many employees are having the same salary in each department:

SQL> SELECT department_id, salary, count(*)

2 FROM employees GROUP BY department_id,

3 salary HAVING count(*) > 1;

DEPARTMENT_ID  SALARY COUNT(*)

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

90 17000 2

50 3200 4

50 2200 2

50 3600 2

80 10500 2

80 9000 2

50 2700 2

......

 151 views

135⟩ Can Group Functions Be Used in the ORDER BY Clause in Oracle?

If the query output is aggregated as groups, you can sort the groups by using group functions in the ORDER BY clause. The following statement returns how many employees are having the same salary in each department. The group output is sorted by the count in each group in descending order:

SQL> SELECT department_id, salary, count(*)

2 FROM employees GROUP BY department_id,

3 salary HAVING count(*) > 1

ORDER BY COUNT(*) DESC;

DEPARTMENT_ID  SALARY COUNT(*)

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

50 2500 5

50 3200 4

50 2800 3

80 10000 3

80 9500 3

50 3100 3

50 2600 3

.....

 146 views

136⟩ How To Join Two Tables in a Single Query using Oracle?

Two tables can be joined together in a query in 4 ways in Oracle:

* Inner Join: Returns only rows from both tables that satisfy the join condition.

* Left Outer Join: Returns rows from both tables that satisfy the join condition, and the rest of rows from the first (left) table.

* Right Outer Join: Returns rows from both tables that satisfy the join condition, and the rest of rows from the second (right) table.

* Full Outer Join: Returns rows from both tables that satisfy the join condition, the rest of rows from the first (left) table, and the rest of rows from the second (right) table.

 145 views

137⟩ How To Write a Query with an Inner Join in Oracle?

If you want to query from two tables with an inner join, you can use the INNER JOIN ... ON clause in the FROM clause. The following query returns output with an inner join from two tables: employees and departments. The join condition is that the department ID in the employees table equals to the department ID in the departments table:

SQL> SELECT employees.first_name, employees.last_name,

2 departments.department_name

3 FROM employees INNER JOIN departments

4 ON employees.department_id=departments.department_id;

FIRST_NAME   LAST_NAME  DEPARTMENT_NAME

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

Steven King Executive

Neena Kochhar Executive

Lex De Haan Executive

Alexander Hunold IT

Bruce Ernst IT

David Austin IT

Valli Pataballa IT

......

Note that when multiple tables are used in a query, column names need to be prefixed with table names in case the same column name is used in both tables

 157 views

138⟩ How To Define and Use Table Alias Names in Oracle?

When column names need to be prefixed with table names, you can define table alias name and use them to prefix column names as shown in the following select statement:

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

FROM employees e INNER JOIN departments d

ON e.department_id=d.department_id;

FIRST_NAME   LAST_NAME  DEPARTMENT_NAME

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

Steven King Executive

Neena Kochhar Executive

Lex De Haan Executive

Alexander Hunold IT

Bruce Ernst IT

David Austin IT

Valli Pataballa IT

 144 views

139⟩ Where Are the Settings Stored for Each Instance in Oracle?

Settings for each instance are stored in a file called Server Parameter File (SPFile). Oracle supports two types of parameter files, Text type, and Binary type. parameter files should be located in $ORACLE_HOMEdatabase directory. A parameter file should be named like "init$SID.ora", where $SID is the instance name.

 141 views

140⟩ How To Write a Query with a Left Outer Join in Oracle?

If you want to query from two tables with a left outer join, you can use the LEFT OUTER JOIN ... ON clause in the FROM clause. The following query returns output with a left 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 LEFT 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

......

Treasury NULL NULL

Corporate Tax NULL NULL

Control And Credit NULL NULL

Shareholder Services NULL NULL

Benefits NULL NULL

Manufacturing NULL NULL

Construction NULL NULL

 155 views