Product Table: Product
+ --------- + -------------- +
| Column the Name | Type |
+ -------------- + - + -------
| product_id | int |
| PRODUCT_NAME | VARCHAR |
| unit_price | int |
+ -------------- + --------- +
product_id It is the primary key of this table.
sales table: sales
+ ------------- + --------- +
| Column the Name | Type |
+ ------------- + ---- + -----
| seller_id | int |
| product_id | int |
| buyer_id | int |
| sale_date | DATE |
| the Quantity | int |
|. price | int |
+ ------ + ------ --------- +
the table does not have a primary key, it can have duplicate rows.
the product_id Product table is a foreign key.
Write a SQL query, the highest total sales sellers, if there are side by side, they are displayed.
Results shown in the following format:
Product 表:
+------------+--------------+------------+
| product_id | product_name | unit_price |
+------------+--------------+------------+
| 1 | S8 | 1000 |
| 2 | G4 | 800 |
| 3 | iPhone | 1400 |
+------------+--------------+------------+
Sales 表:
+-----------+------------+----------+------------+----------+-------+
| seller_id | product_id | buyer_id | sale_date | quantity | price |
+-----------+------------+----------+------------+----------+-------+
| 1 | 1 | 1 | 2019-01-21 | 2 | 2000 |
| 1 | 2 | 2 | 2019-02-17 | 1 | 800 |
| 2 | 2 | 3 | 2019-06-02 | 1 | 800 |
| 3 | 3 | 4 | 2019-05-13 | 2 | 2800 |
+-----------+------------+----------+------------+----------+-------+
Result Table:
+ ------------- +
| seller_id |
+ ------------- +
| 1 |
| 3 |
+ ------- + ------
Id 1 and 3 of the seller, the total amount of sales are the highest of 2800.
Ideas: Find out all the seller's sales, then check greater than or equal selleer all sales.
select seller_id
from sales
group by seller_id
having sum(price) >= all(select sum(price) from sales group by seller_id);