Concatenating with ||
Part of the Beyond the Basics section of Coddy's SQL journey — lesson 10 of 27.
SQLite glues strings together with the || operator (not the + you'd use in JavaScript or Python).
SELECT first_name || ' ' || last_name AS full_name
FROM usersYou can mix in literals, columns, and the results of other string functions. If any operand is NULL the whole expression becomes NULL: wrap nullable columns in COALESCE(col, '') when you want them to act as blank.
Challenge
BeginnerAvailable tables and columns:
<strong>products</strong>:<strong>id</strong>,<strong>brand</strong>,<strong>model</strong>,<strong>price</strong>
Return id and a single column label formatted as '<BRAND> <model> - '<BRAND> <model> - $<price>'lt;price>'. The brand should be in upper case; the rest exactly as stored. Order by id.
Example: 'APPLE iPhone - $999'.
Cheat sheet
In SQLite, strings are concatenated with ||. Mix columns, literals, and functions freely:
SELECT first_name || ' ' || last_name AS full_name
FROM usersIf any operand is NULL, the whole expression becomes NULL. Use COALESCE(col, '') to treat nullable columns as blank:
SELECT COALESCE(middle_name, '') || ' ' || last_name AS name
FROM usersTry it yourself
SELECT id,
-- assemble label with ||
FROM products
ORDER BY id
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Beyond the Basics
2String Functions
LENGTH, UPPER, LOWERSUBSTRINSTRREPLACE and TRIMConcatenating with ||Recap - Invoices