Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL for Customer and Repeat Orders
00:00
5 left

SQL for Customer and Repeat Orders

HardSQL · PostgreSQL

Problem

Swiggy's Business Analytics team wants a combined SQL report covering IPL fixture combinations, restaurant customer exclusions, and monthly retention. Write one PostgreSQL query that returns all three analyses in a consistent output format.

Requirements

  1. Generate every unique IPL team pairing so each team plays every other team once. Do not pair a team with itself or duplicate a pairing.
  2. Find customers who ordered from KFC or McDonald's at least once, but never ordered from Burger King. Return their customer details.
  3. Calculate monthly repeat order rate as customers who ordered in both the current and immediately previous calendar month divided by all customers ordering in the current month. Include active customers, repeat customers, and the rate.
  4. Label each result type so the combined output can be separated downstream. Round the rate to four decimal places.

Schema

teams
ColumnTypeDescription
team_idPKINTIPL team identifier
team_nameVARCHAR(50)IPL team name
customers
ColumnTypeDescription
customer_idPKINTSwiggy customer identifier
customer_nameVARCHAR(100)Customer name
cityVARCHAR(50)Customer city
restaurants
ColumnTypeDescription
restaurant_idPKINTRestaurant identifier
restaurant_nameVARCHAR(100)Restaurant brand
orders
ColumnTypeDescription
order_idPKINTOrder identifier
customer_idINTOrdering customer
restaurant_idINTOrdered restaurant
ordered_atDATEOrder date
Tablesteamscustomersrestaurantsorders
Interviewer

Your question is SQL for Customer and Repeat Orders. Start with the requirements and the four tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.