Trade In Payouts
Apple has a trade-in program where their customers can return the old iPhone device to Apple and Apple gives the customers the trade-in value (known as payout) of the device in cash.
For each store, write a query of the total revenue from the trade-in. Order the result by the descending order.
|114||iPhone 13 Pro Max||650|
Store 512 had 2 trade-ins: iPhone 12 and 13, so the total payout is 800 (350 + 450).
Goal: Find the total revenue of trade-ins for each store.
We need the trade-in transactions and payout (which is the revenue of trade-ins) to be in the same table.
The trade-in transactions are located in the table whereas the payout is stored separately in the table.
Thus, to calculate the total amount of payout, we first need to join the two tables on the common identifier: . For easier readability & writability, you can alias the table names as and .
Showing you the output for the first 5 rows:
The table contains the information of individual payout transactions with each store id.
In the final step, we’ll sum up the payout amounts and group them by the stores respectively. Remember to sort the results by the total payout in descending order.