0

Using the following code:

SELECT * FROM users u
JOIN sales s ON u.id = s.user_id
WHERE u.name = "Guest" AND s.removed = false;

Postgres throws an error saying column "Guest" does not exist, even if I put name in quotes, and yet if I remove that clause and only keep the s.remove = false clause the query compiles. Is there a reason why it can't find a column that is not computed and mentioned in a WHERE clause?

  • 1
    You should use single quotes as 'Guest' – GoonerForLife Dec 09 '21 at 23:26
  • Show us the DDL for the table which contains the "Guest" object/field. though @GoonerForLife is likely dead on. Single quotes for values, double quotes for objects. If you're wanting to only return u.names that are equal to 'Guest' then single quotes. If you're actually tying to find u.names which match a "computed column" called "Guest" Spelled case sensitive that way, then what you have is right and your DDL needs to be reviewed for that table. – xQbert Dec 09 '21 at 23:31
  • Are you sure there's a "Guest" column then? Or should name equal the string 'Guest'? – LukStorms Dec 09 '21 at 23:35
  • if you meant 'guest' string , you should use single quotes – eshirvana Dec 09 '21 at 23:50

1 Answers1

0

"Guest" is a column that does not exist.

'Guest' is a string

You want:

SELECT * FROM users u
JOIN sales s ON u.id = s.user_id
WHERE u.name = 'Guest' AND s.removed = false;

although really you should specify column names and not use *, since adding or removing columns will give you hard to diagnose problems in the future.

Terry Carmen
  • 3,720
  • 1
  • 16
  • 32