Challenge - 5 Problems
Concatenation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Concatenate two columns with || operator
Given a table users with columns
first_name and last_name, what is the output of this query?SELECT first_name || ' ' || last_name AS full_name FROM users WHERE id = 1;
PostgreSQL
CREATE TABLE users (id INT, first_name TEXT, last_name TEXT); INSERT INTO users VALUES (1, 'Alice', 'Smith');
Attempts:
2 left
💡 Hint
The || operator joins strings exactly as written, including spaces.
✗ Incorrect
The || operator concatenates strings. Here, it joins first_name, a space, and last_name to form 'Alice Smith'.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in concatenation
Which option will cause a syntax error when trying to concatenate two strings in PostgreSQL?
Attempts:
2 left
💡 Hint
PostgreSQL does not use + for string concatenation.
✗ Incorrect
In PostgreSQL, the + operator is not valid for strings and causes a syntax error. The || operator is used instead.
❓ query_result
advanced2:00remaining
Concatenate with NULL values
What is the output of this query if
middle_name is NULL?SELECT first_name || ' ' || middle_name || ' ' || last_name AS full_name FROM users WHERE id = 2;
PostgreSQL
CREATE TABLE users (id INT, first_name TEXT, middle_name TEXT, last_name TEXT); INSERT INTO users VALUES (2, 'John', NULL, 'Doe');
Attempts:
2 left
💡 Hint
In PostgreSQL, concatenating with NULL results in NULL.
✗ Incorrect
The || operator returns NULL if any operand is NULL. So the entire concatenation is NULL.
❓ query_result
advanced2:00remaining
Concatenate with COALESCE to handle NULL
How to modify the query to show full name even if
middle_name is NULL?SELECT first_name || ' ' || COALESCE(middle_name, '') || ' ' || last_name AS full_name FROM users WHERE id = 2;
PostgreSQL
CREATE TABLE users (id INT, first_name TEXT, middle_name TEXT, last_name TEXT); INSERT INTO users VALUES (2, 'John', NULL, 'Doe');
Attempts:
2 left
💡 Hint
COALESCE replaces NULL with the given value.
✗ Incorrect
COALESCE(middle_name, '') replaces NULL with empty string, so concatenation works and shows two spaces between first and last name.
🧠 Conceptual
expert2:00remaining
Why does concatenation with || return NULL if any operand is NULL?
In PostgreSQL, why does the || operator return NULL if any string operand is NULL?
Attempts:
2 left
💡 Hint
Think about what NULL means in databases.
✗ Incorrect
NULL means unknown or missing value. Concatenating with unknown results in unknown, so the result is NULL.