Full answer
A SELECT without FROM evaluates its expressions once and returns a single row. It is the quickest way to test how a function or operator behaves before using it on a real table.
SELECT 1 + 1 AS two, UPPER('pune') AS city, LENGTH('canteen') AS letters;| two | city | letters |
|---|---|---|
| 2 | PUNE | 7 |
This is useful in interviews too. If you are unsure whether LENGTH counts bytes or characters, or what ROUND(2.5) returns, a one-line SELECT answers it.
Oracle historically required every SELECT to have a FROM clause. To make the expression form possible, it ships a table named DUAL with exactly one row and one column, so people write SELECT SYSDATE FROM dual. MySQL accepts FROM DUAL for compatibility, and recent Oracle releases also allow omitting FROM. Those Oracle and MySQL details were not run here.
Since there is no table, there are no rows to filter. A WHERE clause on a FROM-less SELECT still works in PostgreSQL and SQLite: if the condition is false you get zero rows.
SELECT 'shown' AS label WHERE 1 = 0;| label |
|---|
(0 rows)
SELECT 1 + 1 AS two, UPPER('pune') AS city, LENGTH('canteen') AS letters;
SELECT 'shown' AS label WHERE 1 = 0;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- SELECT without FROM returns one row of computed values
- It is a fast way to test functions and operators
- Oracle used the one-row DUAL table because it required FROM
Common mistakes
- Writing FROM dual on PostgreSQL, where no such table exists
Follow-ups
- What does ROUND(2.5) return on SQLite and on PostgreSQL?
- How would you generate several rows without a table?
Tips
- Use this trick live in a coding round to check a function before relying on it