SQL Query To Show Top 5 Selling Products
To display the top 5 selling products ORDER BY can be used with the SELECT TOP clause. So let us see the SQL Query for Showing Top 5 Selling Products using ORDER BY and SELECT TOP clause using MSSQL as the server.
Step 1: We are creating a Database. For this use the below command to create a database named GeeksforGeeks.
Query:
CREATE DATABASE GeeksforGeeks;
Step 2: To use the GeeksforGeeks database use the below command.
Query:
USE GeeksforGeeks
Step 3: Now we creating a table. Create a table sales_details with 4 columns using the following SQL query.
Query:
CREATE TABLE sales_details(
item_id VARCHAR(20),
item_price INT,
items_sold INT
)
Step 4: Viewing the description of the table.
Query:
EXEC sp_columns sales_details
Step 5: The query for Inserting rows into the Table. Inserting rows into sales_details table using the following SQL query.
Query:
INSERT INTO sales_details VALUES
('I4001',20000, 5000),
('I4098',1000, 10000),
('I4010',200, 800),
('I4056',30000, 100000),
('I4068',990, 780),
('I4072',10000, 9000),
('I4078',100000, 10),
('I4090',200000, 500)
Step 6: Viewing the inserted data and order by descending order of the number of units sold.
Query:
SELECT * FROM sales_details
ORDER BY items_sold DESC
Step 7: Query to find the top 5 selling products.
Syntax:
SELECT TOP N column_name
FROM table_name ORDER BY column_name ordering_type
Query:
SELECT TOP 5 item_id , items_sold
FROM sales_details ORDER BY items_sold DESC
Output:
Last Updated :
08 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...