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
- SELECT order_id, product_id, unit_price
- FROM order_items
- WHERE (unit_price, order_id) =
- (SELECT MAX(unit_price), order_id
- FROM order_items
- GROUP BY order_id);
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

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):
- SELECT order_id, product_id, unit_price
- FROM order_items
- WHERE (unit_price, order_id) =
- (SELECT MAX(unit_price), order_id
- FROM order_items
- GROUP BY order_id);
What’s wrong with this query?
The subquery returns multiple rows — one
(MAX(unit_price), order_id)pair perORDER_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 =:
- SELECT order_id, product_id, unit_price
- FROM order_items
- WHERE (order_id, unit_price) IN
- (SELECT order_id, MAX(unit_price)
- FROM order_items
- GROUP BY order_id);
Note: You must match the column order in the WHERE clause and subquery.
Evaluate the options:
Remove the GROUP BY clause from the subquery and place it in the main query
Incorrect. You need the
GROUP BYin the subquery to getMAX(unit_price)perorder_id.
Replace = with the >ANY operator
Incorrect. That would give you rows with
unit_pricegreater than any max — not what’s asked.
Replace = with the >ALL operator
Incorrect. That gives you rows greater than all max prices, also not what’s asked.
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.
