Sales Analysis I LeetCode Solution

Last updated on October 9th, 2024 at 10:44 pm

This Leetcode problem Sales Analysis I LeetCode Solution is done in SQL.

List of all LeetCode Solution

Level of Question

Easy

Sales Analysis I LeetCode Solution

Sales Analysis I LeetCode Solution

Problem Statement

Column NameType
product_idint
product_namevarchar
unit_priceint
Table: Product

product_id is the primary key (column with unique values) of this table. Each row of this table indicates the name and the price of each product.

Column NameType
seller_idint
product_idint
buyer_idint
sale_datedate
quantityint
priceint
Table: Sales

This table can have duplicate rows. product_id is a foreign key (reference column) to the Product table. Each row of this table contains some information about one sale.

Write an SQL query that reports the best seller by total sales price, If there is a tie, report them all.

The result format is in the following example.

product_idproduct_nameunit_price
1S81000
2G4800
3iPhone1400
Product table:
seller_idproduct_idbuyer_id sale_datequantityprice
1112019-01-2122000
1222019-02-171800
2232019-06-021800
3342019-05-1322800
Sales table:
Output:
seller_id
1
3
Explanation:
Both sellers with id 1 and 3 sold products with the most total price of 2800.

1. Sales Analysis I LeetCode Solution MySQL

select 
  seller_id 
from 
  Sales 
group by 
  seller_id 
having 
  sum(price) >= (
    select 
      sum(price) as total_price 
    from 
      Sales 
    group by 
      seller_id 
    order by 
      total_price desc 
    limit 
      1
  );
Scroll to Top