View the Exhibit and examine the structure of the PRODUCTS table.
You need to generate a report in the following format:
CATEGORIES
5MP Digital Photo Camera’s category is Photo
Y Box’s category is Electronics
Envoy Ambassador’s category is Hardware
Which two queries would give the required output? (Choose two.)
A.
SELECT prod_name || q”’s category is ‘ || prod_category CATEGORIES FROM products;
B.
SELECT prod_name || q'[‘s ]’category is ‘ || prod_category CATEGORIES FROM products;
C.
SELECT prod_name || q’\\’s\\’ || ‘ category is ‘ || prod_category CATEGORIES FROM products;
D.
SELECT prod_name || q'<‘s >’ || ‘category is ‘ || prod_category CATEGORIES FROM products;
Explanation:
So, how are words that contain single quotation marks dealt with? There are essentially two
mechanisms available. The most popular of these is to add an additional single quotation mark
next to each naturally occurring single quotation mark in the character string
Oracle offers a neat way to deal with this type of character literal in the form of the alternative quote
(q) operator. Notice that the problem is that Oracle chose the single quote characters as the special
pair of symbols that enclose or wrap any other character literal. These character-enclosing symbols
could have been anything other than single quotation marks.
Bearing this in mind, consider the alternative quote (q) operator. The q operator enables you to
choose from a set of possible pairs of wrapping symbols for character literals as alternatives to the
single quote symbols. The options are any single-byte or multibyte character or the four brackets:
(round brackets), {curly braces}, [squarebrackets], or <angle brackets>. Using the q operator, the
character delimiter can effectively be changed from a single quotation mark to any other character
The syntax of the alternative quote operator is as follows:
q’delimiter’character literal which may include the single quotes delimiter’ where delimiter can be
any character or bracket.
Alternative Quote (q) Operator
Specify your own quotation mark delimiter.
Select any delimiter.
Increase readability and usability.
SELECT department_name || q'[ Department’s Manager Id: ]’
|| manager_idAS “Department and Manager”
FROM departments;
Alternative Quote (q) Operator
Many SQL statements use character literals in expressions or conditions. If the literal itself contains
a single quotation mark, you can use the quote (q) operator and select your own quotation mark
delimiter.
You can choose any convenient delimiter, single-byte or multibyte, or any of the following character
pairs: [ ], { }, ( ), or < >.
In the example shown, the string contains a single quotation mark, which is normally interpreted as
a delimiter of a character string. By using the q operator, however, brackets [] are used as the
quotation mark delimiters. The string between the brackets delimiters is interpreted as a literal
character string.
Given the explanation and the following test:
SELECT q”’s category is’ FROM DUAL;
SELECT q'[‘s category is’]’ FROM DUAL;
SELECT q’\\’s category is’\\’ FROM DUAL;
SELECT q” FROM DUAL;
Wouldn’t the correct answer be B and D?
Correct answers are B and D.