[1045] Customers Who Bought All Products

2022. 7. 11. 17:12·SQL/LeetCode

Table: Customer

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| customer_id | int     |
| product_key | int     |
+-------------+---------+
There is no primary key for this table. It may contain duplicates.
product_key is a foreign key to Product table.

 

Table: Product

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| product_key | int     |
+-------------+---------+
product_key is the primary key column for this table.

 

Write an SQL query to report the customer ids from the Customer table that bought all the products in the Product table.

Return the result table in any order.

The query result format is in the following example.

 

Example 1:

Input: 
Customer table:
+-------------+-------------+
| customer_id | product_key |
+-------------+-------------+
| 1           | 5           |
| 2           | 6           |
| 3           | 5           |
| 3           | 6           |
| 1           | 6           |
+-------------+-------------+
Product table:
+-------------+
| product_key |
+-------------+
| 5           |
| 6           |
+-------------+
Output: 
+-------------+
| customer_id |
+-------------+
| 1           |
| 3           |
+-------------+
Explanation: 
The customers who bought all the products (5 and 6) are customers with IDs 1 and 3.

 

문제 조건 

Product 테이블의 모든 제품을 구매한 Customer 테이블의 고객 ID를 출력하시오 

 

풀이 과정

모든 제품을 구매 한 customer을 찾아야 하기 때문에 group by를 해서 묶어줌 

distinct product_key를 count 한 값이, product 테이블의 product_key를 count한 값과 같다면 

그 사람은 모든 제품을 구매 한 적이 있는 것이 되는 것.

 

처음에는 where을 쓸 수 있을까 했는데 그렇게 되면 id별 묶어지지 않으니까... 

SELECT customer_id
FROM Customer 
GROUP BY customer_id 
HAVING COUNT(DISTINCT product_key) = (SELECT COUNT(product_key)
                                     FROM Product)

'SQL > LeetCode' 카테고리의 다른 글

[1484] Group Sold Products By The Date  (0) 2022.07.12
[1070#] Product Sales Analysis III  (0) 2022.07.11
[1435#] Create a Session Bar Chart  (0) 2022.07.11
[1421#] NPV Queries  (0) 2022.07.11
[1407] Top Travellers  (0) 2022.07.11
'SQL/LeetCode' 카테고리의 다른 글
  • [1484] Group Sold Products By The Date
  • [1070#] Product Sales Analysis III
  • [1435#] Create a Session Bar Chart
  • [1421#] NPV Queries
소금깨
소금깨
  • 소금깨
    고군분투 인생살이
    소금깨
  • 전체
    오늘
    어제
    • 분류 전체보기 (328)
      • SQL (271)
        • 프로그래머스 (27)
        • LeetCode (198)
        • Hacker Rank (27)
        • Solve SQL (1)
        • 개념 (15)
      • 데이터 분석 (16)
        • 참고하며 공부하기 (14)
      • 기타 (15)
        • 통계 (14)
      • 오류 (6)
      • 인생살이 (0)
        • 리뷰 (0)
        • 일기 (0)
      • 中文 (0)
      • TABLEAU (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    파이썬을활용한시계열데이터분석
    직장인자기계발
    Hard
    해커랭크
    MySQL
    HACKER_RANK
    프로그래머스
    SQL
    LeetCode
    시계열데이터분석
    medium
    직장인인강
    패스트캠퍼스후기
    프리미엄
    group by
    패캠챌린지
    패스트캠퍼스
    solvesql
    파이썬을활용한시계열데이터분석AtoZ올인원패키지Online
    easy
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
소금깨
[1045] Customers Who Bought All Products
상단으로

티스토리툴바