← Back to SQL, page 1

sql-0008 · Medium mid

How do single quotes and double quotes differ in SQL, and what surprising thing does SQLite do with double quotes?

Single quotes mark a string value; double quotes mark an identifier such as a column or table name. SQLite, for backward compatibility, treats a double-quoted word as a string if no column of that name exists. That hides typos: a misspelled column becomes a constant string instead of an error, while PostgreSQL raises an error.

Full answer

In standard SQL the rule is simple:

  • 'Pune' with single quotes is a string value.
  • "city" with double quotes is an identifier, a name of a column, table or alias. Quoting lets an identifier contain spaces, keep its exact case, or use a reserved word.

The bug usually looks like a filter written with the wrong quotes:

sql
CREATE TABLE students (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  city TEXT,
  year INTEGER
);
CREATE TABLE courses (
  id      INTEGER PRIMARY KEY,
  title   TEXT NOT NULL,
  credits INTEGER
);
CREATE TABLE enrolments (
  student_id INTEGER REFERENCES students(id),
  course_id  INTEGER REFERENCES courses(id),
  marks      INTEGER,
  PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
  (1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
  (3,10,64),(3,20,71),(3,30,80),(5,20,55);
sql
SELECT name FROM students WHERE city = "Leeds" ORDER BY name;
name
Ben
Eli

SQLite returns Ben and Eli, so the query looks correct. It only works by accident. SQLite looked for a column named Leeds, found none, and quietly treated "Leeds" as the string 'Leeds'. PostgreSQL does not guess:

sql
SELECT name FROM students WHERE city = "Leeds";  -- error on purpose
text
Error: column "Leeds" does not exist

The accident breaks as soon as the quoted word happens to be a real column name. Here "city" refers to the column itself, so the condition compares each city with itself and is true for every row that has a city:

sql
SELECT name FROM students WHERE "city" = "city" ORDER BY name;
name
Asha
Ben
Chen
Eli

The typo case is worse. A misspelled column in double quotes becomes a constant string and the query runs:

sql
SELECT name, "citty" AS city FROM students ORDER BY name LIMIT 2;
namecity
Ashacitty
Bencitty

PostgreSQL rejects the same typo, which is what you want:

sql
SELECT name, "citty" AS city FROM students;  -- error on purpose
text
Error: column "citty" does not exist

Use single quotes for values, double quotes only for names, and turn off this compatibility behaviour where you can (SQLite builds can disable it with the SQLITE_DQS compile option).

sql
CREATE TABLE students (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  city TEXT,
  year INTEGER
);
CREATE TABLE courses (
  id      INTEGER PRIMARY KEY,
  title   TEXT NOT NULL,
  credits INTEGER
);
CREATE TABLE enrolments (
  student_id INTEGER REFERENCES students(id),
  course_id  INTEGER REFERENCES courses(id),
  marks      INTEGER,
  PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
  (1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
  (3,10,64),(3,20,71),(3,30,80),(5,20,55);

SELECT name FROM students WHERE city = "Leeds" ORDER BY name;

SELECT name FROM students WHERE "city" = "city" ORDER BY name;

SELECT name, "citty" AS city FROM students ORDER BY name LIMIT 2;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • Single quotes: string literals
  • Double quotes: identifiers (columns, tables, aliases)
  • SQLite falls back to treating an unknown double-quoted name as a string
  • That fallback turns typos into silent constant values

Common mistakes

  • Writing WHERE name = "Asha" and getting a match on SQLite that fails elsewhere
  • Trusting SQLite to report a misspelled double-quoted column

Follow-ups

  • How do you include a single quote inside a string literal?
  • Why might a team quote every identifier, and what does that do to case sensitivity in PostgreSQL?

Tips

  • State the standard rule first, then the SQLite exception
  • Mention that this is a reason to test queries on the production engine