Select hardcoded values without table

Postgresql

Postgresql Problem Overview


I need to run a select without actually connecting to any table. I just have a predefined hardcoded set of values I need to loop over:

foo
bar
fooBar

And I want to loop through those values. I can do:

select 'foo', 'bar', 'fooBar';

But this returns it as one row:

 ?column? | ?column? | ?column? 
----------+----------+----------
 foo      | bar      | fooBar
(1 row)

I am using Postgresql.

Postgresql Solutions


Solution 1 - Postgresql

select a
from (
    values ('foo'), ('bar'), ('fooBar')
) s(a);

http://www.postgresql.org/docs/current/static/queries-values.html

Solution 2 - Postgresql

Using unnest()

> expand an array to a set of rows

select unnest(array['foo', 'bar', 'fooBar']);

demo

Solution 3 - Postgresql

Postgres SQL:

For static data output as single row and multiple columns representation use following query:

select a,b,c from (values('foo','bar','fooBar')) s(a,b,c);

result of this SQL query:

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRichard KnopView Question on Stackoverflow
Solution 1 - PostgresqlClodoaldo NetoView Answer on Stackoverflow
Solution 2 - PostgresqlVivek S.View Answer on Stackoverflow
Solution 3 - PostgresqlSolanki VaibhavView Answer on Stackoverflow