sqlpostgresqlstring-constant

Column doens't exists in PostgreSQL (WHERE column_name = column_value)


I have the following table in PostgreSQL:

enter image description here

id and grade are INTs, note and subject both VARCHARs

When I run the command:

SELECT * FROM grades 
WHERE subject = "latin";

I get the following error:

In pgAdmin4: ERROR: column "latin" does not exist LINE 2: WHERE subject = "latin" ^ SQL state: 42703 Character: 37

And in cmd: ERROR: column "latin" does not exist LINE 1: SELECT * FROM upisi WHERE subject = "latin";

I'm coming from MySQL so I thought this would work. Works fine if I put grade = something in the WHERE clause. Any idea why this might be the case?


Solution

  • It's as simple as the wrong type of quote marks. You wanted:

    SELECT * FROM grades 
    WHERE subject = 'latin';
    

    To explain:

    Although double quotes are in the SQL standard, other DBMSes use different syntax for quoting identifiers, so may treat double quotes as an alternative for writing strings.

    -- Postgres (also works in Microsoft SQL Server, but isn't the default style)
    SELECT * FROM grades WHERE "subject name" = 'latin'
    -- MySQL
    SELECT * FROM grades WHERE `subject name` = 'latin'
    -- Microsoft SQL Server
    SELECT * FROM grades WHERE [subject name] = 'latin'
    

    But if you always use single quotes for strings, and avoid names that need quoting, you'll run into fewer problems.

    -- Works pretty much everywhere
    SELECT * FROM grades WHERE subject = 'latin'