The Weight of Between

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
Asked in
Amazon

Problem

Our small-charge review keeps missing allocations that sit exactly on the edges of its 100 to 300 band. Pull every allocation in that band from cost_allocs, edges included, as one team-and-service label beside its amount and region, smallest amount first.

Between the floor and the ceiling, the ledger keeps its quiet accounts. Sort what rests there.

Tables

cost_allocs

Columns: alloc_id INTEGER, team_name TEXT, svc_name TEXT, region TEXT, amount REAL, period TEXT, acct_id TEXT, category TEXT

cost_allocs: 10 of 200 rows
alloc_idteam_namesvc_nameregionamountperiodacct_idcategory
129data-engS3us-west-283.212026-02acct-101storage
158ml-teamRDSeu-west-1156.422026-03acct-102network
187frontendLambdaap-south-1229.632026-04acct-103database
216backendBigQueryus-central1-120.52026-05acct-104ml
245devopsCloudFronteurope-west1376.052026-06acct-105other
274securityEKSap-southeast-1449.262026-07acct-106compute
303mobileSQSeu-central-1522.472026-08acct-107storage
332PlatformEC2us-east-1595.682026-09NULLnetwork
361DATA-ENGS3us-west-2668.892026-01acct-109database
390platformRDSeu-west-1742.12026-02acct-110ml

Expected output

First 5 of 7 rows
labelamountregion
legal - EC2100us-east-1
finance - S3150us-west-2
ml-team - RDS156.42eu-west-1
frontend - Lambda229.63ap-south-1
marketing - EC2300us-central1

Worked solution and explanation

What this really is

Strip off the review costume and this is a closed-interval filter feeding a string assembly: keep the rows whose amount lands in 100 to 300 with both edges in, then glue team_name and svc_name into one label. Everyone gets the label. The edge is what separates answers. Two of the seven allocations in the band sit exactly on it: 'legal - EC2' at 100.00 and 'marketing - EC2' at 300.00. Write amount > 100 AND amount < 300 and both rows vanish without an error. The review then shows five rows, which is the very symptom the prompt describes.


Break it down

Step 1: Keep the band, edges included

Filter with amount BETWEEN 100 AND 300. BETWEEN is closed on both ends, so the 100.00 and 300.00 allocations stay. amount >= 100 AND amount <= 300 is the same predicate. Only the strict form is wrong.

Step 2: Assemble one label column

Build team_name || ' - ' || svc_name AS label. The preview shows both names in one column with ' - ' between them. Two separate columns fail the shape check even when every value is right.

Step 3: Sort smallest charge first

ORDER BY amount sorts ascending, matching how the review reads the band. The seven amounts in range are all different, so one key fully determines the sequence.

Inclusive band, one label, ascending amount
SELECT
    team_name || ' - ' || svc_name AS label,
    amount,
    region
FROM cost_allocs
WHERE amount BETWEEN 100 AND 300
ORDER BY amount
Strict comparison
amount > 100 AND amount < 300 returns five rows. 'legal - EC2' and 'marketing - EC2' are gone, and nothing in the output says so.
Closed band
amount BETWEEN 100 AND 300 returns all seven, with the two edge allocations first and last.

Common follow-up questions