Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rank Top Product Segments

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

Your question is Rank Top Product Segments. 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

CNA wants to compare product segments using policy premium and incurred loss data. Write a PostgreSQL query that ranks the top three qualifying segments by total annual premium.

Requirements

  1. Join products, policies, and policy claim summaries.
  2. Include only policies effective in 2025 with policy_status = 'Active' and positive annual premium.
  3. Use a CTE to calculate policy count, total premium, total incurred loss, and loss ratio by product segment. Treat missing incurred loss as zero.
  4. Use RANK() to return all segments within the top three ranks, requiring at least two qualifying policies per segment. Sort by rank and total premium descending.

Schema

products
ColumnTypeDescription
product_idPKINTUnique CNA product identifier
product_nameVARCHAR(100)CNA insurance product name
product_segmentVARCHAR(80)Business segment used for ranking
product_lineVARCHAR(80)Insurance product line
policies
ColumnTypeDescription
policy_idPKINTUnique policy identifier
product_idINTReferenced product identifier
effective_dateDATEPolicy effective date
policy_statusVARCHAR(20)Current policy status
annual_premiumNUMERIC(12,2)Annual policy premium
policy_claim_summary
ColumnTypeDescription
policy_idPKINTPolicy identifier represented in the claim summary
incurred_lossNUMERIC(12,2)Total incurred loss for the policy
Tablesproductspoliciespolicy_claim_summary
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results