Perdue
Perdue

Reputation: 479

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!

Upvotes: 0

Views: 4749

Answers (1)

Haleemur Ali
Haleemur Ali

Reputation: 28313

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"

Upvotes: 1

Related Questions