Skip to main content
0 reactions

How to Use SELECT Without FROM in PostgreSQL

Featured card for How to Use SELECT Without FROM in PostgreSQL, with a terminal window listing SELECT 42;, SELECT ‘apple’, 42;, the ?column? name and no FROM dual.
How to use SELECT without FROM in PostgreSQL.

In PostgreSQL, SELECT doesn’t need a table: SELECT 42; returns 42. It’s a quick way to try a value, a calculation or a function.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxSQL
SELECT expression [, expression ...];
  • expression: a value, such as 42 or 'apple', or anything that works it out.
  • Separate several expressions with commas. Each one becomes a column of the result.
  • [ ] marks the optional part.

See the SELECT (opens in a new tab) reference in the PostgreSQL documentation, which notes that PostgreSQL lets you leave out FROM.

The Example

A number on its own, then a word and a number side by side.

Step 1: Connect

Open Terminal and connect to your PostgreSQL server with psql as the postgres user:

TerminalBash
psql -U postgres

Type the user’s password when psql asks for it. psql connects you to the postgres database, and its prompt is postgres=#.

Step 2: Select One Value

postgres=#SQL
SELECT 42;
OutputPlain text
 ?column?
----------
       42
(1 row)

One row with one column. ?column? is the name PostgreSQL gives a column it has no name for.

Step 3: Select Several Values

postgres=#SQL
SELECT 'apple', 42;
OutputPlain text
 ?column? | ?column?
----------+----------
 apple    |       42
(1 row)

Each value got its own column.

Common Mistakes

MistakeWhat happensDo this instead
Adding FROM dual, a habit from other databasesERROR: relation "dual" does not existLeave out FROM: SELECT 42;.

FAQ

Can I name the column?

Yes: put AS and a name after the value. Without it, PostgreSQL names the column ?column?.

Conclusion

SELECT with just values, and no FROM, returns those values as one row. Use it to try anything out before you put it in a real query.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments