You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Write a solution to report the **buyers** who have bought *S8* but not *iPhone*. Note that *S8* and *iPhone* are products presented in the `Product` table.
11
+
12
+
Return the resulting table in **any order**. [[Full Description](https://leetcode.com/problems/sales-analysis-ii/description/)]
13
+
14
+
## Submitted Solution
15
+
16
+
```sql
17
+
-- Submitted Solution
18
+
WITH cte AS (
19
+
SELECT
20
+
s.buyer_id
21
+
,STRING_AGG(p.product_name, ',') AS products
22
+
FROM Sales AS s
23
+
JOIN Product AS p ONs.product_id=p.product_id
24
+
GROUP BYs.buyer_id
25
+
)
26
+
SELECT
27
+
buyer_id
28
+
FROM cte
29
+
WHERE products LIKE'%S8%'
30
+
AND products NOT LIKE'%iPhone%'
31
+
;
32
+
```
33
+
34
+
## Site Solution
35
+
36
+
```sql
37
+
-- LeetCode Solution
38
+
SELECT DISTINCTs.buyer_id
39
+
FROM Sales s
40
+
JOIN Product p
41
+
ONs.product_id=p.product_id
42
+
GROUP BYs.buyer_id
43
+
HAVING GROUP_CONCAT(p.product_name) LIKE'%S8%'
44
+
AND GROUP_CONCAT(p.product_name) NOT LIKE'%iPhone%'
0 commit comments