Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Brands by State

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

Your question is Top Brands by State. 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

Chewy wants a state-level view of which pet food brands are selling the most. Write a PostgreSQL query using joins, aggregation, date filtering, and a window function.

Use 2026-08-29 as the reporting date for the sample data. Include orders from the last 30 days, exclude non-pet-food products, and rank brands by total units sold.

Requirements

  1. Join orders, order_items, and products.
  2. Aggregate units sold by state and brand for qualifying orders.
  3. Return the top three brands per state, including the rank and total units sold.
  4. Break ties deterministically by brand name.

Schema

orders
ColumnTypeDescription
order_idPKINTUnique order identifier
stateVARCHAR(2)Shipping state abbreviation
order_dateDATEDate the order was placed
order_items
ColumnTypeDescription
order_item_idPKINTUnique line-item identifier
order_idINTReferences orders.order_id
product_idINTReferences products.product_id
quantityINTNumber of units in the line item
products
ColumnTypeDescription
product_idPKINTUnique product identifier
brandVARCHAR(100)Product brand
categoryVARCHAR(50)Product category
Tablesordersorder_itemsproducts
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results