Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Rank Vendors With Window Functions
00:00
5 left

Rank Vendors With Window Functions

MediumSQL · PostgreSQL

Problem

Write a SQL query that uses window functions to rank vendors by cost savings within each category.

Use the provided vendor and quote data. Cost savings equal benchmark cost minus quoted cost, summed for each vendor and category. Exclude unmatched vendors and quote rows missing either cost.

Output

  1. One row per vendor and category, with vendor_id, vendor_name, category, total_cost_savings, and savings_rank.
  2. Ties share the same rank.
  3. Order by category, savings_rank, then vendor_id.

Schema

vendors
ColumnTypeDescription
vendor_idPKINTUnique vendor identifier
vendor_nameVARCHAR(100)Vendor business name
vendor_quotes
ColumnTypeDescription
quote_idPKINTUnique quote identifier
vendor_idINTVendor submitting the quote
categoryVARCHAR(50)Procurement category
benchmark_costNUMERIC(12,2)Expected or benchmark cost
quoted_costNUMERIC(12,2)Cost quoted by the vendor
Tablesvendorsvendor_quotes
Interviewer

Your question is Rank Vendors With Window Functions. Start with the requirements and the two 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.