I am looking at an example of correlated subquery with aliases from a PostgreSQL book:
bpsimple=# SELECT * FROM orderinfo o,
bpsimple=# (SELECT * FROM customer c WHERE town = 'Bingham') c
bpsimple=# WHERE c.customer_id = o.customer_id;
Why is the c
alias used twice here? Isn't the one inside the parenthesis enough?
Best Answer
As @ypercube already explained, the subquery has no reference to columns in the outer query, it can be processed independently. So it is not a "correlated subquery". Some call that a "derived table", or just "subquery".
As to your questions:
c1
is a table alias forcustomer
in the subquery, short forcustomer AS c1
. The key wordAS
has been omitted which is fine since it would be just noise for a table alias. I quote the manual on "Omitting the AS Key Word":The example probably goes to demonstrate visibility: only the outer table alias is visible in the outer
WHERE
clause, so there is no naming conflict withc
used twice.Otherwise,
c1
is useless here since nothing refers to it. You can just drop it.c2
is another table alias for the "derived table". This one is mandatory since Postgres requires a name for every used table, and a subquery has none until you name it.The only difference:
customer_id
is listed once instead of twice in the result (due to theUSING
clause), which would be preferable since it is completely redundant in this case.Details in the manual about
SELECT
.