Skip to content
C

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' or DATE '2026-09-14'; the exact accepted formats and whether a leading DATE keyword is required varies by dialect.
  • Boolean literals: TRUE / FALSE in PostgreSQL and MySQL; SQL Server has no true boolean type and typically uses 1/0 instead.
  • 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 named students, 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.

Mock Test

  • Literals - Quick Test

    8 questions on Literals.

    8 questions · 8 min · Medium
    Start Mock Test