Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Bucket Users by Order Count

EasySQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Bucket Users by Order Count. Start with the requirements and the one table on the right.

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.

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.

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
Tablesusers
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results