Oracle Database Sql Certified Associate · Free Practice Question Hard

Question 23

View and examine the following SQL exhibit.

Exhibit: 1


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 INTO because it can conditionally insert into different tables using WHEN ... 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 the order_total is less than 10,000, insert the row into the small_orders table.

  • WHEN order_total > 10000 AND order_total < 20000 THEN INTO medium_orders
    → If the order_total is between 10,001 and 19,999, insert it into medium_orders.

  • WHEN order_total > 20000 THEN INTO large_orders
    → If the order_total is greater than 20,000, insert into large_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 WHEN conditions.

  • It selects:

    • order_id

    • order_total

    • customer_id

  • These are the columns that will be inserted into the matching table(s), depending on the WHEN clauses.

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 SELECT is evaluated independently against each WHEN condition.

  • The evaluation is not exclusive (i.e., it's not like IF...ELSE IF...ELSE logic).

  • A row may match multiple WHEN conditions (if allowed by the logic).

  • Oracle evaluates all WHEN clauses 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 ELSE clause is optional; if no WHEN matches, the row is just not inserted.

  • "Evaluated only if the first fails" →  Also incorrect because all WHEN clauses 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:



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:


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.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need