简体   繁体   中英

Relation Does Not Exist PostgreSQL

I am working on a LeetCode program that I cannot seem to get to work in PostgreSQL. The problem is to find an employee with a salary greater than their manager. Here is the table:

| Id | Name  | Salary | ManagerId |

| 1  | Joe   | 70000  | 3         |

| 2  | Henry | 80000  | 4         |

| 3  | Sam   | 60000  | NULL      |

| 4  | Max   | 90000  | NULL      |

I've struggled with this for a little bit because this is the code that I am trying:

select e.Name as Employee
from Employee as e
,Employee as e2 
inner join e2 on e.Id = e2.Id
and e.Salary > e2.Salary

But I keep getting the error that

ERROR: relation "e2" does not exist.

Can anyone tell me why this is and what I need to do to get this to work?

Thanks!

The proper join syntax should be:

select e."Name" as Employee
from "Employee" as e
inner join employee e2 on e."ManagerId" = e2."Id"
and e."Salary" > e2."Salary"

the table name comes after the inner join , and you need to relate each employee to their manager, so the join condition should be e."ManagerId" = e2."Id"

One last note:

In PostgreSQL, table & column names are case insensitive, unless they are quoted.

so, e.managerid is equivalent to e.ManagerId but not equivalent to e."ManagerId"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM