What is the difference between single quotes and double quotes in PostgreSQL?

SqlPostgresql

Sql Problem Overview


I am new to PostgresSQL.I tried

select * from employee where employee_name="elina";

But that results error as follows:

ERROR: column "elina" does not exist.

Then I tried by replacing double quotes with single quotes as follows:

select * from employee where employee_name='elina';

It result fine..So what is the difference between single quotes and double quotes in postgresql.If we can't use double quotes in postgres query,then if any other use for this double quotes in postgreSQL?

Sql Solutions


Solution 1 - Sql

Double quotes are for names of tables or fields. Sometimes You can omit them. The single quotes are for string constants. This is the SQL standard. In the verbose form, your query looks like this:

select * from "employee" where "employee_name"='elina';

Solution 2 - Sql

As explained in the PostgreSQL manual:

> A string constant in SQL is an arbitrary sequence of characters bounded by single quotes ('), for example 'This is a string'. To include a single-quote character within a string constant, write two adjacent single quotes, e.g., 'Dianne''s horse'. Note that this is not the same as a double-quote character (").

Elsewhere on the same page:

> There is a second kind of identifier: the delimited identifier or quoted identifier. It is formed by enclosing an arbitrary sequence of characters in double-quotes ("). A delimited identifier is always an identifier, never a key word. So "select" could be used to refer to a column or table named "select", whereas an unquoted select would be taken as a key word and would therefore provoke a parse error when used where a table or column name is expected.

TL;DR: Single quotes for string constants, double quotes for table/column names.

Solution 3 - Sql

Well single quotes are used for string literals and double quotes are used for escaping DB objects like table name / column name etc.

Specifically, double quotes are used for escaping a column/table name if it's resemble to any reserve/key word. Though every RDBMS have their own way of escaping the same (like backtique in MySQL or square bracket in SQL Server) but using double quotes is ANSI standard.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjisnaView Question on Stackoverflow
Solution 1 - SqlMichasView Answer on Stackoverflow
Solution 2 - SqlmelpomeneView Answer on Stackoverflow
Solution 3 - SqlRahulView Answer on Stackoverflow