SQL interview question: products with no sales

MediumAnti-join: what’s missing

Problem

Shared data rules

For each city and category, count the products on sale that nobody in that city bought in September 2026. Count older products and new arrivals separately.

Example

Input: products
product_idcategoryadded_atis_activeRow note
1Beverages2025-03-011
2Beverages2025-04-101
3Beverages2026-09-241← new arrival
4Sweets & snacks2025-02-011
5Sweets & snacks2025-02-010← off sale
Input: orders
order_idcitystatuscreated_atRow note
101Bostondelivered2026-09-05 13:00
102Bostondelivered2026-08-20 13:00← August — outside the period
103Bostondelivered2026-09-03 11:00
104Washingtondelivered2026-09-10 12:00
Input: order_items
order_idproduct_id
1011
1022
1034
1041
Output
citycategoryunsold_oldunsold_new
BostonBeverages11
BostonSweets & snacks00
WashingtonBeverages11
WashingtonSweets & snacks10

In Boston only beverage 1 sold in September: products 2 (old) and 3 (new) are unsold. In Washington product 4 is unsold; in Boston it sold, and product 5 is off sale.

Notes

Solve it free

Your query runs on PostgreSQL in your browser. No sign-up needed.

Skills it tests

More interview questions

All questions