QuestionQ126

VerifiedUsing Subqueries

View the exhibits and examine the structures of the COSTS and PROMOTIONS tables (Exhibits 1 and 2, which follow the SQL statement shown in the image).

You want to display the PROD_IDs whose promotion cost is less than the highest cost PROD_ID within a promotion time interval.

Examine the following SQL statement:

Question Image

Exhibit 1: Question Image

Exhibit 2: Question Image

What will be the outcome of running this statement?

Explanation

The GROUP BY clause groups PROMOTIONS rows by the numeric result of (promo_end_date - promo_begin_date), which is a legitimate GROUP BY expression, and MAX(promo_cost) is a valid aggregate function to use with it — so no syntax error occurs, and the ALL operator is also used correctly with the comparison operator. The statement therefore executes successfully. However, the inner subquery that computes MAX(promo_cost) grouped by interval length is not correlated to the promo_id or time interval of each row being evaluated in the outer WHERE clause. As a result, promo_cost is compared against the entire set of per-interval maximum costs rather than specifically the maximum cost within that promotion's own time interval, so the query does not correctly implement the intended business requirement of finding PROD_IDs whose cost is less than the highest cost within their specific promotion time interval. The statement thus runs without error but returns results that do not fulfill the stated requirement.

Community Discussion

No comments yet. Be the first to start the discussion!