Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
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

Your question is SQL Subquery for Top Results. Start with the requirements and the three 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
Sign up free to run your codeLog inLn 1
Run your query to see results here.