Before you start, you need
Use aliases and create calculated columns · Work correctly with NULL values · Combine tables and keep control of the result rows · Transform text with functions
Join several values into a single text
In a report you rarely want the name and the city in separate columns: you want to read Amy Wilson (London). In SQL, text is joined with ||, the concatenation operator. It works the same way with columns and with fixed text.
name || ' (' || city || ')' joins the name, a space and a parenthesis, the city and the closing parenthesis. Fixed text goes in single quotes; columns go without quotes. Add AS to name the new column, for example AS customer.
Watch the spaces and punctuation: name || city produces Amy TorresMadrid. Every space is text you choose to write. You can also join numbers: 'Stock: ' || stock converts them to text without changing the saved data.
If one of the parts is NULL, the whole result is NULL. name || ' <' || email || '>' shows nothing for customers with no email. COALESCE(email, 'no email') replaces the missing value with a text before joining; you already used it with sums in the JOIN lesson.
SELECT column_1 || ' text ' || column_2 AS label
FROM table_name;See it in action
We want a readable label with each customer's name and city.
SELECT name || ' (' || city || ')' AS customer
FROM customers;| customer |
|---|
| Amy Wilson (London) |
| Liam Martin (Manchester) |
| Megan Reed (London) |
| David Clark (Bristol) |
| Sara Green (Leeds) |
| Peter Hill (London) |
| Laura Ward (Bristol) |
| Hugh Miller (Manchester) |
- For each customer, four pieces are joined: their name, a space and a parenthesis, their city and the closing parenthesis.
AS customernames the new column. The customers table still stores name and city separately.- Spaces matter: without the space before the parenthesis, you would see Amy Wilson(London).
- If a column were NULL, the whole label would be NULL. To avoid that, you use COALESCE, as you will see in the second practice task.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Join name and city
Write a query that returns a column called customer with each customer's name and city, in this format: Amy Wilson (London).
Expected result: 12 rows with the column customer, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
You need to join three pieces of text: the name, an opening parenthesis with the city, and the closing parenthesis.
Use || between each piece. Fixed text goes in single quotes; columns go without quotes.
Structure: name || ' (' || city || ')' AS customer.
Solution explained
SELECT name || ' (' || city || ')' AS customer FROM customers;The fixed texts ' (' and ')' are joined with the name and city columns. The stored data does not change: only a new column is built.
Stop NULL from spreading
Guided practice
Stop NULL from spreading
Return a column called contact in the format name <email>. If the customer has no email, show the text no email instead. Example: David Clark <no email>.
Expected result: 12 rows with the column contact, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Try the join without COALESCE first: what appears for customers with no email?
Replace email with COALESCE(email, 'no email') inside the join.
Structure: name || ' <' || COALESCE(email, 'no email') || '>' AS contact.
Solution explained
SELECT name || ' <' || COALESCE(email, 'no email') || '>' AS contact FROM customers;COALESCE returns the email if there is one, and the replacement text if it is missing. That way the joined result never becomes NULL.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Mix text and numbers
Return a column called label with each product's name and stock, in this format: Mouse: 18 units.
Expected result: 9 rows with the column label, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
The question changes, but the shape is the same: column, fixed text, column, fixed text.
Stock is a number, but || joins it as text without any problem.
Structure: name || ': ' || stock || ' units' AS label.
Solution explained
SELECT name || ': ' || stock || ' units' AS label FROM products;A number can also be joined with ||: SQL converts it to text in the result only. The spaces and the colon are fixed text.
Key takeaways
- || joins text, and also numbers converted to text.
- Fixed text takes single quotes; columns do not.
- A single NULL spreads to the whole result: use COALESCE.
- Spaces and punctuation are part of the text you write.
Go deeper
In SQLite and PostgreSQL || is the standard operator. MySQL uses CONCAT(...) and SQL Server uses + or CONCAT(...). Check the syntax for the engine you use.