Back to Browse

Top Selling Product in each category | Most asked SQL problem

2.7K views
Oct 21, 2023
12:01

This is the most asked SQL problem in any interview - Find the top-selling products in each category based on sales revenue. We will use Window function to accomplish this. Learn Window functions in detail: https://youtu.be/E8ZDobnpQJ4 📺 Watch the complete playlist for our SQL tutorials in Hindi series: https://www.youtube.com/playlist?list=PL2bSwoICMApEXtoJNOW2COxCYiH7wkeLj DDL: CREATE TABLE Categories ( CategoryID INT PRIMARY KEY, CategoryName VARCHAR(255) NOT NULL ); -- Insert sample categories INSERT INTO Categories (CategoryID, CategoryName) VALUES (1, 'Electronics'), (2, 'Accessories'), (3, 'Photography'), (4, 'Appliances'); DDL: CREATE TABLE Products ( ProductID INT PRIMARY KEY, ProductName VARCHAR(255) NOT NULL, CategoryID INT, Price DECIMAL(10, 2), FOREIGN KEY (CategoryID) REFERENCES Categories(CategoryID) ); -- Insert sample products INSERT INTO Products (ProductID, ProductName, CategoryID, Price) VALUES (1, 'Laptop', 1, 899.99), (2, 'Smartphone', 1, 699.99), (3, 'Headphones', 2, 149.99), (4, 'Tablet', 1, 399.99), (5, 'Camera', 3, 499.99), (6, 'Printer', 3, 299.99), (7, 'Mouse', 2, 29.99), (8, 'Keyboard', 2, 49.99), (9, 'TV', 4, 899.99), (10, 'Soundbar', 4, 249.99); DDL: CREATE TABLE Sales ( SaleID INT PRIMARY KEY, ProductID INT, QuantitySold INT, SaleDate DATE, FOREIGN KEY (ProductID) REFERENCES Products(ProductID) ); -- Insert sample sales data INSERT INTO Sales (SaleID, ProductID, QuantitySold, SaleDate) VALUES (1, 1, 120, '2023-09-01'), (2, 2, 90, '2023-09-02'), (3, 3, 200, '2023-09-03'), (4, 4, 35, '2023-09-04'), (5, 5, 75, '2023-09-05'), (6, 6, 50, '2023-09-06'), (7, 7, 160, '2023-09-07'), (8, 8, 120, '2023-09-08'), (9, 9, 25, '2023-09-09'), (10, 10, 40, '2023-09-10'); #sqlinterviewquestionsandanswers #sqlinterview #sqlinterviewquestions

Download

0 formats

No download links available.

Top Selling Product in each category | Most asked SQL problem | NatokHD