Summarize each NFT's traits and compute its combined rarity as a product of trait weights
Company: Coinbase
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
An NFT collection stores each token's traits across four tables. For every NFT, return its id, token name and mint time, a text summary of its traits, and a combined rarity score equal to the product of its traits' `rarity_weight` values, rounded to 6 decimal places.
Each entry of the trait summary has the form `category_name: trait_value (rarity_weight)`. Entries are joined with `; ` and ordered by `category_name`, then `trait_value`.
### Input Table(s)
`nfts`
| Column | Type | Meaning |
|---|---|---|
| nft_id | INT | Unique id of the NFT |
| token_name | VARCHAR(100) | Display name of the token (NOT NULL) |
| minted_at | TIMESTAMP | When the token was minted (NOT NULL) |
Primary key: `nft_id`.
`trait_categories`
| Column | Type | Meaning |
|---|---|---|
| category_id | INT | Unique id of the trait category |
| category_name | VARCHAR(50) | Name of the category, such as Background (NOT NULL, unique) |
Primary key: `category_id`.
`traits`
| Column | Type | Meaning |
|---|---|---|
| trait_id | INT | Unique id of the trait |
| category_id | INT | Category the trait belongs to (NOT NULL) |
| trait_value | VARCHAR(50) | Value of the trait, such as Gold (NOT NULL) |
| rarity_weight | NUMERIC(4,3) | Rarity weight of the trait, strictly between 0 and 1 (NOT NULL) |
Primary key: `trait_id`. Foreign key: `traits.category_id` references `trait_categories.category_id`.
`nft_traits`
| Column | Type | Meaning |
|---|---|---|
| nft_id | INT | The NFT |
| trait_id | INT | A trait the NFT has |
Primary key: `(nft_id, trait_id)`. Foreign keys: `nft_traits.nft_id` references `nfts.nft_id` and `nft_traits.trait_id` references `traits.trait_id`.
### Output Contract
- Columns, in this order: `nft_id`, `token_name`, `minted_at`, `traits`, `combined_rarity`.
- `traits` is text: one entry per trait in the form `category_name: trait_value (rarity_weight)`, ordered by `category_name` ascending, then `trait_value` ascending, separated by a semicolon and one space, with no trailing separator. `rarity_weight` appears in its stored form with three decimals (`0.050`, not `0.05`).
- `combined_rarity` is the product of the NFT's `rarity_weight` values, rounded to 6 decimal places.
- One row per NFT.
- Order by the rounded `combined_rarity` ascending, then `nft_id` ascending.
### Example
`trait_categories`
| category_id | category_name |
|---|---|
| 1 | Hat |
| 2 | Background |
| 3 | Eyes |
`traits`
| trait_id | category_id | trait_value | rarity_weight |
|---|---|---|---|
| 10 | 2 | Blue | 0.600 |
| 11 | 2 | Gold | 0.050 |
| 20 | 3 | Laser | 0.100 |
| 21 | 3 | Sleepy | 0.700 |
| 30 | 1 | Crown | 0.020 |
| 31 | 1 | Beanie | 0.300 |
`nfts`
| nft_id | token_name | minted_at |
|---|---|---|
| 1 | Pixel Fox #1 | 2024-03-01 10:00:00 |
| 2 | Pixel Fox #2 | 2024-03-01 10:05:00 |
| 3 | Pixel Fox #3 | 2024-03-02 09:30:00 |
| 4 | Pixel Fox #4 | 2024-03-02 11:15:00 |
| 5 | Pixel Fox #5 | 2024-03-03 08:00:00 |
`nft_traits`
| nft_id | trait_id |
|---|---|
| 1 | 10 |
| 1 | 21 |
| 2 | 11 |
| 2 | 20 |
| 2 | 30 |
| 3 | 11 |
| 3 | 31 |
| 4 | 20 |
| 5 | 20 |
Expected result:
| nft_id | token_name | minted_at | traits | combined_rarity |
|---|---|---|---|---|
| 2 | Pixel Fox #2 | 2024-03-01 10:05:00 | Background: Gold (0.050); Eyes: Laser (0.100); Hat: Crown (0.020) | 0.000100 |
| 3 | Pixel Fox #3 | 2024-03-02 09:30:00 | Background: Gold (0.050); Hat: Beanie (0.300) | 0.015000 |
| 4 | Pixel Fox #4 | 2024-03-02 11:15:00 | Eyes: Laser (0.100) | 0.100000 |
| 5 | Pixel Fox #5 | 2024-03-03 08:00:00 | Eyes: Laser (0.100) | 0.100000 |
| 1 | Pixel Fox #1 | 2024-03-01 10:00:00 | Background: Blue (0.600); Eyes: Sleepy (0.700) | 0.420000 |
NFT 2 scores 0.050 × 0.100 × 0.020 = 0.0001, the rarest. NFT 3's summary lists Background before Hat because entries sort by category name, even though Hat has the smaller `category_id`. NFTs 4 and 5 tie at 0.100000 and are ordered by `nft_id`.
### Constraints and Clarifications
- Every NFT has at least one trait and at most one trait per category.
- 0 < `rarity_weight` < 1.
- At most 10 trait categories and 10,000 NFTs.
- No column contains NULL, and the primary key on `nft_traits` means a trait is never attached twice to the same NFT.
- The sort uses the rounded score, so NFTs whose products round to the same 6-decimal value are ordered by `nft_id`.
```hint Multiplying across rows
PostgreSQL has no `PRODUCT()` aggregate. Look for an identity that rewrites a product in terms of an aggregate it does have.
```
Overview: Write a PostgreSQL query that joins four NFT tables, builds an ordered text summary of each token's traits, and scores its rarity as the product of its trait weights. It tests ordered string aggregation, multiplying values across rows without a product aggregate, rounding, and deterministic sorting.