user33276346
user33276346

Reputation: 1739

What is an alias with parentheses (something like "FROM ... a(m)")?

Someone answered to a question I have about generating a table with random values using existing tables (still struggling with it). And the answer has something like this:

select uuid_generate_v4(), a."Account", tns."Name", tvs."Value"
from generate_series(1, 10) x(rn)
left join (
    select "Account", row_number() over(order by random()) rn from "AccountSamples"
) a on a.rn = x.rn

I don't understand the x(rn) part, it seems like an alias but I don't understand why does it have an "rn" inside of the parenthesis, and the parenthesis itself is new to me, is rn another alias or a special keyword?

Upvotes: 2

Views: 586

Answers (1)

Brits
Brits

Reputation: 18370

This is a column alias. I think the simplest way to explain this is with two queries:

select *
from generate_series(1, 10) x(rn);

select *
from generate_series(1, 10) x(foo);

Both will generate a table with the alias x but the first one will have a single column named rn while the column in the second one will be named foo. See this db fiddle.

You could remove the (rn) but then the column would need to be referenced as x.x which is likely to be confusing.

Upvotes: 6

Related Questions