Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
SQL Subquery for Top Results
00:00
5 left

SQL Subquery for Top Results

MediumSQL · PostgreSQL

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
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the three tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.