Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Second-Highest Sales Per Category

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

Your question is Second-Highest Sales Per Category. Start with the requirements and the three 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

Business Context

Sigmoid's retail analytics platform needs to identify the second-highest sales transaction within each store category for category-level performance reviews.

Task

Write a PostgreSQL query that returns the second-highest distinct transaction amount for every retail category represented by a store. If multiple transactions share the second-highest amount, return all of them.

Requirements

  1. Join categories, stores, and sales transactions.
  2. Rank non-null transaction amounts independently within each category.
  3. Return transactions with rank 2, ordered by category name and transaction ID.
  4. Exclude categories with fewer than two distinct transaction amounts.

Schema

retail_categories
ColumnTypeDescription
category_idPKINTUnique retail category identifier
category_nameVARCHAR(100)Retail category name
retail_stores
ColumnTypeDescription
store_idPKINTUnique store identifier
store_nameVARCHAR(100)Store display name
category_idINTCategory associated with the store
sales_transactions
ColumnTypeDescription
transaction_idPKINTUnique sales transaction identifier
store_idINTStore where the transaction occurred
transaction_dateDATEDate of the transaction
amountNUMERIC(10,2)Transaction amount
Tablesretail_categoriesretail_storessales_transactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results