Simple Solution using CTE
IntuitionIntuition
The goal is to identify products that were exclusively sold in the first quarter of 2019. The first step is to filter sales occurring within
# Intuition
Intuition
The goal is to identify products that were exclusively sold in the first quarter of 2019. The first step is to filter sales occurring within this timeframe, and then exclude products with sales outside this range. The key insight is leveraging SQL constructs like WITH clauses (for Common Table Expressions) and set operations (NOT IN) to achieve this.

# Approach
Step 1: Filter Sales in Q1 2019
Create a temporary list of products sold between 2019-01-01 and 2019-03-31 using a WITH clause. This captures all relevant products during the desired timeframe.

Step 2: Identify Sales Outside Q1 2019
Create another temporary list of products sold before or after Q1 2019. This step ensures we can exclude products that are not exclusive to the Q1 period.

Step 3: Exclude Non-Exclusive Products
Use the two temporary lists to filter products sold only in Q1 2019. This is achieved by joining the first list with the Product table and excluding products present in the second list using NOT IN.

Final Query:
Return the product_id and product_name for products that meet the above criteria.



# Code
```mysql []
WITH FirstQuarterSales AS (
    SELECT DISTINCT product_id
    FROM Sales
    WHERE sale_date BETWEEN '2019-01-01' AND '2019-03-31'
),
OtherSales AS (
    SELECT DISTINCT product_id
    FROM Sales
    WHERE sale_date < '2019-01-01' OR sale_date > '2019-03-31'
)
SELECT p.product_id, p.product_name
FROM Product AS p
JOIN FirstQuarterSales AS fqs
ON p.product_id = fqs.product_id
WHERE p.product_id NOT IN (SELECT product_id FROM OtherSales);


```
['MySQL']