Oracle Database Sql Certified Associate · Free Practice Question Medium

Question 20

View and examine the structure of the ORDER_ITEMS table and the exhibit.

Required: Display the PRODUCT_ID of the product that has the highest UNIT_PRICE per ORDER_ID.

The following SQL query has been executed.

Exhibit: 1


Indicate the correction which should be made in the above SQL statement to achieve this?

  • A

    Replace = with the >ALL operator

  • B

    Replace = with the >ANY operator

  • C

    Replace = with the IN operator

  • D

    Remove the GROUP BY clause from the subquery and place it in the main query

Reveal correct answer

Correct answer: C

Explanation

Requirement:

Display the PRODUCT_ID of the product that has the highest UNIT_PRICE per ORDER_ID.

This means for each ORDER_ID, we want the product with the maximum UNIT_PRICE.

Given SQL Query (Exhibit 1):


What’s wrong with this query?

  • The subquery returns multiple rows — one (MAX(unit_price), order_id) pair per ORDER_ID.

  • The main query uses =, which only works with a subquery returning a single row.

  • Using = with a multi-row subquery causes an error.

Correct Fix:

To compare a tuple against multiple values, use the IN operator instead of =:

Note: You must match the column order in the WHERE clause and subquery.

Evaluate the options:

  1. Remove the GROUP BY clause from the subquery and place it in the main query

    • Incorrect. You need the GROUP BY in the subquery to get MAX(unit_price) per order_id.

  2. Replace = with the >ANY operator

    • Incorrect. That would give you rows with unit_price greater than any max — not what’s asked.

  3. Replace = with the >ALL operator

    • Incorrect. That gives you rows greater than all max prices, also not what’s asked.

  4. Replace = with the IN operator

    • Correct. This allows comparison with multiple rows, which is what the subquery returns.

Note: When evaluating the tables, the Primary and Foreign Keys are colour coded and the table with the 3 prong connector is the upper level. There may be more than one Primary Key per table and the Foreign Key to a given table may be the linked to a lower level Foreign Key.

A. Replacing the = operator with the >ALL operator would not be suitable for this scenario as it is used to compare a value to all values in a list, not for finding the highest UNIT_PRICE per ORDER_ID.

B. Replacing the = operator with the >ANY operator would not be appropriate in this context as it is used for comparisons with a single value, not for identifying the highest UNIT_PRICE per ORDER_ID.

C. Replacing the = operator with the IN operator would be the correct correction to make in this SQL statement to achieve the desired result of displaying the PRODUCT_ID of the product with the highest UNIT_PRICE per ORDER_ID. The IN operator allows for comparing a value to a list of values, which is necessary in this case to identify the highest UNIT_PRICE.

D. Removing the GROUP BY clause from the subquery and placing it in the main query would result in the correct aggregation of data by ORDER_ID, allowing the query to correctly identify the PRODUCT_ID with the highest UNIT_PRICE per ORDER_ID.

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