Sub topic 13
SELECT Without FROM in Oracle Database 23ai
Prerequisites and the version difference
This chapter assumes that you understand tables, rows, columns, numbers, strings, and dates. You also need access to an Oracle Database session through SQL*Plus, SQL Developer, or another SQL worksheet.
A SELECT statement normally reads data from a row source. A row source is a table, view, or another object that supplies rows. In most Oracle Database releases, even a query that only calculates a value needs a FROM clause. Oracle provides the special DUAL table for this purpose.
Oracle Database 23ai allows a SELECT statement without a FROM clause. This is called a from-less SELECT. It is useful when the query only evaluates literals, expressions, or functions and does not read application data.
The 23ai syntax
In Oracle Database 23ai, you can write:
SELECT 1 + 1 AS two;The result contains one row and one column named TWO, with the value 2.
The same calculation in an earlier Oracle release requires DUAL:
SELECT 1 + 1 AS two
FROM dual;The expression and result are the same. Only the row source syntax differs.
| Oracle release | Query form | Use |
|---|---|---|
| Oracle Database 23ai | `SELECT expression;` | Calculate without a row source |
| Earlier releases | `SELECT expression FROM dual;` | Use the one-row `DUAL` table |
Do not add a semicolon when sending SQL through an API that expects only the statement text. SQL*Plus and many worksheet tools use the semicolon to recognize the end of the statement.
What can be selected without a table?
A from-less query can evaluate constants, arithmetic, string operations, and single-row functions. A single-row function returns one result for each input value. Examples include UPPER, TO_CHAR, and date functions.
SELECT 'Oracle' AS product,
2 + 3 AS total,
UPPER('sql') AS language_name,
TO_CHAR(SYSDATE, 'YYYY-MM-DD') AS today;This query returns one row because no table supplies multiple rows. SYSDATE returns the database server's current date and time. TO_CHAR converts that date into formatted text.
The query can also evaluate a sequence value if the expression is valid in the database version and context:
SELECT order_seq.NEXTVAL AS next_id;A sequence generates numeric values, often for identifiers. NEXTVAL requests the next value. In releases or contexts that require a row source, use:
SELECT order_seq.NEXTVAL AS next_id
FROM dual;Choosing between 23ai syntax and DUAL
Use the from-less form when your code targets Oracle Database 23ai or a later compatible release and the statement does not need a table. It is shorter and makes the purpose clear.
Use DUAL when the same SQL must run on older Oracle releases. DUAL is an Oracle-supplied table with one column, DUMMY, and one row containing X. Selecting an expression from it produces one result row.
How the database chooses the result shape
The selected expressions determine the columns. Since there is no table row source, Oracle evaluates the expressions once and returns one row. A query such as SELECT 1, 2, 3; therefore returns three columns and one row.
The version distinction matters when deploying scripts, creating shared application code, or moving SQL between database environments. Check the target database version before removing FROM dual.
Exercise
You must support Oracle Database 19c and Oracle Database 23ai with one query that returns the current date as YYYY-MM-DD text. Which form should you use, and why?
Show answer
Use SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD') AS today FROM dual;. DUAL is accepted by both releases, while the from-less form is not available in older releases.