Rating Tiers

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L4

Problem

The review team sorts each category's products into whole-star tiers, where a product's tier is its rating rounded down to a whole number, so a 4.9 and a 4.2 both count as 4 stars. Within each category, number those tiers from the highest downward, so products in the same tier share a number and the next tier down takes the very next one with no gaps. Skip products with no rating, and return the product_name, category, rating, and that number as position.

No gaps, no skips. Ratings stacked tight within each category.

Tables

products

Columns: product_id INTEGER, product_name TEXT, category TEXT, price REAL, rating REAL, in_stock BOOLEAN

products: 10 of 200 rows
product_idproduct_namecategorypriceratingin_stock
1001Basic Device 1XBooks17.522.71
1050Deluxe Bundle 2XClothing25.054.41
1099Pro Unit 3XHome & Kitchen32.582.11
1148Ultra Tool 4XCollectibles40.113.81
1197Essential Set 5XToys47.641.51
1246Classic System 6XBeauty03.21
1295Smart Gadget 7XAutomotive62.74.90
1344Eco Kit 8XGarden70.232.61
1393Turbo Pack 9XMusic77.764.30
1442Premium Widget 10XElectronics85.2921

Expected output

First 5 of 187 rows
product_namecategoryratingposition
Smart Gadget 7XAutomotive4.91
Smart Gadget 47XAutomotive4.91
Smart Gadget 37XAutomotive3.92
Smart Gadget 27XAutomotive2.93
Smart Gadget 17XAutomotive1.94

Worked solution and explanation

The real question hiding in a star tier

Strip the business words and this is a bucketing step wearing a ranking costume. First a continuous rating collapses into a whole-star tier with the fraction dropped: 4.9 and 4.2 both land in the 4-star tier. Only then do you number those tiers inside each category. Anyone can reach for a window function; the tell is how the fraction goes. A bare CAST(rating AS INTEGER) rounds in PostgreSQL, Trino, Snowflake and most other engines, so Beauty's 4.5 products climb into a 5-star tier and its 1.2 product slides from position 4 to position 5. Order by the raw rating and Beauty's 4.2 and 4.5 split into separate positions. Pick a numbering that skips after shared tiers and Automotive's 3-star tier lands at 6 instead of 2.


Working the requirements

Step 1: Turn rating into a whole-star tier

rating is a REAL with one decimal, so rating * 10 is the rating in tenths of a star, a whole number. CAST(rating * 10 AS INTEGER) gives it an integer type without losing anything, because no fraction is left for a cast to round. An integer divided by the integer 10 truncates, so 49 / 10 is 4 and 42 / 10 is 4. FLOOR(rating) lands on the same tier. What you must never write is a cast of the raw rating. Drop the cast here and rating * 10 / 10 is just rating again, so every distinct rating becomes its own position.

Step 2: Number the tiers per category with DENSE_RANK

PARTITION BY category restarts the count at 1 in every category, and ORDER BY ... DESC gives the highest tier position 1. DENSE_RANK is the one of the 3 ranking functions that lets a tier share its number and keeps the next tier contiguous. In Automotive 5 products share the 4-star tier: DENSE_RANK gives the 3-star tier 2, RANK gives it 6, and ROW_NUMBER hands the five 4-star products 1 through 5.

Step 3: Set unrated products aside first

WHERE rating IS NOT NULL removes the 13 products with no rating before the window ever sees them. Left in, a NULL tier sorts to a spot of its own, picks up a position, and pads the result past the 187 rated products.


The solution

Tenths of a star, integer-divided into whole-star tiers
SELECT
    product_name,
    category,
    rating,
    DENSE_RANK() OVER (
        PARTITION BY category
        ORDER BY CAST(rating * 10 AS INTEGER) / 10 DESC
    ) AS position
FROM products
WHERE rating IS NOT NULL
The cast that reads right and rounds
SELECT
    product_name,
    category,
    rating,
    DENSE_RANK() OVER (
        PARTITION BY category
        ORDER BY CAST(rating AS INTEGER) DESC
    ) AS position
FROM products
WHERE rating IS NOT NULL

CAST(rating AS INTEGER) rounds 4.5 up to 5, so Beauty's 1.2 product comes back at position 5.


Common follow-up questions