Back to questions
As a trading analyst at Bloomberg, your task is to identify specific months when a majority of the FAANG stocks (Facebook, Amazon, Apple, Netflix, Google) experienced a gain in value compared to the previous month, while one stock lagged behind its peers by recording a decrease in value. This analysis involves comparing opening prices from the previous month.
In essence, you're seeking months where 5 out of 6 FAANG stocks demonstrated an increase in value with one stock experiencing a decline.
Write a query to display the month and year in 'Mon-YYYY' format along with the ticker symbol of the stock that underperformed relative to its peers, ordered by month and year (in 'Mon-YYYY' format).
| Column Name | Type | Description |
|---|---|---|
| date | datetime | The specified date (mm/dd/yyyy) of the stock data. |
| ticker | varchar | The stock ticker symbol (e.g., AAPL) for the corresponding company. |
| open | decimal | The opening price of the stock at the start of the trading day. |
| high | decimal | The highest price reached by the stock during the trading day. |
| low | decimal | The lowest price reached by the stock during the trading day. |
| close | decimal | The closing price of the stock at the end of the trading day. |
Note that the table below displays data in June and July 2023.
| date | ticker | open | high | low | close |
|---|---|---|---|---|---|
| date | ticker | open | high | low | close |
| 06/30/2023 00:00:00 | AAPL | 191.26 | 191.63 | 194.48 | 193.97 |
| 06/30/2023 00:00:00 | GOOG | 123.50 | 129.55 | 116.91 | 120.97 |
| 06/30/2023 00:00:00 | AMZN | 120.69 | 131.49 | 119.93 | 130.36 |
| 06/30/2023 00:00:00 | META | 265.90 | 289.79 | 258.88 | 286.98 |
| 06/30/2023 00:00:00 | MSFT | 325.93 | 351.47 | 322.50 | 340.54 |
| 06/30/2023 00:00:00 | NFLX | 397.41 | 448.65 | 393.08 | 440.49 |
| 07/31/2023 00:00:00 | AAPL | 195.26 | 196.06 | 196.49 | 196.45 |
| 07/31/2023 00:00:00 | GOOG | 120.32 | 134.07 | 115.83 | 133.11 |
| 07/31/2023 00:00:00 | AMZN | 130.82 | 136.65 | 125.92 | 133.68 |
| 07/31/2023 00:00:00 | META | 286.65 | 326.11 | 284.85 | 318.60 |
| 07/31/2023 00:00:00 | MSFT | 339.19 | 366.78 | 327.00 | 335.92 |
| 07/31/2023 00:00:00 | NFLX | 439.76 | 485.00 | 411.88 | 438.97 |
| mth_yr | underperforming_stock |
|---|---|
| Jul-2023 | GOOG |
In July 2023, the GOOG stock underperformed relative to the other five FAANG stocks which is exhibited by comparing their opening prices in June 2023. The remaining FAANG stocks performed better in July 2023, except for GOOG which experienced a decline in value.
The dataset you are querying against may have different input & output - this is just an example!
Our first task is to extract the opening prices for the current and previous months. To do so, we use the to fetch the previous month's opening price (). By extracting the previous month's opening price, we can compare it with the current month's opening price to determine if there was a gain in value.
Additionally, we use the TO_CHAR() function to format the date into the 'Mon-YYYY' format.
Output displaying the first five rows:
| mth_yr | ticker | prev_open | curr_open |
|---|---|---|---|
| Jan-2020 | AAPL | NULL | 74.06 |
| Feb-2020 | AAPL | 74.06 | 76.07 |
| Mar-2020 | AAPL | 76.07 | 70.57 |
| Apr-2020 | AAPL | 70.57 | 61.63 |
| May-2020 | AAPL | 61.63 | 71.56 |
Next, we create a CTE named 'monthly_changes' using the query from Step 1.
In the main query, we identify the months during which each stock experienced a gain in value represented as . This involves using a , where a value of 1 is assigned when the current month's opening price is higher than the previous month's opening price (). Otherwise, a value of 0 is assigned.
By determining the gain for each stock and month, we can identify which stocks performed well in each month.
The output displays the individual gain counts for February 2020:
| mth_yr | ticker | gain_count |
|---|---|---|
| Feb-2020 | AAPL | 1 |
| Feb-2020 | AMZN | 1 |
| Feb-2020 | GOOG | 1 |
| Feb-2020 | META | 0 |
| Feb-2020 | MSFT | 1 |
| Feb-2020 | NFLX | 1 |
In February 2020, AAPL, AMZN, GOOG, MSFT, and NFLX each recorded a gain count of 1 indicating an increase in value. In contrast, META had a gain count of 0, signifying no gain.
This means that while 5 out of 6 FAANG stocks exhibit an increase in value, one stock (META) experienced a decline in value.
Similarly, we create a CTE called 'monthly_gains', building upon the query from Step 2.
In this step, we summarize the stock performance for each month by utilizing the to calculate total gains.
Additionally, we use a to identify underperforming stocks for a given month, checking:
Assign the ticker if both conditions are met, otherwise assign NULL.
By summarizing the stock performance, we can identify months where 5 out of 6 stocks gained in value and one stock underperformed compared to its peers.
The output displays the stock performance for February 2020, highlighting the identified underperforming stock:
| mth_yr | ticker | total_gains | underperforming_stock |
|---|---|---|---|
| Feb-2020 | AAPL | 5 | NULL |
| Feb-2020 | AMZN | 5 | NULL |
| Feb-2020 | GOOG | 5 | NULL |
| Feb-2020 | META | 5 | META |
| Feb-2020 | MSFT | 5 | NULL |
| Feb-2020 | NFLX | 5 | NULL |
The column identifies any stocks that underperformed relative to their peers. In this instance, the stock with the ticker 'META' is flagged as underperforming as it recorded a gain count of 0 amidst a total gain count of 5 for the month.
Likewise, we create a CTE called 'stock_summary' based on the query in Step 3.
In the final query, we refine the results by filtering for months where the total gains amount to 5 () and an underperforming stock is identified (). The results are then ordered by month-year.
The final output showing the months where these FAANG stocks showed underperformance compared to the other 5 stocks.
| mth_yr | underperforming_stock |
|---|---|
| Feb-2020 | META |
| Jul-2020 | GOOG |
| Jul-2023 | GOOG |
| May-2021 | NFLX |
| May-2023 | NFLX |
| Nov-2021 | META |