Summarize each NFT's traits and compute its combined rarity as a product of trait weights

Quick 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.

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.

Coinbase logo
Coinbase
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

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

ColumnTypeMeaning
nft_idINTUnique id of the NFT
token_nameVARCHAR(100)Display name of the token (NOT NULL)
minted_atTIMESTAMPWhen the token was minted (NOT NULL)

Primary key: nft_id.

trait_categories

ColumnTypeMeaning
category_idINTUnique id of the trait category
category_nameVARCHAR(50)Name of the category, such as Background (NOT NULL, unique)

Primary key: category_id.

traits

ColumnTypeMeaning
trait_idINTUnique id of the trait
category_idINTCategory the trait belongs to (NOT NULL)
trait_valueVARCHAR(50)Value of the trait, such as Gold (NOT NULL)
rarity_weightNUMERIC(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

ColumnTypeMeaning
nft_idINTThe NFT
trait_idINTA 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_idcategory_name
1Hat
2Background
3Eyes

traits

trait_idcategory_idtrait_valuerarity_weight
102Blue0.600
112Gold0.050
203Laser0.100
213Sleepy0.700
301Crown0.020
311Beanie0.300

nfts

nft_idtoken_nameminted_at
1Pixel Fox #12024-03-01 10:00:00
2Pixel Fox #22024-03-01 10:05:00
3Pixel Fox #32024-03-02 09:30:00
4Pixel Fox #42024-03-02 11:15:00
5Pixel Fox #52024-03-03 08:00:00

nft_traits

nft_idtrait_id
110
121
211
220
230
311
331
420
520

Expected result:

nft_idtoken_nameminted_attraitscombined_rarity
2Pixel Fox #22024-03-01 10:05:00Background: Gold (0.050); Eyes: Laser (0.100); Hat: Crown (0.020)0.000100
3Pixel Fox #32024-03-02 09:30:00Background: Gold (0.050); Hat: Beanie (0.300)0.015000
4Pixel Fox #42024-03-02 11:15:00Eyes: Laser (0.100)0.100000
5Pixel Fox #52024-03-03 08:00:00Eyes: Laser (0.100)0.100000
1Pixel Fox #12024-03-01 10:00:00Background: 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 .