Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Second-Highest Sales Per Category
00:00
5 left

Second-Highest Sales Per Category

MediumSQL · PostgreSQL

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
Interviewer

Your question is Second-Highest Sales Per Category. Start with the requirements and the three tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.