← Back to SQL, page 1

sql-0009 · Easy fresher

Can you run a SELECT without a FROM clause? What is the DUAL table?

Yes in SQLite, PostgreSQL, MySQL and SQL Server: SELECT 1 + 1 returns one row with no table involved. Oracle, before version 23ai, required a FROM clause, so it provides a one-row table called DUAL for this purpose. The pattern is handy for testing expressions and functions.

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.

sql
SELECT 1 + 1 AS two, UPPER('pune') AS city, LENGTH('canteen') AS letters;
twocityletters
2PUNE7

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.

sql
SELECT 'shown' AS label WHERE 1 = 0;
label

(0 rows)

sql
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