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
| product_id | product_name | category | price | rating | in_stock |
|---|---|---|---|---|---|
| 1001 | Basic Device 1X | Books | 17.52 | 2.7 | 1 |
| 1050 | Deluxe Bundle 2X | Clothing | 25.05 | 4.4 | 1 |
| 1099 | Pro Unit 3X | Home & Kitchen | 32.58 | 2.1 | 1 |
| 1148 | Ultra Tool 4X | Collectibles | 40.11 | 3.8 | 1 |
| 1197 | Essential Set 5X | Toys | 47.64 | 1.5 | 1 |
| 1246 | Classic System 6X | Beauty | 0 | 3.2 | 1 |
| 1295 | Smart Gadget 7X | Automotive | 62.7 | 4.9 | 0 |
| 1344 | Eco Kit 8X | Garden | 70.23 | 2.6 | 1 |
| 1393 | Turbo Pack 9X | Music | 77.76 | 4.3 | 0 |
| 1442 | Premium Widget 10X | Electronics | 85.29 | 2 | 1 |
Expected output
| product_name | category | rating | position |
|---|---|---|---|
| Smart Gadget 7X | Automotive | 4.9 | 1 |
| Smart Gadget 47X | Automotive | 4.9 | 1 |
| Smart Gadget 37X | Automotive | 3.9 | 2 |
| Smart Gadget 27X | Automotive | 2.9 | 3 |
| Smart Gadget 17X | Automotive | 1.9 | 4 |
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
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 NULLSELECT
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 NULLCAST(rating AS INTEGER) rounds 4.5 up to 5, so Beauty's 1.2 product comes back at position 5.
Common follow-up questions
- What if the team wanted half-star tiers (4.5, 4.0, 3.5) instead of whole stars? (Integer-divide the tenths count by 5 instead of 10: the cast already did the hard part.)
- How would you return only the top 2 star tiers per category? (Wrap the query and filter `position <= 2`; shared tiers mean a category can return more than 2 rows.)
- What changes if `rating` were stored as TEXT like '4.9'? (Text sorts character by character, so '10.0' sorts before '9.0'; convert to a number before counting tenths.)