← Back to SQL, page 25

sql-0483 · Easy fresher

What do '10' + '5', '10' || '5' and 10 || 5 return in SQLite?

They return 15, '105' and '105'. The + operator converts text that looks numeric into numbers and adds them, while || converts its operands to text and concatenates, so the operator, not the operand types, decides the result.

Full answer
sql
SELECT '10' + '5' AS plus_text, '10' || '5' AS concat_text, 10 || 5 AS concat_numbers, typeof(10 || 5) AS concat_type;
plus_textconcat_textconcat_numbersconcat_type
15105105text

SQLite applies numeric conversion for arithmetic, so the quoted values become 10 and 5. Concatenation always yields text, even with integer operands, which the typeof column confirms. PostgreSQL is stricter: '10' + '5' resolves the unknown literals to integers and returns 15, but 10 || 5 fails because || has no integer form unless one side is text (not run here). Relying on implicit conversion makes queries engine-dependent.

sql
SELECT '10' + '5' AS plus_text, '10' || '5' AS concat_text, 10 || 5 AS concat_numbers, typeof(10 || 5) AS concat_type;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • + converts to numbers
  • || converts to text
  • Concatenation returns text
  • PostgreSQL is stricter with ||

Common mistakes

  • Expecting + to concatenate

Follow-ups

  • What does 'abc' + 1 return in SQLite?
  • How would you build a zero-padded code from a number?

Tips

  • Let the operator decide the type