Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Subquery for Top Results

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

Your question is SQL Subquery for Top Results. 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

Maersk operations teams need to identify the busiest ports in the January 2026 reporting period. Write a PostgreSQL query that uses a subquery to return the top two ports by total container volume, measured in TEU.

Requirements

  1. Join port_calls, ports, and container_movements.
  2. Include only completed port calls between 2026-01-01 and 2026-01-31, inclusive.
  3. Aggregate TEU by port in an inner subquery.
  4. Return the top two ports, ordered by total TEU descending, with port_code as a deterministic tie-breaker.

Schema

ports
ColumnTypeDescription
port_codePKVARCHAR(10)Maersk port code
port_nameVARCHAR(100)Name of the port
port_calls
ColumnTypeDescription
call_idPKINTUnique port call identifier
port_codeVARCHAR(10)Port visited during the call
call_dateDATEDate of the port call
statusVARCHAR(20)Operational status of the call
vessel_nameVARCHAR(100)Name of the vessel
container_movements
ColumnTypeDescription
movement_idPKINTUnique movement identifier
call_idINTRelated port call
container_idVARCHAR(20)Container identifier
teuINTHandled volume in twenty-foot equivalent units
Tablesportsport_callscontainer_movements
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results