Sub topic 2
What DUAL Is and Why Oracle Has It
Assume you already understand tables, rows, columns, and basic Oracle data types. You also have access to an Oracle Database session through SQL*Plus, SQL Developer, or SQL Worksheet.
Most SELECT statements read data from a table:
SELECT employee_id, last_name
FROM employees;The FROM clause identifies the row source. Oracle evaluates the selected expressions once for every row in that source. But sometimes you need to evaluate an expression without reading an application table. For example, you might want the current date, a calculation, or the next value from a sequence. Oracle provides the DUAL table for this purpose.
What DUAL contains
DUAL is a special one-row, one-column table. Its column is named DUMMY, and its data type is VARCHAR2(1). The row contains the value X.
SELECT * FROM dual;The result is one row:
| DUMMY |
|---|
| X |
Because DUAL has exactly one row, a query against it normally returns exactly one row:
SELECT 'Hello, Oracle' AS greeting,
2 + 3 AS total
FROM dual;| GREETING | TOTAL |
|---|---|
| Hello, Oracle | 5 |
The selected expressions do not need to use the DUMMY column. The row supplied by DUAL simply gives Oracle a row source on which to evaluate them.
Why Oracle uses DUAL
In most Oracle Database releases before 23ai, a SELECT statement requires a FROM clause. If no business table is involved, DUAL supplies the required single row.
This makes DUAL useful for testing expressions and calling single-row functions:
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS now_text,
UPPER('oracle') AS upper_text,
'Ora' || 'cle' AS joined_text
FROM dual;SYSDATE returns the database server's current date and time. TO_CHAR formats that date as text. UPPER converts text to uppercase. The || operator concatenates strings.
A query against DUAL also helps isolate an expression from application data. If the following query fails, the problem is in the expression or its data types, not in a table join or a row filter.
SELECT ROUND(15.678, 2) AS rounded_value,
LENGTH('Oracle') AS character_count
FROM dual;Using DUAL with sequences
A sequence is an Oracle object that generates numeric values. To request a sequence value, use its NEXTVAL pseudocolumn. A pseudocolumn behaves like a column in a query, although it is generated by Oracle rather than stored as ordinary table data.
CREATE SEQUENCE order_seq
START WITH 1
INCREMENT BY 1;
SELECT order_seq.NEXTVAL AS next_id
FROM dual;The query returns one generated value. Running it again returns the next value. Sequence values are not guaranteed to be gap-free. A value can be consumed by a transaction that later rolls back.
The same pattern is useful when inserting a row:
INSERT INTO orders (order_id, order_date)
VALUES (order_seq.NEXTVAL, SYSDATE);The INSERT statement does not need DUAL because it already has a target table. DUAL is needed when you want to select the generated value by itself.
DUAL and newer Oracle releases
Oracle Database 23ai allows a SELECT without a FROM clause:
SELECT 1 + 1 AS two;On earlier releases, this syntax raises an error, so use:
SELECT 1 + 1 AS two
FROM dual;Using DUAL remains portable across older Oracle versions and makes the intended one-row source explicit.
Exercise
What result should SELECT UPPER('sql') AS text_value FROM dual; return, and why does it contain one row?
Show answer
It returns one row with TEXT_VALUE equal to SQL. DUAL supplies exactly one row, and UPPER evaluates the string once.