← Back to SQL, page 16

sql-0312 · Medium mid

What is the result of SELECT 1 = 1.0, '1' = 1, and 'a' = 'A' in SQLite?

1 = 1.0 is true, because integer and real values compare numerically. '1' = 1 is false for two literals, because a text value never equals a number when no column affinity applies. 'a' = 'A' is false under the default BINARY collation.

Full answer
sql
SELECT 1 = 1.0 AS int_real, '1' = 1 AS text_int, 'a' = 'A' AS case_cmp, 'a' = 'A' COLLATE NOCASE AS nocase_cmp;
int_realtext_intcase_cmpnocase_cmp
1001

SQLite compares across storage classes by class first, so text and integer are never equal unless an affinity conversion happens, as it would when one side is an INTEGER column.

PostgreSQL resolves '1' = 1 by casting the untyped literal to integer, so it returns true (not run here); with a text column instead of a literal, it would raise a type error. These differences are why mixed-type comparisons are best avoided.

sql
SELECT 1 = 1.0 AS int_real, '1' = 1 AS text_int, 'a' = 'A' AS case_cmp, 'a' = 'A' COLLATE NOCASE AS nocase_cmp;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Integer and real compare numerically
  • Text and integer literals are not equal in SQLite
  • Column affinity can change that result
  • Default collation is case-sensitive

Common mistakes

  • Comparing numbers stored as text with numeric literals
  • Expecting case-insensitive equality by default

Follow-ups

  • What does PostgreSQL return for '1' = 1?
  • How does an INTEGER column change '1' = col?

Tips

  • Mention the affinity exception