SQL MOSAIC # 1084. Sales Analysis III

Summer Nie
Jul 5, 2022

--

Date: 7.02.2022

Write an SQL query that reports the products that were only sold in the first quarter of 2019. That is, between 2019–01–01 and 2019–03–31 inclusive.

Return the result table in any order.

Thinking Pathway:

  1. Filter for 2019 Q1
  2. TRICK: this question needs us to filter for products that were sold ONLY in Q1. So, we will need to filter for products that were sold in Q1 AND NOT in the other quarters.

Solution(s):

SELECT DISTINCT p.product_id, p.product_name

FROM Product p JOIN Sales s ON p.product_id = s.product_id

WHERE s.product_id NOT IN

(SELECT product_id

FROM Sales

WHERE sale_date NOT BETWEEN ‘2019–01–01’ AND ‘2019–03–31’)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response