N054-M2 Tier 4 · Advanced · medium ecommerce · Brightlane

Return a JSON object for every product whose `'format'` attribute is `'Paperback'`. The object must have keys `'id'`, `'name'`, and `'pages'`, where `'pages'` is the value extracted from the product's `attributes` under the `'pages'` key

Part of JSONB Aggregation (jsonb_agg, json_build_object) in SQL

The problem

Brightlane's book catalog export pipeline needs a structured JSON document per paperback product that combines standard product columns with a JSONB attribute.

Write a query to return a JSON object for every product whose 'format' attribute is 'Paperback'. The object must have keys 'id', 'name', and 'pages', where 'pages' is the value extracted from the product's attributes under the 'pages' key.

Assumptions:

  • The products table has one row per product with an id, a name, and an attributes JSONB column.
  • A paperback product has its 'format' key set to 'Paperback' and a 'pages' key in attributes.
  • Only paperback products should appear. Each output row carries one JSON document combining the product's id, name, and the text-extracted 'pages' value.

Output:

  • One row per paperback product, with one column, book_spec, containing the JSON document.
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
  JSON_BUILD_OBJECT('id', id, 'name', name, 'pages', attributes ->> 'pages') AS book_spec
FROM
  products
WHERE
  attributes ->> 'format' = 'Paperback'

The shape

json_build_object accepts any expression as a value, including the ->> extraction from a JSONB column. Mixing the relational columns id and name with the JSONB-extracted attributes->>'pages' inside one call produces a single output document per row, blending data from both sources.

Clause by clause

  • SELECT json_build_object('id', id, 'name', name, 'pages', attributes->>'pages') AS book_spec builds the JSON document for each surviving row. The first two values come straight from the column, so id lands as a JSON number and name as a JSON string. The third value comes from attributes->>'pages', which extracts the 'pages' key from the JSONB column as text — that is why "pages": "431" appears in the result with the value quoted as a JSON string.
  • FROM products reads the product records.
  • WHERE attributes->>'format' = 'Paperback' restricts the input to products whose 'format' attribute is the text 'Paperback'. Only four rows survive the filter — Writing Clean Code, The Efficient Developer, Relational Databases in Practice, and Improving Existing Code — and the function runs once per surviving row.

Why this and not ->

attributes->'pages' returns the JSONB value (which would carry the source type — quoted string if stored as a JSONB string, number if stored as a JSONB number). attributes->>'pages' returns text. The prompt does not require the page count as a number, and the reference result shows "pages": "431" as a string, so the text-extracting ->> is the right choice. Forcing a numeric type would be a separate cast and a different problem.

You practiced mixing relational columns and JSONB-extracted values inside json_build_object — the function accepts any expression as a value, including ->>-extracted strings.

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.