Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Three Vendors Per Department

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

Your question is Top Three Vendors Per Department. 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

Write a query to find the top three most expensive vendors per department, handling ties appropriately.

Calculate each vendor's total invoice amount within each department. Include vendors with tied totals when they fall within the top three spending levels.

Output

  1. One row per qualifying department and vendor
  2. Columns: department_name, vendor_name, total_spend, and spend_rank
  3. Sort by department name, rank ascending, then vendor name
  4. Exclude records without a valid department or vendor

Schema

departments
ColumnTypeDescription
department_idPKINTUnique department identifier
department_nameVARCHAR(100)Department name
vendors
ColumnTypeDescription
vendor_idPKINTUnique vendor identifier
vendor_nameVARCHAR(150)Vendor name
invoices
ColumnTypeDescription
invoice_idPKINTUnique invoice identifier
vendor_idINTReferenced vendor identifier
department_idINTReferenced department identifier
amountDECIMAL(12,2)Invoice amount
Tablesdepartmentsvendorsinvoices
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results