Which SQL statement gives the required output?

Examine the structure of the PRODUCTS table:
name Null Type
PROD_ID NOT NULL NUMBER(4)
PROD_NAME VARCHAR2(20)
PROD_STATUS VARCHAR2(6)
QTY_IN_HAND NUMBER(8,2)
UNIT_PRICE NUMBER(10,2)
You want to display the names of the products that have the highest total value for UNIT_PRICE *
QTY_IN_HAND.
Which SQL statement gives the required output?

A.
SELECT prod_name
FROM products
WHERE (unit_price * qty_in_hand) = (SELECT MAX(unit_price * qty_in_hand) FROM products);

B.
SELECT prod_name
FROM products
WHERE (unit_price * qty_in_hand) = (SELECT MAX(unit_price * qty_in_hand) FROM products
GROUP BY prod_name);

C.
SELECT prod_name
FROM products
GROUP BY prod_name
HAVING MAX(unit_price * qty_in_hand) = (SELECT MAX(unit_price * qty_in_hand) FROM products
GROUP BY prod_name);

D.
SELECT prod_name
FROM products
WHERE (unit_price * qty_in_hand) = (SELECT MAX(SUM(unit_price * qty_in_hand)) FROM products)
GROUP BY prod_name;

12 Comments on “Which SQL statement gives the required output?

  1. banu says:

    Here it asks for the highest TOTAL value of UNIT_PRICE *
    QTY_IN_HAND.

    But Option A gives only the highest value without group by or using SUM.

    How is A correct

  2. Justyna says:

    A returns only one row: the prod_name with MAX(unit_price*qty_in_hand).

    For exmaple:
    select ename from emp where sal+nvl(comm,0) = (select max(sal+nvl(comm,0)) from emp);

    ENAME
    ———-
    KING

  3. banu says:

    But what if there is duplicate records for the same product name with different UNIT_PRICE *QTY_IN_HAND values?

    we need not find the GROUP BY based on product name?

    In this case you mean the record with max(UNIT_PRICE *QTY_IN_HAND) will be displayed irrespective of duplicates or not?

  4. Justyna says:

    We only need to know what is the maximum value for all products in table not for each product. Functions like max, avg, min… return one record regardles of how many duplicate values is in table when ‘group by ‘ is not used.

  5. Justyna says:

    if the question would be “You want to display the name of the PRODUCT that have the highest total value for UNIT_PRICE * QTY_IN_HAND then the answer would be:
    SELECT MAX(unit_price * qty_in_hand) FROM products

    but we need to know the names of all products that have the highest total value for UNIT_PRICE * QTY_IN_HAND. To solve this problem you use subquery like in A. ‘Group by’ is not needed to get correct result.

    http://www.devshed.com/c/a/Oracle/Stepping-through-SubQueries-in-Oracle/2/

    D is not correct:
    – there is max(sum(….)) so you have nested group functions there

  6. Anand Aili says:

    If A is correct answer then Why B is incorrect?
    Because A will not sort out inner query result set but B will , still comparison is done for every row in the inner query result set.

    1. Sayed says:

      The PRODUCTS table has all the individual product names. There is no way having repeated product names. So, GROUP BY prod_name clause does not make any sense here as all rows are distinct. The question asked ‘nameS of the products’ meaning more than one products could have the highest total value for UNIT_PRICE * QTY_IN_HAND.

  7. sdvsvds says:

    D tambien es correcta, si para cada uno de los registros en la tabla productos, sea Ășnico e irrepetible.

  8. Karan says:

    I think question is not framed correctly here. It says to display ‘Names of the products’ NOT ‘name of a product’. So as per the question, a single product may appar more than once which may require group by clause. OR in this case, table values should have been provided to avoid confusion. As per the question i do not think answer is A is correct.


Leave a Reply

Your email address will not be published. Required fields are marked *