Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top 5 Categories by Total Count

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

Your question is Top 5 Categories by Total Count. Start with the requirements and the two 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

University of Kansas Medical Center reporting needs a summary of transaction volume by category. Write a PostgreSQL query that identifies the five categories with the most completed transactions during calendar year 2024.

Requirements

  1. Include every category in the aggregation, including categories with no qualifying transactions.
  2. Count only transactions with status = 'Completed' and a transaction_date in 2024.
  3. Return the category name and transaction count, ordered from highest to lowest count, with category name ascending as the tie-breaker.
  4. Return only the top five categories.

Schema

transaction_categories
ColumnTypeDescription
category_idPKINTPrimary key for the category
category_nameVARCHAR(100)KUMC transaction category name
is_activeBOOLEANWhether the category is currently active
transactions
ColumnTypeDescription
transaction_idPKINTPrimary key for the transaction
category_idINTAssigned transaction category
statusVARCHAR(20)Transaction processing status
transaction_dateDATEDate the transaction was recorded
amountDECIMAL(12,2)Illustrative transaction amount
Tablestransaction_categoriestransactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results