0
0
PostgreSQLquery~20 mins

Concatenation with || operator in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Concatenation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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');
A[{"full_name": "Alice Smith"}]
B[{"full_name": "AliceSmith"}]
C[{"full_name": "Alice || Smith"}]
DSyntaxError
Attempts:
2 left
💡 Hint
The || operator joins strings exactly as written, including spaces.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in concatenation
Which option will cause a syntax error when trying to concatenate two strings in PostgreSQL?
ASELECT 'Hello' + ' World';
BSELECT 'Hello' || ' ' || 'World';
CSELECT 'Hello' || ' World';
DSELECT 'Hello' || 'World';
Attempts:
2 left
💡 Hint
PostgreSQL does not use + for string concatenation.
query_result
advanced
2: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');
A[{"full_name": "John Doe"}]
B[{"full_name": "John Doe"}]
C[{"full_name": "JohnNULL Doe"}]
D[{"full_name": null}]
Attempts:
2 left
💡 Hint
In PostgreSQL, concatenating with NULL results in NULL.
query_result
advanced
2: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');
A[{"full_name": "John Doe"}]
B[{"full_name": "John Doe"}]
C[{"full_name": "JohnNULL Doe"}]
D[{"full_name": null}]
Attempts:
2 left
💡 Hint
COALESCE replaces NULL with the given value.
🧠 Conceptual
expert
2: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?
ABecause || operator converts NULL to 'NULL' string before concatenation.
BBecause || operator treats NULL as empty string automatically.
CBecause NULL represents unknown, so concatenation with unknown yields unknown (NULL).
DBecause || operator ignores NULL operands and concatenates the rest.
Attempts:
2 left
💡 Hint
Think about what NULL means in databases.