Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Window Function for Top Containers
00:00
5 left

Window Function for Top Containers

MediumSQL · PostgreSQL

Problem

Write a query using a window function to find the top three heaviest containers loaded onto each vessel at Mediterranean Shipping.

Use the vessel and container loading tables. Exclude containers whose weight is NULL. Assign distinct, deterministic positions by sorting heavier containers first and breaking equal-weight ties by container ID.

Output

  1. One row per selected container, with vessel_id, vessel_name, container_id, weight_kg, and weight_rank.
  2. Include up to three containers per vessel, ordered by vessel_id and weight_rank.

Schema

vessels
ColumnTypeDescription
vessel_idPKINTUnique identifier for the vessel
vessel_nameVARCHAR(100)Mediterranean Shipping vessel name
registry_portVARCHAR(100)Port where the vessel is registered
container_loads
ColumnTypeDescription
container_idPKINTUnique identifier for the container loading record
vessel_idINTVessel carrying the container
container_codeVARCHAR(20)Container identification code
weight_kgNUMERIC(10,2)Container gross weight in kilograms
Tablesvesselscontainer_loads
Interviewer

Your question is Window Function for Top Containers. 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.