HiveBrain v1.2.0
Get Started
← Back to all entries
principlesqlMinor

SELECT sql_function(...) vs SELECT * FROM sql_function(...)

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
selectfromsql_function

Problem

I'm reading the PostgreSQL documentation for the first time and when facing SQL functions I think I have understood all the basics, but still can't see the difference between SELECT sql_function(...) and SELECT * FROM sql_function(...) and when to use one or the other.

Solution

You typically use a function in FROM if it is a set-returning function (SRF). Such functions return a result set rather than a single value or tuple.

But, confusingly, this is not written in stone in PostgreSQL:

-
You can also call a SRF in the SELECT list, as in SELECT f().

As the documentation says:

Functions returning sets can also be called in the select list of a query. For each row that the query generates by itself, the set-returning function is invoked, and an output row is generated for each element of the function's result set.

-
You can use any function in the FROM clause. It is then treated as a SRF that returns only a single row.

Context

StackExchange Database Administrators Q#281220, answer score: 5

Revisions (0)

No revisions yet.