Sub topic 2
What DUAL is and why Oracle has it
This chapter explains the DUAL table: what it holds, why Oracle needs it, and the mistakes people make with it. It assumes you can already run a basic SELECT ... FROM table in SQL*Plus, SQL Developer, or SQL Worksheet. It also assumes you know what rows, columns, and the basic data types are. The SELECT and FROM clauses were covered in the previous chapter.
The problem DUAL solves
In Oracle SQL, a SELECT statement needs a FROM clause. The FROM clause names a row source, and the query produces its output once for each row the row source supplies. The SELECT list says what to compute for each of those rows.
Sometimes you want a value that has no table behind it. You might want to compute 2 + 3, see what SYSDATE returns, or test how a string function behaves. Before Oracle Database 23ai, this fails:
SELECT 2 + 3 AS total;Oracle raises ORA-00923: FROM keyword not found where expected. The parser expects a row source, and there isn't one.
Oracle's answer is DUAL, a table that exists only to be a row source with exactly one row. Query it and your expressions are evaluated exactly once, so you get one result row.
What DUAL contains
DUAL has one column named DUMMY, of type VARCHAR2(1), and one row whose value is 'X'. You can check this yourself:
DESCRIBE dual
SELECT * FROM dual;DESCRIBE is a SQL*Plus and SQL Developer command, not SQL, so it needs no semicolon. The query returns:
DUMMY
-----
XThe value 'X' has no meaning. It is a placeholder, and the name DUMMY says as much. In normal use you never select DUMMY and never care what it holds. What matters is that the table has one row.
| Property | Value |
|---|---|
| Owner | `SYS` |
| Column | `DUMMY VARCHAR2(1)` |
| Rows | 1 (the value `'X'`) |
| Access | Public synonym `DUAL`, with `SELECT` granted to `PUBLIC` |
| Typical use | Evaluating expressions, functions, `SYSDATE`, and sequence values |
Why one row matters
A query returns one output row for each row the FROM clause supplies. This is the core of how DUAL works. Compare these two queries:
SELECT 'Hello, Oracle' AS greeting, 2 + 3 AS total FROM dual;This returns exactly one row, because DUAL supplies one row. The same select list against a table with 107 rows would return 107 identical rows, one per table row.
SELECT 'Hello, Oracle' AS greeting, 2 + 3 AS total
FROM employees;This is why DUAL has to hold exactly one row. A scalar calculation should produce one answer, not as many answers as some unrelated table has rows. The WHERE clause still applies to DUAL like any other table:
SELECT 'visible' AS msg FROM dual WHERE 1 = 0;This returns zero rows. The row source supplied one row, but the filter rejected it. You can use this to confirm that a condition is false without needing a real table.
Where DUAL lives and how you reach it
DUAL is owned by the SYS schema, which is the data dictionary owner. Oracle creates it when it builds the database. A public synonym named DUAL points to SYS.DUAL, and PUBLIC has SELECT on it. Because of this, every user can write FROM dual without a schema prefix and without any grant.
The following query shows the owner and the synonym:
SELECT owner, object_name, object_type
FROM all_objects
WHERE object_name = 'DUAL'
ORDER BY owner, object_type;You should see a SYS table named DUAL and a PUBLIC synonym named DUAL. Unquoted Oracle identifiers are stored in uppercase, which is why the filter uses 'DUAL'.
When you write FROM dual, Oracle resolves the name in a fixed order. It looks first in your own schema, then falls back to public synonyms.
You can bypass name resolution entirely by writing SYS.DUAL. This is rarely needed, but it is useful when you suspect a shadowing problem.
A short history
DUAL dates from early Oracle versions. The name reflects its original design as a table with two rows, which could be joined to another table to duplicate each of its rows. Over time its use changed to the single-row form you see today. You don't need the history for day-to-day work. It explains the odd name, and it shows that the one-row design was a deliberate choice for scalar evaluation.
Using DUAL in practice
You will meet DUAL in several roles. The next chapters cover them in detail, so this section only shows the pattern.
Evaluating an expression:
SELECT 'Hello, Oracle' AS greeting, 2 + 3 AS total FROM dual;Testing single-row functions and the || concatenation operator without touching an application table:
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;Generating a sequence value, because NEXTVAL needs a row source:
CREATE SEQUENCE order_seq START WITH 1 INCREMENT BY 1;
SELECT order_seq.NEXTVAL AS next_id FROM dual;Producing a small set of rows, using DUAL as a one-row seed for a hierarchical query:
SELECT LEVEL AS n FROM dual CONNECT BY LEVEL <= 3;This returns the rows 1, 2, and 3. The query starts from one row and repeats it by level. The technique is common, but its details belong to a later topic.
How the optimizer treats DUAL
Reading DUAL looks like reading a table, but the optimizer special-cases it. Since Oracle Database 10g, a query against only DUAL uses an internal shortcut that avoids reading data blocks. In the execution plan you will typically see an operation named FAST DUAL. You can look at it:
EXPLAIN PLAN FOR SELECT 1 FROM dual;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);The practical result is that FROM dual is cheap. It does no logical reads on the table, so there is no reason to avoid it in frequently executed code. The optimization applies when DUAL is the only row source. If you join DUAL to another table, you pay for that table as usual.
Common errors and gotchas
Forgetting FROM before 23ai
This is the most common error. The statement SELECT SYSDATE; fails with ORA-00923 on releases before 23ai. The fix is SELECT SYSDATE FROM dual;. Code that must run on older databases should keep FROM dual, even though 23ai no longer requires it. The last chapter in this series covers the 23ai change.
Treating DUAL as a normal table
DUAL belongs to SYS, and you should never change it. Do not insert, update, or delete rows, and do not drop or recreate it. The optimizer and many internal and application queries assume exactly one row. If DUAL ends up with zero rows, expression queries return nothing. If it ends up with two rows, queries that should return one row return two, and code that expects a single value can fail with errors such as ORA-01422: exact fetch returns more than requested number of rows. These failures show up far from the cause, which makes them hard to trace.
Shadowing DUAL
Name resolution checks your own schema before public synonyms. If you create a table or view named DUAL in your schema, your queries will use it instead of SYS.DUAL. Your table might have no rows, or many. Queries that worked before may then return zero rows or too many, and only for your user. A schema-qualified query shows the difference:
SELECT COUNT(*) AS rows_in_default FROM dual;
SELECT COUNT(*) AS rows_in_sys FROM sys.dual;Both should return 1. If they differ, something is shadowing DUAL.
Selecting DUMMY and expecting meaning
SELECT dummy FROM dual returns 'X', but the value carries no information. Don't write logic that depends on it.
Assuming other databases behave the same
DUAL is an Oracle idiom. MySQL accepts FROM DUAL for compatibility, but PostgreSQL and SQL Server allow a SELECT with no FROM at all. Oracle Database 23ai joined them by accepting SELECT 1 + 1 AS two;. When you port SQL between databases, FROM dual is the part most likely to need changing.
Summary of the working model
- DUAL is a
SYStable with oneVARCHAR2(1)column,DUMMY, and one row,'X'. - Every user reaches it through a public synonym.
- Its job is to supply one row, so that an expression is evaluated once and returns one result row.
- The optimizer handles it cheaply with
FAST DUAL. - You read from it and never write to it.
Exercise
A colleague runs SELECT 'ready' AS status FROM dual; and gets 3 rows instead of 1, but the same query works for everyone else. What is the most likely cause, and how can you confirm it?
Show answer
The colleague's schema probably owns a table or view named DUAL that holds 3 rows. Name resolution finds it before the public synonym, so it shadows SYS.DUAL. Confirm it by comparing SELECT COUNT(*) FROM dual; with SELECT COUNT(*) FROM sys.dual;. The second query should return 1 and the first returns 3. The fix is to drop the extra DUAL object from that schema.
Exercise
What does SELECT 'visible' AS msg FROM dual WHERE 1 = 0; return, and why?
Show answer
It returns zero rows. DUAL supplies one row, but the WHERE condition 1 = 0 is false, so that row is filtered out before the result is returned.