Module 5 · Advanced queries

Join text values into a readable column

By the end you will be able to join several values into a single text with ||.

About 12 min

0 of 52 lessons

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.

Minimal syntax
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.

SQL
SELECT name || ' (' || city || ')' AS customer
FROM customers;
Example result: 8 of 12 rows
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)
  1. For each customer, four pieces are joined: their name, a space and a parenthesis, their city and the closing parenthesis.
  2. AS customer names the new column. The customers table still stores name and city separately.
  3. Spaces matter: without the space before the parenthesis, you would see Amy Wilson(London).
  4. 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

Not started

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.

Stop NULL from spreading

Guided practice

Stop NULL from spreading

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Mix text and numbers

Not started

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.

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.

Next: Calculate and query dates

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.