Oracle Database Sql Certified Associate · Free Practice Question Hard
Question 23
View and examine the following SQL exhibit.
Exhibit: 1
- INSERT ALL
- WHEN order_total < 10000 THEN
- INTO small_orders
- WHEN order_total > 10000 AND order_total < 20000 THEN
- INTO medium_orders
- WHEN order_total > 20000 THEN
- INTO large_orders
- SELECT order_id, order_total, customer_id
- FROM orders;
Identify the true statement regarding the evaluation of rows returned by the subquery in the INSERT statement.
-
A
The insert statement would give an error because the ELSE clause is not present for support in case none of WHEN clauses are true.
-
B
They are evaluated by the first WHEN clause. If the condition is true, then the row would not be evaluated by the subsequent WHEN clauses.
-
C
They are evaluated by all the three WHEN clauses regardless of the results of the evaluation of any other WHEN clause.
-
D
They are evaluated by the first WHEN clause. If the condition is false, then the row would be evaluated by the subsequent WHEN clauses.
Reveal correct answer
Correct answer: C
Explanation
Correct option: They are evaluated by all the three WHEN clauses regardless of the results of the evaluation of any other WHEN clause.
Breakdown of the Query
1. INSERT ALL
This is an Oracle-specific syntax used to insert rows into multiple tables based on conditions.
It's often referred to as conditional multitable insert.
It’s different from a regular
INSERT INTObecause it can conditionally insert into different tables usingWHEN ... THEN.
2. WHEN ... THEN INTO ...
These clauses define the conditions that determine which table the row should be inserted into.
WHEN order_total < 10000 THEN INTO small_orders
→ If theorder_totalis less than 10,000, insert the row into thesmall_orderstable.WHEN order_total > 10000 AND order_total < 20000 THEN INTO medium_orders
→ If theorder_totalis between 10,001 and 19,999, insert it intomedium_orders.WHEN order_total > 20000 THEN INTO large_orders
→ If theorder_totalis greater than 20,000, insert intolarge_orders.
Note: There’s no ELSE clause, so if a row doesn’t match any condition, it is not inserted anywhere.
3. SELECT ... FROM orders
This part is a subquery that retrieves the rows to evaluate against the
WHENconditions.It selects:
order_idorder_totalcustomer_id
These are the columns that will be inserted into the matching table(s), depending on the
WHENclauses.
The SQL statement in Exhibit 1 uses Oracle's INSERT ALL syntax with conditional WHEN clauses, which is used for conditional multitable inserts.
How Conditional INSERT ALL Works in Oracle:
Each row returned by the
SELECTis evaluated independently against eachWHENcondition.The evaluation is not exclusive (i.e., it's not like
IF...ELSE IF...ELSElogic).A row may match multiple
WHENconditions (if allowed by the logic).Oracle evaluates all
WHENclauses independently, and multiple inserts can happen for the same row if multiple conditions are true.
Why the Others Are Incorrect:
"Evaluated by the first WHEN clause only" → False: Oracle doesn't stop after the first match.
"Else clause is required" → False: The
ELSEclause is optional; if noWHENmatches, the row is just not inserted."Evaluated only if the first fails" → Also incorrect because all
WHENclauses are always considered.
From Oracle Docs: “All WHEN clauses are evaluated for each row returned by the subquery. If more than one WHEN condition evaluates to true, then multiple INTO clauses can be executed for the same row.”
The Oracle WHEN keyword is used in two ways. One use is in the CASE statement to pick among given values. The other use of WHEN is in a conditional INSERT statement.
Using WHEN in the CASE statement
The CASE statement operates like a series of IF statements, only using the key word WHEN.
Example Usage:
An example of WHEN used in a CASE statement that reports a temperature range:
- CASE
- WHEN n_temperature = 10 THEN v_status := 'very low';
- WHEN n_temperature < 20 THEN v_status := 'low';
- WHEN n_temperature = 50 THEN v_status := 'medium';
- WHEN n_temperature > 80 THEN v_status := 'high';
- ELSE v_status := 'very high';
- END CASE;
Using WHEN in a conditional INSERT statement
There are two modes for conditional insert: ALL and FIRST.
If you specify ALL, the default value, then the database evaluates each WHEN clause regardless of the results of the evaluation of any other WHEN clause. For each WHEN clause whose condition evaluates to true, the database executes the corresponding INTO clause list.
√ They are evaluated by all the three WHEN clauses regardless of the results of the evaluation of any other WHEN clause.
If you specify FIRST, then the database evaluates each WHEN clause in the order in which it appears in the statement. For the first WHEN clause that evaluates to true, the database executes the corresponding INTO clause and skips subsequent WHEN clauses for the given row.
Example Usage:
- INSERT
- WHEN MOD(deptno,2)=0 THEN
- INTO even_employees (empno, ename)
- VALUES (empno, ename)
- WHEN MOD(deptno,2)=1 THEN
- INTO uneven_employees (empno, ename)
- VALUES (empno, ename)
- ELSE
- INTO unknow_employees (empno, ename)
- VALUES (empno, ename)
- SELECT empno, ename, deptno FROM emp;
A. The assertion that the INSERT statement would give an error because the ELSE clause is not present for support in case none of the WHEN clauses are true is not applicable in this context. The absence of an ELSE clause does not necessarily result in an error, as the INSERT statement can still be executed based on the conditions specified in the WHEN clauses.
B. The statement that the rows are evaluated by the first WHEN clause, and if the condition is true, then the row would not be evaluated by the subsequent WHEN clauses, is incorrect. In the SQL exhibit, all three WHEN clauses are evaluated for each row returned by the subquery, regardless of the result of the evaluation of any other WHEN clause.
C. In the provided SQL exhibit, the rows returned by the subquery in the INSERT statement are evaluated by all three WHEN clauses, regardless of the results of the evaluation of any other WHEN clause. This means that each row will be checked against all three conditions specified in the WHEN clauses.
D. The statement that the rows are evaluated by the first WHEN clause, and if the condition is false, then the row would be evaluated by the subsequent WHEN clauses, is incorrect. In the provided SQL exhibit, all three WHEN clauses are evaluated for each row returned by the subquery, regardless of the outcome of the evaluation of any other WHEN clause.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
