The answer has already been accepted, but I thought I would throw it in:
This can help think about the data type and where these types fit into the general query. SQL queries can return essentially three types:
- Single scalar value
- List of values
- Value table
(Of course, a list is just a table with one column, and a scalar is just a single-valued list.)
When you look at types, you see that the SQL SELECT query has the following pattern:
SELECT scalar(s) FROM table WHERE boolean-scalar
If your function or subquery returns a table, it belongs in the FROM clause. If it returns a list, it can go to the FROM clause or it can be used with the IN operator as part of the WHERE clause. If it returns a scalar, it can go into a SELECT clause, a FROM clause, or in a Boolean predicate in a WHERE clause.
This is an incomplete representation of SELECT queries, but I found that it helps to figure out where my subqueries should go.
source share