PrepAway - Latest Free Exam Questions & Answers

Which code segment should you use?

You have a database that contains the tables as shown in the exhibit. (Refer to the Exhibit.)

You need to create a query that returns a list of products from Sales.ProductCatalog. The solution must meet
the following requirements:
UnitPrice must be returned in descending order.
The query must use two-part names to reference the table.
The query must use the RANK function to calculate the results.
The query must return the ranking of rows in a column named PriceRank.
The list must display the columns in the order thatthey are defined in the table.
PriceRank must appear last.
Which code segment should you use?
To answer, type the correct code in the answer area.

A.

Explanation:
SELECT ProductCatalog.CatID, ProductCatalog.CatName, ProductCatalog.ProductID,
ProductCatalog.ProdName, ProductCatalog.UnitPrice,
RANK() OVER (PARTITION BY ProductCatalog.UnitPrice ORDER BY ProductCatalog.
UnitPrice DESC) AS PriceRank
FROM Sales.ProductCatalog
ORDER BY ProductCatalog.UnitPrice DESC

22 Comments on “Which code segment should you use?

  1. riemi says:

    The query submitted as answer fulfills the requirements but I don’t understand why the “PARTITION BY ProductCatalog.UnitPrice” is necessary?

    I would use the following query:

    SELECT ProductCatalog.CatID, ProductCatalog.CatName, ProductCatalog.ProductID,
    ProductCatalog.ProdName, ProductCatalog.UnitPrice,
    RANK() OVER (ORDER BY ProductCatalog.UnitPrice DESC) AS PriceRank
    FROM Sales.ProductCatalog
    ORDER BY ProductCatalog.UnitPrice DESC




    1



    0
      1. Vince says:

        I think Riemi is right.

        Whether using partition by clause or not, RANK() function will make the same price with the same rank.

        If you want the same price with the different ranks, sequential ones, you should use ROW_NUMBER().




        1



        0
  2. Ronald says:

    I have the exact same problem with this answer.
    It doesn’t make sense at all.

    If you partition on UnitPrice, all the UnitPrices within a single partition will be the same! No use Ranking them.

    Leaving out the Partition By clause will rank the prices throughout the table i would think and would make better sense.

    A better scenario would be if you would want to know how the UnitPrices would rank within the Categories: OVER (PARTITION BY ProductCatalog.CatID ORDER BY ProductCatalog.UnitPrice DESC)




    0



    0
  3. Umar Memon says:

    Select ProductCatalog.CatID, ProductCatalog.CatName, ProductCatalog..ProductID,ProductCatalog..ProductName,ProductCatalog..Unit Price, from sales.productCatalog,
    RANK() OVER (ORDER BY ProductCatalog.UnitPrice DESC) AS PriceRank
    From SAles.ProductCatalog
    order by productCatalog.unitprice desc




    0



    0
  4. Ps says:

    Answer has to be like this.

    SELECT ProductCatalog.CatID,
    ProductCatalog.CatName,
    ProductCatalog.ProductID,
    ProductCatalog.ProdName,
    ProductCatalog.UnitPrice,
    RANK() OVER (ORDER BY ProductCatalog.UnitPrice DESC) AS PriceRank
    FROM Sales.ProductCatalog
    ORDER BY ProductCatalog.UnitPrice DESC




    1



    0

Leave a Reply