# Customer Full Name Concat

> First name, last name. Combine them.

Canonical URL: <https://datadriven.io/problems/customer_full_name_concat>

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The customer success team needs a clean roster for their outreach emails. Combine each customer's first and last name into a single full name field, and include their ID and country, listed alphabetically by full name.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests string concatenation and ordering. It screens for the ability to combine columns into a derived string and sort by it.

---

### Break down the requirements

#### Step 1: Concatenate first and last name

Use `first_name || ' ' || last_name` or `CONCAT(first_name, ' ', last_name)` to create the full name.

#### Step 2: Select required columns and sort

Return `customer_id`, full name, and `country`, ordered alphabetically by full name.

---

### The solution

**String concatenation with sort**

```sql
SELECT
    customer_id,
    first_name || ' ' || last_name AS full_name,
    country
FROM customers
ORDER BY full_name
```

> **Cost Analysis**
>
> Scan of 1.5M rows with string concatenation per row and a sort on the derived column. An expression index on `(first_name || ' ' || last_name)` would speed up the sort.

> **Interviewers Watch For**
>
> Whether the candidate uses `||` (ANSI SQL) vs `CONCAT()` (function). Both work, but `||` returns NULL if either operand is NULL, while CONCAT treats NULLs as empty strings. This behavior difference matters.

> **Common Pitfall**
>
> If `first_name` or `last_name` is NULL, the `||` operator returns NULL for the entire expression. Use `COALESCE(first_name, ") || ' ' || COALESCE(last_name, ")` to handle NULLs safely.

---

## Common follow-up questions

- What happens to the full name if last_name is NULL? _(With ||, the entire expression is NULL. Tests NULL-safe concatenation.)_
- What is the difference between || and CONCAT? _(|| propagates NULLs; CONCAT treats NULLs as empty strings. Database-specific.)_
- How would you sort by last name first, then first name? _(ORDER BY last_name, first_name instead of the concatenated string.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/customer_full_name_concat)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.