Roll Call

An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
easy
Seniority
L3

Problem

Support needs a clean roster of everyone in the customer table. List each customer's first name, last name, and country, ordered alphabetically by first name, then by last name, and finally by country so identical names still fall in a fixed sequence.

Every name on the books, in order.

Tables

customers

Columns: customer_id INTEGER, first_name TEXT, last_name TEXT, country TEXT

customers: 10 of 200 rows
customer_idfirst_namelast_namecountry
40089PriyaGuptaIndia
40178LucaRossiItaly
40267AvaNguyenVietnam
40356EthanJohnsonSpain
40445MariaGarciaChina
40534ChenWeiRussia
40623SofiaPetrovaIreland
40712LiamO'ConnorMexico
40801IsabellaMartinezCanada
40890NoahBrownFrance

orders

Columns: order_id INTEGER, status TEXT, region TEXT, profit REAL

orders: 10 of 200 rows
order_idstatusregionprofit
10097PendingEU69.37
10194CancelledAPAC88.74
10291ShippedLATAM108.11
10388ProcessingMEA127.48
10485ReturnedUS146.85
10582On HoldEU166.22
10679CompletedAPAC185.59
10776PendingLATAM204.96
10873CancelledMEA224.33
10970ShippedUS243.7

Expected output

First 5 of 200 rows
first_namelast_namecountry
AidenKimBangladesh
AidenKimUnited States
AidenSmithBangladesh
AidenSmithFrance
AlexanderBrownItaly

Worked solution and explanation

What this is really asking

Strip the costume and this is a single-table projection wearing a two-table schema. You are handed customers and orders and an easy-sounding ask, so the reflex is to reach for a join. But look at the keys: orders holds order_id, status, region, and profit, and there is no customer_id anywhere in it. There is nothing to join on. The skill being probed is discipline: read exactly what is asked, pull it from the one table that has it, and leave the distractor alone. Reach for orders and you either get an all-null column from a key that does not mean anything, or you fan every customer across every order.

Building it

Step 1: Start from the table that actually holds the data

All three requested fields (first_name, last_name, country) sit in customers. That single fact decides the FROM clause. No join, no subquery, no CTE.

Step 2: Project only the three columns asked for

Name the columns explicitly instead of SELECT star. The roster is a clean deliverable for support, so extra columns like customer_id are noise. Listing the columns also documents the exact contract of the output.

Step 3: Sort on two keys, in order

Sort by first_name first, then last_name. The second key is not decoration: when two people share a first name, it is the only thing that gives a stable, predictable order. Drop it and those rows come back in whatever order the engine happens to scan them.

The roster
SELECT first_name, last_name, country
FROM customers
ORDER BY first_name, last_name, country;

One table, three columns, a two-key sort. Nothing more is needed.

The reflex (wrong)
SELECT c.first_name, c.last_name, c.country, o.status FROM customers c LEFT JOIN orders o ON c.customer_id = o.order_id. The join key pairs a customer id against an order id, two unrelated number spaces, so status comes back null for every row and you have added a column nobody asked for.
The answer
SELECT first_name, last_name, country FROM customers ORDER BY first_name, last_name. Reads from the only table that carries the requested fields and returns exactly the roster that was asked for.

Common follow-up questions