N050-M1 Tier 4 · Advanced · medium ecommerce · Brightlane

Return every order ID and its product IDs as a comma-separated string, with the IDs arranged in ascending numeric order

Part of STRING_AGG and ARRAY_AGG in SQL

The problem

Brightlane's order processing team needs a line-item summary per order showing the product IDs that were included as a comma-separated text string.

Write a query to return every order ID and its product IDs as a comma-separated string, with the IDs arranged in ascending numeric order.

Assumptions:

  • The order_items table has one row per line item with an order_id and a product_id.
  • Each order_id with at least one line item should appear once.
  • For each order, the product-IDs list contains every product_id value of items in that order (one entry per line item, no de-duplication), with the IDs sorted numerically (not lexically) before being assembled with ', ' between adjacent values.

Output:

  • One row per order with at least one line item, with columns order_id and product_ids.
Schema · ecommerce 5 tables
categories
id integer
name text
parent_id? integer
products
id integer
name text
category_id integer
price numeric
stock_qty integer
attributes? jsonb
order_items
id integer
order_id integer
product_id integer
quantity integer
unit_price numeric
customers
id integer
name text
email text
city? text
country text
created_at timestamptz
is_active boolean
orders
id integer
customer_id integer
ordered_at timestamptz
status text
total_amount numeric

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution Try it yourself first
Solution query
SELECT
  order_id,
  STRING_AGG(
    product_id::TEXT,
    ', '
    ORDER BY
      product_id
  ) AS product_ids
FROM
  order_items
GROUP BY
  order_id

The shape

STRING_AGG(product_id::text, ', ' ORDER BY product_id) joins each order's product IDs into a single text value, with the IDs sorted by their numeric value before they are concatenated. The ::text cast handles the type requirement of STRING_AGG without changing the sort order, because the ORDER BY is evaluated against the original numeric column.

Clause by clause

  • SELECT order_id, STRING_AGG(product_id::text, ', ' ORDER BY product_id) AS product_ids returns the order ID alongside the concatenated product-ID list. STRING_AGG requires its first argument to be text, which is why product_id::text converts the integer column into text values the aggregate can join. The ORDER BY product_id references the original integer column, so the sequence is 14, 22, 32 — numeric order — not the lexical order a text sort would give (14, 22, 32 happens to match here, but on IDs like 2, 11, 100 a text sort would produce 100, 11, 2 instead). The ', ' literal sits between adjacent values.
  • FROM order_items reads the line-item rows. Every line item contributes to its order's list.
  • GROUP BY order_id partitions the rows by order so the aggregate runs once per order. One output row per distinct order_id.

Why this and not STRING_AGG(product_id::text, ', ' ORDER BY product_id::text)

Both compile, but they produce different results once IDs cross digit-length boundaries. Sorting by the cast text orders the values lexically: '100' sorts before '11' because '0' comes before '1' character by character. Sorting by the underlying product_id orders them numerically, which is what the prompt asks for. Cast for concatenation, sort against the typed source.

You practiced STRING_AGG(column::text, separator ORDER BY column) — cast a numeric column to text for concatenation while keeping the numeric ordering intact.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.