Sub topic 13
SELECT without FROM in Oracle Database 23ai
This chapter covers the one place where the FROM clause stops being mandatory in Oracle SQL. It assumes you have read the earlier chapters on DUAL, column aliases, expressions and single-row functions, and that you can run statements in SQL*Plus, SQL Developer or SQL Worksheet. To try the examples that need 23ai, you need a 23ai session. Everything else runs on older releases too.
The rule before 23ai
In Oracle Database 21c and earlier, every query block needs a FROM clause. If you leave it out, the parser stops with an error:
SELECT 1 + 1 AS two;ORA-00923: FROM keyword not found where expectedThe reason is structural. SELECT describes what to compute for each row of a row source, and the FROM clause names that source. A query with no source has no rows to compute against. Oracle's historical answer was DUAL, a table with one row, so that expressions like 1 + 1 have exactly one row to be evaluated for.
SELECT 1 + 1 AS two FROM dual;This form works on every Oracle release and is still the portable choice.
What 23ai changes
Oracle Database 23ai makes the FROM clause optional when the select list contains only expressions. The statement that failed above now succeeds:
SELECT 1 + 1 AS two;The result is one row with one column named TWO. The behaviour is the same as SELECT 1 + 1 AS two FROM dual. Oracle supplies the single row itself, so you do not have to name a table that exists only to provide one.
All the DUAL examples from earlier in this tutorial can drop their FROM clause:
SELECT 'Hello, Oracle' AS greeting, 2 + 3 AS total;
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS now_text,
UPPER('oracle') AS upper_text,
'Ora' || 'cle' AS joined_text;Both statements return exactly one row, just as the DUAL versions do.
The working model
A useful way to think about it: a query with no FROM clause is evaluated against an implicit one-row source. The select list is computed once, and one row comes back.
The diagram must use the required fence, so here it is in full:
Two consequences follow from this model:
- The result always has exactly one row. There is no way to get zero or several rows from a FROM-less select list.
- Column references have nothing to refer to.
SELECT first_namewithout a FROM clause fails, because no table supplies that column.SELECT *fails for the same reason, since there is nothing to expand.
Common uses
Everything you used DUAL for in earlier chapters has a FROM-less equivalent.
| Task | With DUAL (all releases) | Without FROM (23ai) |
|---|---|---|
| Arithmetic | `SELECT 2 * 21 FROM dual;` | `SELECT 2 * 21;` |
| Current date and time | `SELECT SYSDATE FROM dual;` | `SELECT SYSDATE;` |
| Test a function | `SELECT UPPER('abc') FROM dual;` | `SELECT UPPER('abc');` |
| Next sequence value | `SELECT order_seq.NEXTVAL FROM dual;` | `SELECT order_seq.NEXTVAL;` |
Here is the sequence case end to end:
CREATE SEQUENCE order_seq START WITH 1 INCREMENT BY 1;
SELECT order_seq.NEXTVAL AS next_id;Each execution advances the sequence, whichever form you use. Removing FROM does not make the call read-only or repeatable.
Aliases and expression text
Aliases behave as before. Without an alias, the column heading is the text of the expression, normally shown in uppercase. That is awkward for long expressions, so alias anything you plan to read back.
SELECT 100 * 1.2 AS price_with_tax,
ROUND(100 / 3, 2) AS third_of_hundred,
'Total: ' || TO_CHAR(1234.5, 'FM9990.00') AS label;The FM modifier in the format model removes the padding spaces that TO_CHAR adds to numbers by default, so the label has no stray blanks.
Mixing with other clauses
Only the FROM clause becomes optional. The rest of the syntax keeps its normal rules. Because the select list is made of constants and functions, clauses such as ORDER BY or DISTINCT have no useful work to do on a single row, and filtering clauses depend on columns that do not exist. In practice a FROM-less SELECT is a short, one-line statement. If you find yourself adding more clauses, you probably want a real row source.
You can still use a FROM-less select inside larger statements where a query is allowed, for example as a scalar subquery:
SELECT employee_id,
last_name,
(SELECT SYSDATE) AS report_time
FROM employees
WHERE department_id = 50;Each row shows the same timestamp. Whether this reads better than calling SYSDATE directly in the outer select list is a style question. Here the plain SYSDATE is simpler, so this example only shows that the form is accepted.
Is DUAL obsolete?
No. DUAL remains in 23ai and works exactly as before. Three reasons to keep writing FROM dual:
- Portability. A script that must run on 19c, 21c and 23ai needs the FROM clause, because the older releases reject the short form. This applies to installation scripts, shared libraries and anything delivered to customers whose release you do not control.
- Existing code. Millions of lines of application SQL use DUAL. Rewriting them gains nothing, and mixed styles in one code base make reviews harder.
- Tooling. Some SQL parsers, linters, code generators and drivers have not caught up with the optional FROM clause. They may flag valid 23ai SQL as a syntax error.
Reasons to prefer the short form: interactive experiments, new code that only targets 23ai, and teaching material where DUAL would be a distraction from the point being made.
Common errors
Running the short form on an older database. The error is ORA-00923, as shown at the start of the chapter. The statement is correct for 23ai but the target release is older. The fix is to add FROM dual. Check which database a worksheet connection points to before assuming the SQL is wrong. Development and production often run different releases.
Expecting column references to work. SELECT last_name WHERE 1 = 1; has no source for last_name. The error is an invalid-identifier error, not a missing-FROM error, which can mislead you. Put the table back.
Expecting zero rows. With DUAL you could write SELECT 1 FROM dual WHERE 1 = 0 to get an empty result. That pattern depends on a real row source, so keep FROM dual in code that needs it, such as an empty-result test fixture.
Assuming sequences are free. A FROM-less NEXTVAL consumes a number just as the DUAL form does. Gaps in numbering still occur if you run it only to look at a value.
Mixing a client's own parser with the server. Some client tools split or validate statements locally before sending them. If the server accepts the statement in one tool and another tool rejects it, the tool is the likely culprit, not your SQL.
Checking the release you are on
If you are unsure which behaviour to expect, try the statement. A FROM-less SELECT is a cheap probe: it either returns a row or raises ORA-00923.
SELECT 'FROM-less SELECT works here' AS probe;On an older release, the equivalent that always works is:
SELECT 'FROM dual still works' AS probe FROM dual;For a precise version number, query the dictionary view that holds it, provided your account is allowed to read it:
SELECT banner FROM v$version;Summary
- Before 23ai, a SELECT without FROM raises ORA-00923. DUAL exists to supply one row.
- In 23ai, FROM is optional when the select list holds only expressions, and the result is one row.
- Column references,
SELECT *and row filtering do not make sense without a source. - DUAL still works everywhere. Use
FROM dualfor code that must run on older releases, and the short form when you only target 23ai.
Exercise
A script contains SELECT SYSDATE AS today;. It runs without problems on a 23ai development database, but fails on a 19c production database. What error do you expect, and what is the smallest change that makes the script work on both?
Show answer
The 19c database raises ORA-00923: FROM keyword not found where expected, because releases before 23ai require a FROM clause. Changing the statement to SELECT SYSDATE AS today FROM dual; works on both releases.
Exercise
On 23ai, why does SELECT first_name; fail even though FROM is now optional? Rewrite it so that it returns the first name of employee 100 from the HR sample schema.
Show answer
FROM is optional only when the select list needs no table. first_name is a column, and with no FROM clause there is no table to supply it, so Oracle cannot resolve the identifier. A working version is SELECT first_name FROM employees WHERE employee_id = 100;.
Exercise
You need a test fixture that returns an empty result set with one column, and the code must run on 19c and 23ai. Which form do you write, and why can a FROM-less SELECT not do this job?
Show answer
Write SELECT 1 AS dummy_col FROM dual WHERE 1 = 0;. It works on both releases and returns zero rows. A FROM-less SELECT always returns exactly one row from its implicit source, and it is not portable to 19c.