SQL Interview Questions – Unique Orders and Customers Per Month

Spread the love

Problem Description –

Write an SQL query to find the number of unique orders and the number of unique customers with invoices > $20 for each different month.

Return the result table sorted in any order.

The query result format is in the following example.

Problem Link – unique orders and customers

Difficulty Level – Easy

Solution –

SELECT
    DATE_FORMAT(order_date, '%Y-%m') as month,
    COUNT(DISTINCT order_id) as order_count,
    COUNT(DISTINCT customer_id) as customer_count
FROM Orders
WHERE invoice > 20
GROUP BY 1

Rating: 1 out of 5.

Leave a Reply