N045-E1 Tier 4 · Advanced · easy ecommerce · Brightlane

Return every product's ID, name, price, and the product's price tier across the catalog. Sort the final result by `price` ascending

Part of NTILE and Percentile Functions in SQL

The problem

Brightlane's merchandising team is segmenting the product catalog into four price tiers for promotional planning.

Write a query to return every product's ID, name, price, and the product's price tier across the catalog. Sort the final result by price ascending.

Assumptions:

  • Products are sorted by price ascending and assigned to one of four tiers based on position. Tier 1 covers the lowest-priced quarter of products by row count; tier 4 covers the highest-priced quarter.
  • When the row count does not divide evenly by 4, the earlier tiers each receive one extra record.
  • Two products with identical price values may land in different tiers if they fall on opposite sides of a tier boundary.
  • The final result is sorted by price ascending.

Output:

  • One row per product, with columns id, name, price, and price_quartile. Sorted by price ascending.
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
  id,
  name,
  price,
  NTILE(4) OVER (
    ORDER BY
      price
  ) AS price_quartile
FROM
  products
ORDER BY
  price

The shape

NTILE(4) over an ascending price sort splits the catalog into four equal-row tiers and stamps each product with its tier number. The window function does the sort, the count, and the labelling in one pass. The outer ORDER BY price makes the printed result line up tier 1 first.

Clause by clause

  • SELECT id, name, price, NTILE(4) OVER (ORDER BY price) AS price_quartile returns the four columns the merchandising team needs. NTILE(4) walks the products in ascending price order and assigns the first quarter of rows to bucket 1, the next quarter to bucket 2, and so on. The ORDER BY price inside OVER is what defines the sort the buckets are cut from.
  • FROM products reads the full catalog. There is no WHERE, so every product participates in the bucketing.
  • ORDER BY price is the outer sort. It controls the printed sequence so the result reads from cheapest to most expensive, which is also the order the tiers ascend in.

Why this and not ROW_NUMBER divided into groups

ROW_NUMBER would give you a sequential rank from 1 to N, and you would then have to derive a tier from that rank yourself. NTILE(4) is the purpose-built function for the question being asked: cut the sorted rows into four equal-row buckets. One function, one column, no arithmetic.

The trap

NTILE splits by row position, not by price gaps. Two products that share an identical price near a bucket boundary can land on opposite sides of that boundary. The reference result shows it directly: HDMI Cable 2m and Picture Frame 8x10 both cost 12.99 and both land in tier 1, but the same kind of tie at the 16th-to-17th row boundary would put two equal-priced rows in different tiers. The team should read price_quartile as "the bottom quarter of products by row count," not "every product below a hard price cutoff."

You practiced NTILE(4) OVER (ORDER BY ...) — distribute records into four position-based buckets; the assignment is by row-count, not by value range.

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.