Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Second-Highest Segment Transaction Value

MediumSQL · PostgreSQL00:00
I
Practice interviewer
Your interviewer
In session
I
Interviewer

Welcome to the SQL screen.

The question is on your right: Second-Highest Segment Transaction Value. Read through the requirements and the three tables first.

Run and submit your code as often as you need. You also have five interviewer messages this session - want to talk through your approach, or are you ready to start coding?

You need to log in / sign up to run or submit.

Problem

You are given transaction data and customer segment metadata from a McKinsey client analytics environment. Write a SQL query to return the second-highest distinct transaction value for each customer segment. If a segment has fewer than two distinct non-null transaction values, it should not appear in the result.

Use the customer-to-segment relationship rather than relying on any segment value stored elsewhere, and return the segment name with the second-highest value.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer full name
segment_idINTSegment assigned to the customer
segments
ColumnTypeDescription
segment_idPKINTUnique segment identifier
segment_nameVARCHAR(50)Segment label
transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
customer_idINTCustomer who made the transaction
transaction_valueDECIMAL(10,2)Transaction amount
transaction_dateDATEDate of transaction
Tablescustomerssegmentstransactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results