Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Bucket Users by Order Count

Easy
SQL & Data ManipulationData WranglingGroup ByCase WhenAsked 1 times

Problem

ShopWave wants a simple user segmentation report based on how many orders each user has placed. Write a PostgreSQL query that buckets users into segments using CASE WHEN.

Requirements

  1. Create a user_segment column using these rules based on order_count:
    • New for order_count = 0
    • Light for order_count between 1 and 4
    • Active for order_count between 5 and 9
    • Power for order_count >= 10
    • Unknown when order_count is NULL
  2. Return the number of users in each segment as user_count, ordered by user_count descending and then user_segment ascending.

Table Definition

users

ColumnTypeDescription
user_idINTPrimary key for each user
user_nameVARCHAR(100)User name
order_countINTTotal number of orders placed by the user
signup_sourceVARCHAR(50)Acquisition channel

Schema

users
ColumnTypeDescription
user_idPKINTPrimary key for each user
user_nameVARCHAR(100)User name
order_countINTTotal number of orders placed by the user
signup_sourceVARCHAR(50)Marketing channel where the user signed up

You are practicing as a guest. Sign up free to run your code against the sample data. Your draft stays right here.

Sign up freeI have an account
SELECT ...
FROM ...
JOIN ... ON ...
GROUP BY ...
HAVING ...
ORDER BY ... DESC;
Sign up to unlock solutions
Vanguard Data Scientist Interview Questions
Next questions
QuoraClassify Power Users by ActivityMediumInstacartSegment Orders by Delivery OutcomeEasyPublicis GroupeWeekly Active Purchaser Growth RateMedium
PostgreSQL