Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL for Third-Last Sale

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

Your question is SQL for Third-Last Sale. 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

SailPoint Technologies wants to identify person records in its identity data whose sale amount exceeds the amount of the third most recent sale. Write a PostgreSQL query that returns qualifying person IDs.

The third most recent sale is determined by sold_at DESC. Use sale_id DESC as a deterministic tie-breaker when timestamps are equal.

Requirements

  1. Find the amount associated with the third most recent sale across all sales.
  2. Return each person_id with at least one sale amount greater than that amount.
  3. Exclude NULL sale amounts through the comparison and return IDs in ascending order without duplicates.

Schema

persons
ColumnTypeDescription
person_idPKINTEGERUnique person identifier
person_nameVARCHAR(100)Person display name
sales
ColumnTypeDescription
sale_idPKINTEGERUnique sale identifier
person_idINTEGERReferences persons.person_id
sale_amountNUMERIC(12,2)Sale value
sold_atTIMESTAMPSale timestamp
Tablespersonssales
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results