Oracle Database SQL (1Z0-071) free question 5 / 10 · Domain 2: Retrieving Data using the SQL SELECT Statement
What is the result of running the following SELECT statement? SELECT 'Oracle' || NULL || 'SQL' FROM dual;
- An error is raised at run time
- The joined string OracleSQL
- The string Oracle, without SQL
- NULL, shown as an empty result
Show the answer and explanation
Answer: B
Why this is correct
"Concatenation Operator" in the SQL Language Reference says that a zero-length character string is treated as NULL, but concatenating a zero-length character string with another operand always results in the other operand, and that a NULL results only from the concatenation of two null strings. 'Oracle' || NULL gives Oracle, and concatenating 'SQL' onto it gives OracleSQL.
Why the other options are wrong
"The string Oracle, without SQL" … || concatenates from left to right, so 'SQL' is also concatenated onto the result of the first concatenation, Oracle. The trailing string does not disappear.
"NULL, shown as an empty result" … When one operand of a concatenation is NULL, the NULL side is ignored and the other value is the result. The result is NULL only when both operands are NULL.
"An error is raised at run time" … Writing NULL as an operand of the concatenation operator is not a syntax error. Concatenating with NULL yields the other operand.
Key takeaway
When you concatenate an expression that can be NULL, converting it explicitly to a zero-length string with NVL keeps your SQL independent of future changes in behavior. This document itself notes that the treatment of NULL and zero-length strings may change in a future release.