AMAZON INTERVIEW QUESTION
1. SQL Query to Calculate Average Review Ratings.
Suppose you need to analyze customer feedback for products over time and you have a
below table Reviews containing review_id, product_id,
user_id, rating and review_date. Your task is to calculate the average review
ratings for each product every month.
CREATE TABLE ProductReviews (
review_id INT PRIMARY KEY,
product_id INT,
user_id INT,
rating INT,
review_date DATE
);
INSERT INTO ProductReviews (review_id, product_id, user_id, rating, review_date)
VALUES
(1, 101, 201, 4, '2024-01-10'),
(2, 102, 202, 5, '2024-01-15'),
(3, 101, 203, 3, '2024-02-10'),
(4, 103, 201, 4, '2024-02-20'),
(5, 101, 204, 5, '2024-02-25');
A) select *from ProductReviews
select product_id,year(review_date) as years ,month(review_date) as
months ,avg(rating) as avg_rating from productreviews
group by product_id,year(review_date) ,month(review_date) order by product_id
-----------------------------------------------------------------------------------
----------------------------------------
2. Determining Top-Selling Products Based on Analyzing Sales Data for Profitable
Insights?
CREATE TABLE Orders (
order_id INT PRIMARY KEY,
product_id INT,
order_amount DECIMAL(10, 2)
);
INSERT INTO Orders (order_id, product_id, order_amount)
VALUES
(1, 101, 50.00), (2, 102, 75.00), (3, 101, 100.00), (4, 103, 120.00), (5, 101,
80.00), (6, 102, 90.00),
(7, 101, 110.00),(8, 103, 130.00), (9, 102, 85.00),(10, 101, 95.00), (11, 102,
70.00),(12, 103, 140.00);
A) select product_id,sum(order_amount) as top_selling_product from Orders group by
product_id