Customers with 3 consecutive buying months

Hard
16 views 23 Jan 2026
Find customers who placed at least one order in 3 consecutive months (any year). Return customer_id only. Hint: convert to month buckets, then use a window row_number trick....

Basic SELECT with filter

Easy
3 views 23 Jan 2026
You have Customers(customer_id, name, city). Write a query to show only customer_id and name for customers living in 'Mumbai'. Keep it simple: one WHERE condition....

Sort results

Easy
3 views 23 Jan 2026
From Orders(order_id, order_date), show the latest 10 orders. Sort by order_date (newest first). If dates are same, you can sort by order_id too....

Count rows per group

Easy
2 views 23 Jan 2026
You need a small city-wise report. From Customers(city), return city and how many customers are in that city....

Use HAVING for group filter

Easy
4 views 23 Jan 2026
From Employees and Departments, show only those departments where employee count is more than 5. Display dept_name and the count....

Distinct values

Easy
3 views 23 Jan 2026
From Orders(status), list all different statuses available. You should not repeat the same status in output....

Top customer by total spend

Medium
2 views 23 Jan 2026
From Orders(customer_id, total_amount), find the customer who spent the most overall. First do SUM per customer, then pick the highest one....

Daily sales report

Medium
2 views 23 Jan 2026
Make a simple daily sales report for the last 7 days. From Orders(order_date, total_amount), show order_date and total sales per day....

Customers with zero orders

Medium
4 views 23 Jan 2026
List customers who never placed any order. You can solve using LEFT JOIN + NULL check (or NOT EXISTS)....

Find gaps in order ids

Medium
2 views 23 Jan 2026
Assume order_id should be continuous. Using a helper Numbers(n) table, list the missing order_id between MIN(order_id) and MAX(order_id)....

Second highest salary per department

Medium
2 views 23 Jan 2026
For each department, return employee(s) who have the second highest salary. Use DENSE_RANK so ties are handled nicely....

3-month rolling revenue

Hard
3 views 23 Jan 2026
Create month-wise revenue and also show a rolling 3-month total. First aggregate by month, then apply a window sum over the last 3 months....

Detect customers with suspicious refunds

Hard
2 views 23 Jan 2026
Assume Payments.amount is negative for refunds. Find customers where total refund is more than 30% of total paid amount. Use conditional SUM....

Find first purchase date and latest purchase date

Hard
2 views 23 Jan 2026
For each customer, show the first time they ordered and the most recent order date. This is a clean use of MIN and MAX....

Market basket: products bought together

Hard
2 views 23 Jan 2026
From OrderItems(order_id, product_id), count how often two products are bought in the same order. Return top 10 pairs by count....