SQL Interview Questions – Sales Analysis III

Spread the love

Problem Description –

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.

The query result format is in the following example.

Difficulty Level – Easy

Problem Link – https://leetcode.com/problems/sales-analysis-iii/

Solution –

SELECT
    DISTINCT
    product_id,
    product_name
FROM (
SELECT 
    s.product_id,
    p.product_name
FROM sales as s JOIN product as p
ON s.product_id = p.product_id
    ) x 
WHERE product_id NOT IN (
                        SELECT
                            product_id
                        FROM sales
                        WHERE sale_date NOT BETWEEN '2019-01-01' AND '2019-03-31'
                        )

Rating: 1 out of 5.

Leave a Reply