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

82⟩ How To Concatenate Two Text Values in Oracle?

There are two ways to concatenate two text values together:

* CONCAT() function.

* '||' operation.

Here is some examples on how to use them:

SELECT 'ggl' || 'Center' || '.com' FROM DUAL;

rendc.org

SELECT CONCAT('globalguideline','.com') FROM DUAL;

rendc.org

 162 views

83⟩ How To Increment Dates by 1 in Oracle?

If you have a date, and you want to increment it by 1. You can do this by adding the date with a date interval. You can also do this by adding the number 1 directly on the date. The tutorial example below shows you how to adding numbers to dates, and take date differences:

SELECT TO_DATE('30-APR-06') + 1 FROM DUAL;

-- Adding 1 day to a date

01-MAY-06

SELECT TO_DATE('01-MAY-06') - TO_DATE('30-APR-06')

FROM DUAL;

-- Taking date differences

1

SELECT SYSTIMESTAMP + 1 FROM DUAL;

-- The number you add is always in days.

08-MAY-06

SELECT TO_CHAR(SYSTIMESTAMP+1,'DD-MON-YYYY HH24:MI:SS.FF3')

FROM DUAL;

-- Error: Adding 1 to a timestamp makes it a date.

 156 views

84⟩ How To Calculate Date and Time Differences in Oracle?

If you want to know how many years, months, days and seconds are there between two dates or times, you can use the date and time interval expressions: YEAR ... TO MONTH and DAY ... TO SECOND. The tutorial exercise below gives you some good examples:

SELECT

(TO_DATE('01-MAY-2006 16:52:57','DD-MON-YYYY HH24:MI:SS')

-

TO_DATE('31-JAN-1897 09:26:50','DD-MON-YYYY HH24:MI:SS'))

YEAR(4) TO MONTH FROM DUAL;

-- 109 years and 3 months

109-3

SELECT

(TO_DATE('01-MAY-2006 16:52:57','DD-MON-YYYY HH24:MI:SS')

-

TO_DATE('31-JAN-1897 09:26:50','DD-MON-YYYY HH24:MI:SS'))

DAY(9) TO SECOND FROM DUAL;

-- 39901 days and some seconds

39901 7:26:7.0

SELECT

(TO_TIMESTAMP('01-MAY-2006 16:52:57.847',

'DD-MON-YYYY HH24:MI:SS.FF3') -

TO_TIMESTAMP('31-JAN-1897 09:26:50.124',

'DD-MON-YYYY HH24:MI:SS.FF3'))

YEAR(4) TO MONTH FROM DUAL;

-- 109 years and 3 months

109-3

SELECT

(TO_TIMESTAMP('01-MAY-2006 16:52:57.847',

'DD-MON-YYYY HH24:MI:SS.FF3') -

TO_TIMESTAMP('31-JAN-1897 09:26:50.124',

'DD-MON-YYYY HH24:MI:SS.FF3'))

DAY(9) TO SECOND

FROM DUAL;

-- 39

 150 views

85⟩ How To Use IN Conditions in Oracle?

An IN condition is single value again a list of values. It returns TRUE, if the specified value is in the list. Otherwise, it returns FALSE.

Some examples are given in the script below:

SELECT CASE WHEN 3 IN (1,2,3,5) THEN

'TRUE' ELSE 'FALSE' END FROM DUAL;

TRUE

SELECT CASE WHEN 3 NOT IN (1,2,3,5) THEN

'TRUE' ELSE 'FALSE' END FROM DUAL;

FALSE

SELECT CASE WHEN 'Y' IN ('F','Y','I') THEN

'TRUE' ELSE 'FALSE' END FROM DUAL;

TRUE

 173 views

86⟩ How To Use LIKE Conditions in Oracle?

LIKE condition is also called pattern patch. There 3 main rules on using LIKE condition:

* '_' is used in the pattern to match any one character.

* '%' is used in the pattern to match any zero or more characters.

* ESCAPE clause is used to provide the escape character in the pattern.

The following script provides you some good pattern matching examples:

SELECT CASE WHEN 'rendc.org' LIKE '%Center%'

THEN 'TRUE' ELSE 'FALSE' END FROM DUAL;

TRUE

SELECT CASE WHEN 'rendc.org' LIKE '%CENTER%'

THEN 'TRUE' ELSE 'FALSE' END FROM DUAL;

-- Case sensitive by default

FALSE

SELECT CASE WHEN 'rendc.org' LIKE '%Center_com'

THEN 'TRUE' ELSE 'FALSE' END FROM DUAL;

TRUE

SELECT CASE WHEN '100% correct' LIKE '100% %' ESCAPE ''

THEN 'TRUE' ELSE 'FALSE' END FROM DUAL;

TRUE

 145 views

87⟩ How To Use Regular Expression in Pattern Match Conditions in Oracle?

If you have a pattern that is too complex for LIKE to handle, you can use the regular expression pattern patch function: REGEXP_LIKE().

The following script provides you some good examples:

SELECT CASE WHEN REGEXP_LIKE ('rendc.org', '.*ggl.*',

'i') THEN 'TRUE' ELSE 'FALSE' END FROM DUAL;

TRUE

SELECT CASE WHEN REGEXP_LIKE ('rendc.org', '.*com$',

'i') THEN 'TRUE' ELSE 'FALSE' END FROM DUAL;

TRUE

SELECT CASE WHEN REGEXP_LIKE ('rendc.org', '^F.*','i')

THEN 'TRUE' ELSE 'FALSE' END FROM DUAL;

TRUE

 144 views

88⟩ What Are DDL Statements in Oracle?

DDL (Data Definition Language) statements are statements to create and manage data objects in the database. The are 3 primary DDL statements:

* CREATE - Creating a new database object.

* ALTER - Altering the definition of an existing data object.

* DROP - Dropping an existing data object.

 145 views

89⟩ How To Create a New Table in Oracle?

If you want to create a new table in your own schema, you can log into the server with your account, and use the CREATE TABLE statement. The following script shows you how to create a table:

>.insqlplus /nolog

SQL> connect HR/globalguideline

Connected.

SQL> CREATE TABLE tip (id NUMBER(5) PRIMARY KEY,

2 subject VARCHAR(80) NOT NULL,

3 description VARCHAR(256) NOT NULL,

4 create_date DATE DEFAULT (sysdate));

Table created.

This scripts creates a testing table called "tip" with 4 columns in the schema associated with the log in account "HR".

 164 views

90⟩ How To Create a New Table by Selecting Rows from Another Table?

Let's say you have a table with many data rows, now you want to create a backup copy of this table of all rows or a subset of them, you can use the CREATE TABLE...AS SELECT statement to do this. Here is an example script:

>.insqlplus /nolog

SQL> connect HR/globalguideline

Connected.

SQL> CREATE TABLE emp_dept_10

2 AS SELECT * FROM employees WHERE department_id=10;

Table created.

SQL> SELECT first_name, last_name, salary

2 FROM emp_dept_10;

FIRST_NAME   LAST_NAME    SALARY

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

Jennifer Whalen 4400

As you can see, this SQL scripts created a table called "emp_dept_10" using the same column definitions as the "employees" table and copied data rows of one department.

This is really a quick and easy way to create a table.

 141 views

91⟩ How To Add a New Column to an Existing Table in Oracle?

If you have an existing table with existing data rows, and want to add a new column to that table, you can use the ALTER TABLE ... ADD statement to do this. Here is an example script:

SQL> connect HR/globalguideline

Connected.

SQL> CREATE TABLE emp_dept_110

2 AS SELECT * FROM employees WHERE department_id=110;

Table created.

SQL> ALTER TABLE emp_dept_110 ADD (vacation NUMBER);

Table altered.

SQL> SELECT first_name, last_name, vacation

2 FROM emp_dept_110;

FIRST_NAME   LAST_NAME    VACATION

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

Shelley Higgins

William Gietz

This SQL script added a new column called "vacation" to the "emp_dept_110" table. NULL values were added to this column on all existing data rows.

 157 views

92⟩ How To Delete a Column in an Existing Table in Oracle?

If you have an existing column in a table and you need that column any more, you can delete it with ALTER TABLE ... DROP COLUMN statement. Here is an example SQL script:

SQL> CREATE TABLE emp_dept_90

2 AS SELECT * FROM employees WHERE department_id=90;

Table created.

SQL> SELECT last_name FROM emp_dept_90;

LAST_NAME

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

King

Kochhar

De Haan

SQL> ALTER TABLE emp_dept_90 DROP COLUMN last_name;

Table altered.

SQL> SELECT last_name FROM emp_dept_90;

ERROR at line 1:

ORA-00904: "LAST_NAME": invalid identifier

As you can see the column "last_name" is gone.

 190 views

93⟩ How To Drop an Existing Table in Oracle?

If you want to delete an existing table and its data rows, you can use the DROP TABLE statement as shown in this script:

SQL> connect HR/globalguideline

Connected.

SQL> CREATE TABLE emp_dept_10

2 AS SELECT * FROM employees WHERE department_id=10;

Table created.

SQL> DROP TABLE emp_dept_10;

Table dropped.

Be careful, when you use the DROP TABLE statement. All data rows are gone too.

 147 views

94⟩ How To Create a Table Index in Oracle?

If you have a table with a lots of rows, and you know that one of the columns will be used often a search criteria, you can add an index for that column to in improve the search performance. To add an index, you can use the CREATE INDEX statement as shown in the following script:

CREATE TABLE tip (id NUMBER(5) PRIMARY KEY,

subject VARCHAR(80) NOT NULL,

description VARCHAR(256) NOT NULL,

create_date DATE DEFAULT (sysdate));

Table created.

CREATE INDEX tip_subject ON tip(subject);

Index created.

 142 views

95⟩ How To Rename an Index in Oracle?

Let's say you have an existing index, and you don't like its name anymore for some reason, you can rename it with the ALTER INDEX ... RENAME TO statement. Here is an example script on how to rename an index:

CREATE TABLE student (id NUMBER(5) PRIMARY KEY,

first_name VARCHAR(80) NOT NULL,

last_name VARCHAR(80) NOT NULL,

birth_date DATE NOT NULL,

social_number VARCHAR(80) UNIQUE NOT NULL);

Table created.

SELECT index_name, table_name, uniqueness

FROM USER_INDEXES WHERE table_name = 'STUDENT';

INDEX_NAME   TABLE_NAME  UNIQUENES

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

SYS_C004153 STUDENT UNIQUE

SYS_C004154 STUDENT UNIQUE

ALTER INDEX SYS_C004153 RENAME TO student_pk;

Statement processed.

SELECT index_name, table_name, uniqueness

FROM USER_INDEXES WHERE table_name = 'STUDENT';

INDEX_NAME   TABLE_NAME  UNIQUENES

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

STUDENT_PK STUDENT UNIQUE

SYS_C004154 STUDENT UNIQUE

 180 views

96⟩ How To Drop an Index in Oracle?

If you don't need an existing index any more, you should delete it with the DROP INDEX statement. Here is an example SQL script:

CREATE TABLE student (id NUMBER(5) PRIMARY KEY,

first_name VARCHAR(80) NOT NULL,

last_name VARCHAR(80) NOT NULL,

birth_date DATE NOT NULL,

social_number VARCHAR(80) UNIQUE NOT NULL);

Table created.

CREATE INDEX student_birth_date ON student(birth_date);

Index created.

SELECT index_name, table_name, uniqueness

FROM USER_INDEXES WHERE table_name = 'STUDENT';

INDEX_NAME   TABLE_NAME  UNIQUENES

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

SYS_C004129 STUDENT UNIQUE

SYS_C004130 STUDENT UNIQUE

STUDENT_BIRTH_DATE STUDENT NONUNIQUE

DROP INDEX STUDENT_BIRTH_DATE;

Index dropped.

 141 views

97⟩ How To Create a New View in Oracle?

You can create a new view based on one or more existing tables by using the CREATE VIEW statement as shown in the following script:

CREATE VIEW employee_department AS

SELECT e.employee_id, e.first_name, e.last_name,

e.email, e.manager_id, d.department_name

FROM employees e, departments d

WHERE e.department_id = d.department_id;

View created.

SELECT first_name, last_name, department_name

FROM employee_department WHERE manager_id = 101;

FIRST_NAME   LAST_NAME   DEPARTMENT_NAME

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

Nancy Greenberg Finance

Jennifer Whalen Administration

Susan Mavris Human Resources

Hermann Baer Public Relations

Shelley Higgins Accounting

 151 views

98⟩ How To Drop an Existing View in Oracle?

If you have an existing view, and you don't want it anymore, you can delete it by using the DROP VIEW statement as shown in the following script:

DROP VIEW employee_department;

View dropped.

 146 views

99⟩ What Are DML Statements in Oracle?

DML (Data Manipulation Language) statements are statements to change data values in database tables. The are 3 primary DML statements:

* INSERT - Inserting new rows into database tables.

* UPDATE - Updating existing rows in database tables .

* DELETE - Deleting existing rows from database tables.

 137 views

100⟩ How To Create a Testing Table in Oracle?

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

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

url VARCHAR2(80) NOT NULL,

notes VARCHAR2(1024),

counts NUMBER,

created DATE DEFAULT (sysdate));

Table created.

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

 145 views