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.
You have a table named *Products* with the following schema:
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?
3 answers
Sort by: Most helpful
-
-
LiHongMSFT-4306 27,016 Reputation points
2023-08-28T02:34:58.7366667+00:00 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.
-
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
andprice
columns from theProducts
table and orders the results in descending order of theprice
column. TheLIMIT 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.