Search code examples
sqlpostgresqlplpgsqlambiguity

why is postgresql considering this subquery column-name not fully qualified and ambiguous


  Select distinct A.col1, B.col2, col3
   from A inner join B on A.id = B.id
   and B.id in 

       (select distinct col2 from B where ..... )

PostgreSQL's plpgsql parser does not like the unqualified "col2" in the parenthetic subquery. Since there's only one table mentioned in the subquery, isn't the parser manufacturing an ambiguity where there is none?


Solution

  • An SQL query like you display would just work.

    Since you mention plpgsql, my shot in the dark is that you are only showing fragments from what is actually a plpgsql function and you run into naming conflicts with function parameters, which are visible anywhere in the function body (except dynamic SQL).