Literals
Literals
Definition
A literal is a fixed value written directly into a SQL statement, as opposed to a value read from a column. 85000, 'Asha', TRUE, and NULL are all literals.
How It Works
The main literal categories:
- Numeric literals:
42,3.14,-7. No quotes. - String literals: single-quoted in standard SQL —
'Asha'. To include an actual apostrophe inside a string, double it:'O''Brien'. (MySQL also accepts double quotes for strings by default, which is a dialect quirk, not the standard.) - Date/time literals: usually a quoted string in a specific format, e.g.
'2026-09-14'orDATE '2026-09-14'; the exact accepted formats and whether a leadingDATEkeyword is required varies by dialect. - Boolean literals:
TRUE/FALSEin PostgreSQL and MySQL; SQL Server has no true boolean type and typically uses1/0instead. - The NULL literal: represents "unknown/absent," and is a category of its own — it behaves differently from every other literal in comparisons (see NULL Semantics).
Edge Cases and Pitfalls
- Using double quotes for a string literal (
"Asha") works in MySQL by default but means something entirely different in standard SQL and PostgreSQL, where double quotes delimit an identifier, not a string —"students"refers to a column/table literally namedstudents, not the text "students". - Forgetting to escape an apostrophe inside a string (writing
'O'Brien'instead of'O''Brien') breaks the statement, because the parser sees the string as ending at the first unescaped quote. - Numeric literals are never quoted; quoting a number (
'42') turns it into a string literal, which most engines will silently convert back when compared to a numeric column, but this implicit conversion can be a source of subtle bugs or index-usage problems in some engines.
Key Takeaways
- Literals are fixed values embedded directly in SQL text: numeric (unquoted), string (single-quoted, apostrophe doubled to escape), date/time (dialect-dependent format), boolean, and NULL.
- Single quotes for strings and double quotes for identifiers is the standard-SQL rule — MySQL's default relaxation of this (allowing double-quoted strings) is a dialect-specific exception.
- NULL is not really "a value" in the normal sense — it needs its own semantics, covered separately.