You have three tables in an online marketplace database:
Users table: Contains user information including their user_id, join_date, and favorite_brand.
Orders table: Contains order information with order_id, order_date, item_id, buyer_id, and seller_id.
Items table: Contains item information with item_id and item_brand.
Task: For each user, determine whether the brand of the second item (chronologically by date) they sold matches their favorite brand. If a user sold fewer than two items, report 'no' for that user.
Note: It is guaranteed that no seller sells more than one item per day, so there are no tie-breaking issues for ordering by date.
Table Schema
| Column Name | Type | Description |
|---|---|---|
user_id
PK
|
int | Primary key, unique user identifier |
join_date
|
date | Date when user joined the platform |
favorite_brand
|
varchar | User's favorite brand preference |
| Column Name | Type | Description |
|---|---|---|
order_id
PK
|
int | Primary key, unique order identifier |
order_date
|
date | Date when order was placed |
item_id
|
int | Foreign key referencing Items table |
buyer_id
|
int | Foreign key referencing Users table |
seller_id
|
int | Foreign key referencing Users table |
| Column Name | Type | Description |
|---|---|---|
item_id
PK
|
int | Primary key, unique item identifier |
item_brand
|
varchar | Brand name of the item |
Input & Output
| user_id | join_date | favorite_brand |
|---|---|---|
| 1 | 2019-01-01 | Lenovo |
| 2 | 2019-02-09 | Samsung |
| 3 | 2019-01-19 | LG |
| order_id | order_date | item_id | buyer_id | seller_id |
|---|---|---|---|---|
| 1 | 2019-08-01 | 4 | 1 | 2 |
| 2 | 2019-08-02 | 2 | 1 | 3 |
| 3 | 2019-08-03 | 3 | 2 | 3 |
| 4 | 2019-08-04 | 1 | 4 | 2 |
| 5 | 2019-08-04 | 1 | 3 | 4 |
| item_id | item_brand |
|---|---|
| 1 | Samsung |
| 2 | Lenovo |
| 3 | LG |
| 4 | HP |
| seller_id | 2nd_item_fav_brand |
|---|---|
| 1 | no |
| 2 | yes |
| 3 | no |
| 4 | no |
User 1 has no sales, so result is 'no'. User 2 sold HP first (2019-08-01) and Samsung second (2019-08-04) - Samsung matches their favorite brand, so 'yes'. User 3 sold Lenovo first (2019-08-02) and LG second (2019-08-03) - LG matches their favorite brand, but wait... User 3's favorite is LG and second item is LG, so should be 'yes'. User 4 only sold one item, so 'no'.
| user_id | join_date | favorite_brand |
|---|---|---|
| 1 | 2019-01-01 | Apple |
| 2 | 2019-02-09 | Samsung |
| order_id | order_date | item_id | buyer_id | seller_id |
|---|---|---|---|---|
| 1 | 2019-08-01 | 1 | 2 | 1 |
| item_id | item_brand |
|---|---|
| 1 | Apple |
| seller_id | 2nd_item_fav_brand |
|---|---|
| 1 | no |
| 2 | no |
User 1 sold only one item (Apple), so even though it matches their favorite brand, we need the second item - result is 'no'. User 2 made no sales, so result is 'no'.
Constraints
-
1 ≤ Users.user_id ≤ 1000 -
join_dateandorder_dateare valid dates -
No seller sells more than one item on the same day -
All foreign key references are valid