'Super trivial postgreSQL question (alias)

This is what I did

select f.visits 
from pls_fy2014_pupld14a pfpa as f

and I got:

SQL Error [42601]: ERROR: syntax error at or near "AS"

This case below is working but I don't get why I cannot use 'as'

select visits 
from pls_fy2014_pupld14a pfpa


Solution 1:[1]

Aliases always have to be put directly after the table or column name, so in your case:

SELECT pfpa.visits AS f
FROM pls_fy2014_pupld14a pfpa

Solution 2:[2]

In your second query:

select visits
from pls_fy2014_pupld14a pfpa;

the pfpa appearing after the table name is an alias. Note that we can only alias a table once in SQL. In your second query:

select f.visits
from pls_fy2014_pupld14a pfpa as f

you attempting to alias pfpa a second time as f. You can't do that.

Solution 3:[3]

You are - probably - trying to alias the table twice. AS is not mandatory, so

SELECT pfpa.visits
FROM pls_fy2014_pupld14a pfpa

is the same as:

SELECT pfpa.visits
FROM pls_fy2014_pupld14a AS pfpa

You can not add another alias f

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jonas Metzler
Solution 2 Tim Biegeleisen
Solution 3 Lennart