Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Customers Active in Both Years

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

Your question is Customers Active in Both Years. Start with the requirements and the two tables 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

Amazon Retail wants to identify customers who were consistently active across 2019 and 2020. Write a PostgreSQL query that returns customers with more than three transactions in each of those two calendar years.

Requirements

  1. Join customers to transactions using customer_id.
  2. Count each customer's transactions separately for 2019 and 2020.
  3. Return only customers whose count is greater than 3 in both years.
  4. Include the customer ID, customer name, and both yearly transaction counts. Order the output by customer_id.

Schema

customers
ColumnTypeDescription
customer_idPKINTEGERUnique Amazon customer identifier
customer_nameVARCHAR(100)Customer display name
marketplaceVARCHAR(30)Amazon marketplace associated with the customer
transactions
ColumnTypeDescription
transaction_idPKINTEGERUnique transaction identifier
customer_idINTEGERCustomer who made the transaction
transaction_dateDATEDate of the transaction
amountNUMERIC(10,2)Transaction amount in USD
Tablescustomerstransactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results