You have a table named *Products* with the following schema:

Syed Shah Hussain Bukhari 140 Reputation points
2023-08-27T01:13:04.4766667+00:00

You have a table named Products with the following schema:

  • product_id (Primary Key)
  • product_name
  • price
  • stock_quantity

How to write an SQL query to retrieve the names of the top 5 most expensive products along with their prices. Order the results from the highest price to the lowest?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,786 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Erland Sommarskog 111.1K Reputation points MVP
    2023-08-27T09:51:24.0633333+00:00

    Can you please tell us why we should write the query for your class assignment or interview question for you? I will give you the hint that you can achieve this with the TOP clause or the row_number function.

    0 comments No comments

  2. LiHongMSFT-4306 27,016 Reputation points
    2023-08-28T02:34:58.7366667+00:00

    Hi @Syed Shah Hussain Bukhari

    Check this:

    Select Top(5) product_id,product_name,price
    from Products
    Order by price DESC
    

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Muhammad Binyameen 395 Reputation points
    2023-08-28T14:02:59.9366667+00:00

    To retrieve the names of the top 5 most expensive products along with their prices in SQL Server, you can use the following SQL query:

    
    SELECT product_name, price
    
    FROM Products
    
    ORDER BY price DESC
    
    LIMIT 5;
    
    

    This query selects the product_name and price columns from the Products table and orders the results in descending order of the price column. The LIMIT 5 clause ensures that only the top 5 most expensive products are returned.

    Please note that the syntax and functionality of SQL queries can vary slightly between different database management systems. So, make sure to adjust the query if you are using a different database system than SQL Server.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.