Full answer
sql
SELECT TRUE AS t, FALSE AS f, TRUE + TRUE AS two, 5 > 3 AS cmp, typeof(5 > 3) AS cmp_type;| t | f | two | cmp | cmp_type |
|---|---|---|---|---|
| 1 | 0 | 2 | 1 | integer |
In SQLite, TRUE is simply 1, so TRUE + TRUE is 2 and a comparison like 5 > 3 produces the integer 1.
PostgreSQL keeps booleans separate:
sql
SELECT TRUE AS t, 5 > 3 AS cmp, pg_typeof(5 > 3) AS cmp_type;| t | cmp | cmp_type |
|---|---|---|
| true | true | boolean |
sql
SELECT TRUE + TRUE; -- error on purposetext
Error: operator does not exist: boolean + booleanA portable SQLite schema can restrict a flag column with CHECK (active IN (0, 1)), otherwise any value can be stored. Summing a flag (SUM(active)) counts true rows in SQLite; PostgreSQL needs COUNT(*) FILTER (WHERE active) or SUM(active::int).
sql
SELECT TRUE AS t, FALSE AS f, TRUE + TRUE AS two, 5 > 3 AS cmp, typeof(5 > 3) AS cmp_type;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- SQLite TRUE and FALSE are 1 and 0
- Comparisons in SQLite return integers
- PostgreSQL has a strict boolean type
- Constrain SQLite flag columns with CHECK
Common mistakes
- Adding booleans in PostgreSQL without a cast
- Storing 'Y', 'yes' and 1 in the same SQLite flag column
Follow-ups
- How would you count active users on each engine?
- How does NULL behave in a boolean column?
Tips
- Show TRUE + TRUE on SQLite