patternsqlModerate
What does [FROM x, y] mean in Postgres?
Viewed 0 times
postgreswhatmeandoesfrom
Problem
I'm just getting started with Postgres. Reading this document I came across this query:
I can understand everything in this query, except for this:
What does this
I searched the net for no avail. After looking at it and thinking, it seems to me that it's declaring a variable called query so it can use it multiple times. But if this is true, what does it have to do with
SELECT title, ts_rank_cd(textsearch, query) AS rank
FROM apod, to_tsquery('neutrino|(dark & matter)') query
WHERE query @@ textsearch
ORDER BY rank DESC
LIMIT 10;I can understand everything in this query, except for this:
FROM apod, ....What does this
, mean? I'm used to joins but not to multiple FROM statements separated by a comma.I searched the net for no avail. After looking at it and thinking, it seems to me that it's declaring a variable called query so it can use it multiple times. But if this is true, what does it have to do with
FROM?Solution
The manual has detailed explanation for the comma in the
The
a comma-separated table reference list.
A table reference can be a table name (possibly schema-qualified), or
a derived table such as a subquery, a
combinations of these. If more than one table reference is listed in
the
product of their rows is formed; see below).
The fact that comma-separated table references have been defined in an earlier version of the SQL standard than explicit
The manual again:
(see below). It is also equivalent to
There are corner cases where both are not completely equivalent. The manual:
Note
This latter equivalence does not hold exactly when more than two
tables appear, because
reference
This related question demonstrates the relevance of the difference:
Basically, your observation is exactly right:
it seems to me that it's declaring a variable called query so it can use it multiple times.
Any function can be used as "table function" in the
And function parameters can reference columns from all tables to the left of the function, because the notation:
... is really equivalent to:
The manual on LATERAL queries:
Table functions appearing in
arguments can contain references to columns provided by preceding FROM
items in any case.
Bold emphasis mine.
The keyword
FROM list in the chapter Table Expressions:The
FROM Clause derives a table from one or more other tables given ina comma-separated table reference list.
FROM table_reference [, table_reference [, ...]]A table reference can be a table name (possibly schema-qualified), or
a derived table such as a subquery, a
JOIN construct, or complexcombinations of these. If more than one table reference is listed in
the
FROM clause, the tables are cross-joined (that is, the Cartesianproduct of their rows is formed; see below).
The fact that comma-separated table references have been defined in an earlier version of the SQL standard than explicit
JOIN syntax does not make the comma wrong or outdated. Use explicit join syntax, where it's technically necessary (see below) or where it makes the query text clearer.The manual again:
FROM T1 CROSS JOIN T2 is equivalent to FROM T1 INNER JOIN T2 ON TRUE(see below). It is also equivalent to
FROM T1, T2.There are corner cases where both are not completely equivalent. The manual:
Note
This latter equivalence does not hold exactly when more than two
tables appear, because
JOIN binds more tightly than comma. For exampleFROM T1 CROSS JOIN T2 INNER JOIN T3 ON condition is not the same asFROM T1, T2 INNER JOIN T3 ON condition because the condition canreference
T1 in the first case but not the second.This related question demonstrates the relevance of the difference:
- “invalid reference to FROM-clause entry for table” in Postgres query
Basically, your observation is exactly right:
it seems to me that it's declaring a variable called query so it can use it multiple times.
Any function can be used as "table function" in the
FROM list.And function parameters can reference columns from all tables to the left of the function, because the notation:
FROM apod, to_tsquery('neutrino|(dark & matter)') query... is really equivalent to:
FROM apod CROSS JOIN LATERAL to_tsquery('neutrino|(dark & matter)') AS queryThe manual on LATERAL queries:
Table functions appearing in
FROM can also be preceded by the key wordLATERAL, but for functions the key word is optional; the function'sarguments can contain references to columns provided by preceding FROM
items in any case.
Bold emphasis mine.
The keyword
AS is completely optional noise before table aliases (as opposed to column aliases, where it's recommended not to omit AS to avoid possible ambiguities). See:- Date column arithmetic in PostgreSQL query
Code Snippets
FROM apod, to_tsquery('neutrino|(dark & matter)') queryFROM apod CROSS JOIN LATERAL to_tsquery('neutrino|(dark & matter)') AS queryContext
StackExchange Database Administrators Q#167201, answer score: 14
Revisions (0)
No revisions yet.