PostgreSQL 풀이 | Highest-Grossing Items

2025. 3. 7. 16:37·SQL

문제 1.  Highest-Grossing Items

Assume you're given a table containing data on Amazon customers and their spending on products in different category, write a query to identify the top two highest-grossing products within each category in the year 2022. The output should include the category, product, and total spend.

https://datalemur.com/questions/sql-highest-grossing

 

Amazon SQL Interview Question | DataLemur

Amazon SQL Interview Question: Write a query to identify highest-grossing products within each category.

datalemur.com


💡 문제 풀이 

`내 풀이`

소요 시간 08:06

with t1 as(
SELECT category, product, 
  sum(spend) AS total_spend,
  rank() over(partition by category order by sum(spend) desc) AS ranking
FROM product_spend
WHERE EXTRACT (YEAR FROM transaction_date) = 2022
GROUP BY category, product
)
SELECT category, product, total_spend
FROM t1
WHERE ranking < 3;

 

`오답노트`

  • 자꾸 맨 뒤에 세미콜론 ; 빼먹는데 습관 들이기

💯결과

 

'SQL' 카테고리의 다른 글
  • PostgreSQL 풀이 | Active User Retention
  • PostgreSQL 풀이 | Teams Power Users
  • PostgreSQL 풀이 | Histogram of Tweets
  • PostgreSQL 풀이 | Data Science Skills
초담
초담
4년차 마케터입니다
  • 초담
    그로스마케터의 기록
    초담
  • 전체
    오늘
    어제
  • 글쓰기 관리
    • 분류 전체보기 (117)
      • Data Analytics Project (3)
      • SQL (55)
      • Python (43)
      • GA4 (0)
      • Tableau (8)
      • 아티클 스터디 (7)
  • 인기 글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.2
초담
PostgreSQL 풀이 | Highest-Grossing Items
상단으로

티스토리툴바