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_real | text_int | case_cmp | nocase_cmp |
|---|---|---|---|
| 1 | 0 | 0 | 1 |
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