Skip to content

Commit 8293000

Browse files
add 1083
1 parent 5bb708a commit 8293000

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ _If you like this project, please leave me a star._ ★
852852
|1179|[Reformat Department Table](https://leetcode.com/problems/reformat-department-table/)|[Solution](../master/database/_1179.sql) | | Easy |
853853
|1173|[Immediate Food Delivery I](https://leetcode.com/problems/immediate-food-delivery-i/)|[Solution](../master/database/_1173.sql) | | Easy |
854854
|1148|[Article Views I](https://leetcode.com/problems/article-views-i/)|[Solution](../master/database/_1148.sql) | | Easy |
855+
|1083|[Sales Analysis II](https://leetcode.com/problems/sales-analysis-ii/)|[Solution](../master/database/_1083.sql) | | Easy |
855856
|1082|[Sales Analysis I](https://leetcode.com/problems/sales-analysis-i/)|[Solution](../master/database/_1082.sql) | | Easy |
856857
|1069|[Product Sales Analysis II](https://leetcode.com/problems/product-sales-analysis-ii/)|[Solution](../master/database/_1069.sql) | | Easy |
857858
|1068|[Product Sales Analysis I](https://leetcode.com/problems/product-sales-analysis-i/)|[Solution](../master/database/_1068.sql) | | Easy |

database/_1083.sql

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
--1083. Sales Analysis II
2+
--
3+
--Table: Product
4+
--
5+
--+--------------+---------+
6+
--| Column Name | Type |
7+
--+--------------+---------+
8+
--| product_id | int |
9+
--| product_name | varchar |
10+
--| unit_price | int |
11+
--+--------------+---------+
12+
--product_id is the primary key of this table.
13+
--Table: Sales
14+
--
15+
--+-------------+---------+
16+
--| Column Name | Type |
17+
--+-------------+---------+
18+
--| seller_id | int |
19+
--| product_id | int |
20+
--| buyer_id | int |
21+
--| sale_date | date |
22+
--| quantity | int |
23+
--| price | int |
24+
--+------ ------+---------+
25+
--This table has no primary key, it can have repeated rows.
26+
--product_id is a foreign key to Product table.
27+
--
28+
--
29+
--Write an SQL query that reports the buyers who have bought S8 but not iPhone. Note that S8 and iPhone are products present in the Product table.
30+
--
31+
--The query result format is in the following example:
32+
--
33+
--Product table:
34+
--+------------+--------------+------------+
35+
--| product_id | product_name | unit_price |
36+
--+------------+--------------+------------+
37+
--| 1 | S8 | 1000 |
38+
--| 2 | G4 | 800 |
39+
--| 3 | iPhone | 1400 |
40+
--+------------+--------------+------------+
41+
--
42+
--Sales table:
43+
--+-----------+------------+----------+------------+----------+-------+
44+
--| seller_id | product_id | buyer_id | sale_date | quantity | price |
45+
--+-----------+------------+----------+------------+----------+-------+
46+
--| 1 | 1 | 1 | 2019-01-21 | 2 | 2000 |
47+
--| 1 | 2 | 2 | 2019-02-17 | 1 | 800 |
48+
--| 2 | 1 | 3 | 2019-06-02 | 1 | 800 |
49+
--| 3 | 3 | 3 | 2019-05-13 | 2 | 2800 |
50+
--+-----------+------------+----------+------------+----------+-------+
51+
--
52+
--Result table:
53+
--+-------------+
54+
--| buyer_id |
55+
--+-------------+
56+
--| 1 |
57+
--+-------------+
58+
--The buyer with id 1 bought an S8 but didn't buy an iPhone. The buyer with id 3 bought both.
59+
60+
--# Write your MySQL query statement below
61+
select distinct buyer_id from Sales s
62+
join Product p
63+
on p.product_id = s.product_id
64+
where p.product_name = 'S8'
65+
and buyer_id not in
66+
(
67+
select buyer_id from Sales s
68+
join Product p on p.product_id = s.product_id
69+
where p.product_name = 'iPhone'
70+
)

0 commit comments

Comments
 (0)