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
SELECT expression [, expression ...];
expression: a value, such as42or'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:
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
SELECT 42;
?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
SELECT 'apple', 42;
?column? | ?column?
----------+----------
apple | 42
(1 row)
Each value got its own column.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Adding FROM dual, a habit from other databases | ERROR: relation "dual" does not exist | Leave 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