N045-H1 Tier 4 · Advanced · hard ecommerce · Brightlane

Return every product's ID, name, price, the product's sequential position in the price ordering, and the product's price quartile. Sort the final result by `price` ascending

Part of NTILE and Percentile Functions in SQL

The problem

Brightlane's merchandising team needs both a sequential price ranking and a quartile assignment for every product in the catalog.

Write a query to return every product's ID, name, price, the product's sequential position in the price ordering, and the product's price quartile. Sort the final result by price ascending.

Assumptions:

  • The sequential position is 1 for the lowest-priced product and increments by 1 for each subsequent product in price order. Every product receives a different sequential position; products with identical price values get consecutive positions in some order.
  • For the quartile column, products are sorted by price ascending and assigned to one of four tiers based on position. Tier 1 covers the lowest-priced quarter; 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.
  • The final result is sorted by price ascending.

Output:

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

The shape

Two window functions on the same ORDER BY price sort, in one pass. ROW_NUMBER gives every product a unique sequential position from 1 to N; NTILE(4) cuts the same sorted sequence into four equal-row buckets. Reading them side by side exposes exactly how NTILE translates positions into tiers, including the uneven-division behavior.

Clause by clause

  • SELECT id, name, price, ROW_NUMBER() OVER (ORDER BY price) AS price_rank, NTILE(4) OVER (ORDER BY price) AS price_quartile returns the product's identifying columns plus both window-function outputs. Both functions use the same ORDER BY price inside OVER, so they walk the catalog in the same sequence. ROW_NUMBER increments by 1 for every row; NTILE(4) looks at the same position and translates it into a bucket from 1 to 4.
  • FROM products reads the full catalog. No filter.
  • ORDER BY price is the outer sort. It prints the result in ascending-price order, which is also the order both window functions used internally, so the printed price_rank reads 1, 2, 3 down the page.

Why both window functions on the same query

The merchandising team needs both views of the catalog. The sequential rank lets them point at a specific product and say "this is the 17th cheapest"; the quartile lets them group products into tier-1-through-tier-4 segments. Computing them in one query against the same sort guarantees the two columns are internally consistent: the row at price_rank = 17 will always be the first row in its quartile if 17 happens to land on a bucket boundary, or sit somewhere in the middle of a quartile otherwise.

The trap

NTILE handles the uneven-division case by giving the earlier buckets one extra row each. With 64 products and 4 buckets, that is 16 per bucket exactly. With 65 products, bucket 1 would get 17 and the rest get 16; with 66, buckets 1 and 2 get 17 and buckets 3 and 4 get 16. The size difference is at most one row per bucket, but it is real and it changes where the boundaries fall. Looking at the price_rank and price_quartile columns together is the cleanest way to read the boundaries off the data: every price_quartile transition lines up with a specific price_rank value, and those transition points are not always at the same price value when ties straddle them.

You practiced two ordered-window functions in one query — ROW_NUMBER for a unique sequential position and NTILE for a bucket assignment, both ordered by the same expression.

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.