Top Ten ZIP Codes by Population

Quick Overview

Write a SQL query that returns the ten ZIP codes with the most people, counting one row per person and breaking ties by ZIP code. It tests GROUP BY with COUNT(*), how NULL columns affect counts, string keys that keep leading zeros, and deterministic ordering with LIMIT.

Top Ten ZIP Codes by Population

Company: Fortinet

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Each row in the `people` table is one person. Write a SQL query that returns the `zip` and `population` of the ten most populated ZIP codes. ### Input Table(s) `people` | Column | Type | Meaning | |---|---|---| | id | INTEGER | Person identifier | | first_name | VARCHAR(50) | First name, may be NULL | | last_name | VARCHAR(50) | Last name, may be NULL | | address | VARCHAR(200) | Street address, may be NULL | | zip | VARCHAR(10) | ZIP code stored as a string, never NULL | Primary key: `id`. ### Output Contract - Columns, in this order: `zip`, `population`. - `population` is the number of rows in `people` with that `zip`. Count every row with `COUNT(*)`, including people whose names are `NULL` and people who share the same name. - Sort by `population` in descending order, then by `zip` in ascending order to break ties. - Return at most 10 rows. If several ZIP codes tie for 10th place, still return only 10 rows. Do not include every tied row. - If there are fewer than ten ZIP codes, return all of them. If the table is empty, return no rows. ### Example `people` | id | first_name | last_name | address | zip | |---|---|---|---|---| | 1 | John | Smith | 12 Oak St | 10001 | | 2 | Jane | Doe | 45 Pine Ave | 10001 | | 3 | Mike | Brown | 7 Elm Rd | 02134 | | 4 | NULL | NULL | 99 Maple Dr | 10001 | | 5 | Sara | Lee | 3 Birch Ln | 02134 | | 6 | Tom | Clark | 8 Cedar Ct | 60614 | Expected output | zip | population | |---|---| | 10001 | 3 | | 02134 | 2 | | 60614 | 1 | ZIP 10001 has 3 people. The person with NULL names still counts because `COUNT(*)` counts rows. 02134 has 2 people and keeps its leading zero. 60614 has 1. There are fewer than ten ZIP codes, so all three are returned. ### Constraints and Clarifications - The `people` table has 0 to 100,000 rows. - `id` is unique for every row. - `zip` is never `NULL` and is at most 10 characters long. - `first_name`, `last_name` and `address` may be `NULL`. - `zip` is stored as a string, so leading zeros are kept (for example, `'02134'` and `'2134'` are different ZIP codes). The tiebreak compares `zip` as a string, so `'02134'` sorts before `'10001'`.

Overview: Write a SQL query that returns the ten ZIP codes with the most people, counting one row per person and breaking ties by ZIP code. It tests GROUP BY with COUNT(*), how NULL columns affect counts, string keys that keep leading zeros, and deterministic ordering with LIMIT.

Fortinet logo
Fortinet
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Each row in the people table is one person. Write a SQL query that returns the zip and population of the ten most populated ZIP codes.

Input Table(s)

people

ColumnTypeMeaning
idINTEGERPerson identifier
first_nameVARCHAR(50)First name, may be NULL
last_nameVARCHAR(50)Last name, may be NULL
addressVARCHAR(200)Street address, may be NULL
zipVARCHAR(10)ZIP code stored as a string, never NULL

Primary key: id.

Output Contract

  • Columns, in this order: zip , population .
  • population is the number of rows in people with that zip . Count every row with COUNT(*) , including people whose names are NULL and people who share the same name.
  • Sort by population in descending order, then by zip in ascending order to break ties.
  • Return at most 10 rows. If several ZIP codes tie for 10th place, still return only 10 rows. Do not include every tied row.
  • If there are fewer than ten ZIP codes, return all of them. If the table is empty, return no rows.

Example

people

idfirst_namelast_nameaddresszip
1JohnSmith12 Oak St10001
2JaneDoe45 Pine Ave10001
3MikeBrown7 Elm Rd02134
4NULLNULL99 Maple Dr10001
5SaraLee3 Birch Ln02134
6TomClark8 Cedar Ct60614

Expected output

zippopulation
100013
021342
606141

ZIP 10001 has 3 people. The person with NULL names still counts because COUNT(*) counts rows. 02134 has 2 people and keeps its leading zero. 60614 has 1. There are fewer than ten ZIP codes, so all three are returned.

Constraints and Clarifications

  • The people table has 0 to 100,000 rows.
  • id is unique for every row.
  • zip is never NULL and is at most 10 characters long.
  • first_name , last_name and address may be NULL .
  • zip is stored as a string, so leading zeros are kept (for example, '02134' and '2134' are different ZIP codes). The tiebreak compares zip as a string, so '02134' sorts before '10001' .