Description
Table: Users
| Column Name | Type |
|---|---|
| user_id | int |
| join_date | date |
| favorite_brand | varchar |
user_idis the primary key (column with unique values) of this table.- This table has the info of the users of an online shopping website where users can sell and buy items.
Table: Orders
| Column Name | Type |
|---|---|
| order_id | int |
| order_date | date |
| item_id | int |
| buyer_id | int |
| seller_id | int |
order_idis the primary key (column with unique values) of this table.item_idis a foreign key (reference column) to the Items table.buyer_idandseller_idare foreign keys to the Users table.
Table: Items
| Column Name | Type |
|---|---|
| item_id | int |
| item_brand | varchar |
item_idis the primary key (column with unique values) of this table.
Problem Statement
Write a solution to find for each user whether the brand of the second item (by date) they sold is their favorite brand. If a user sold less than two items, report the answer for that user as ’no’. It is guaranteed that no seller sells more than one item in a day.
Return the result table in any order. The result format is in the following example.
Example 1:
Input:
Userstable:
| user_id | join_date | favorite_brand |
|---|---|---|
| 1 | 2019-01-01 | Lenovo |
| 2 | 2019-02-09 | Samsung |
| 3 | 2019-01-19 | LG |
| 4 | 2019-05-21 | HP |
Orderstable:
| 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 |
| 6 | 2019-08-05 | 2 | 2 | 4 |
Itemstable:
| item_id | item_brand |
|---|---|
| 1 | Samsung |
| 2 | Lenovo |
| 3 | LG |
| 4 | HP |
Output:
| seller_id | 2nd_item_fav_brand |
|---|---|
| 1 | no |
| 2 | yes |
| 3 | yes |
| 4 | no |
Explanation:
- The answer for the user with id 1 is no because they sold nothing.
- The answer for the users with id 2 and 3 is yes because the brands of their second sold items are their favorite brands.
- The answer for the user with id 4 is no because the brand of their second sold item is not their favorite brand.
solution
The problem can be solved in step-by-step manner.
- First, you need to find the second order for each seller. For this, you can use
ROW_NUMBER()window function on theOrderstable.
1WITH second_orders AS (
2 SELECT seller_id, item_id,
3 ROW_NUMBER() OVER (PARTITION BY seller_id ORDER BY order_date) row_num
4 FROM Orders
5) SELECT * FROM second_orders
6 WHERE row_num = 2;
| seller_id | item_id | row_num |
|---|---|---|
| 2 | 1 | 2 |
| 3 | 3 | 2 |
| 4 | 2 | 2 |
- Now, if you want to check whether it’s user’s favorite brand, you need to check with
Itemstable. BecauseUserstable does not have reference toitem_idbut it has reference ofitem_brandasfavorite_brand, you need to fetch brand information fromItemstable by joining with it.
1WITH second_orders AS (
2 SELECT seller_id, item_id,
3 ROW_NUMBER() OVER (PARTITION BY seller_id ORDER BY order_date) row_num
4 FROM Orders
5), second_order_with_brand AS (
6 SELECT so.seller_id, i.item_brand
7 FROM second_orders so
8 JOIN Items i
9 ON so.item_id = i.item_id
10 WHERE so.row_num = 2
11) SELECT * FROM second_order_with_brand;
| seller_id | item_brand |
|---|---|
| 2 | Samsung |
| 4 | Lenovo |
| 3 | LG |
- Here, we have information for only 3 users, but in the output we need to report for each user. So, we need to join with
Userstable withUsers LEFT OUTER JOIN second_order_with_brand. At this point, you can also check if theitem_brandfromsecond_order_with_brandmatchesfavorite_brandfromUserstable. If yes, then you putyesas2nd_item_fav_brandelseno.
1WITH second_orders AS (
2 SELECT seller_id, item_id,
3 ROW_NUMBER() OVER (PARTITION BY seller_id ORDER BY order_date) row_num
4 FROM Orders
5) , second_order_with_brand AS (
6 SELECT so.seller_id, i.item_brand
7 FROM second_orders so
8 JOIN Items i
9 ON so.item_id = i.item_id
10 WHERE so.row_num = 2
11) SELECT
12 u.user_id seller_id,
13 CASE WHEN u.favorite_brand = sb.item_brand THEN 'yes' ELSE 'no' END AS 2nd_item_fav_brand
14 FROM Users u
15 LEFT OUTER JOIN second_order_with_brand sb
16 ON u.user_id = sb.seller_id;


Comments