|
| 1 | +# SQLZoo Solutions: COVID-19 Window Functions (LAG) |
| 2 | + |
| 3 | +This document contains my solutions to the SQLZoo ['Window LAG' (COVID-19 Data) section](https://sqlzoo.net/wiki/Window_LAG) using MySQL syntax, along with my personal learning notes and explanations. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Problem 1 |
| 8 | +Modify the query to show data from Spain (instead of Italy). |
| 9 | + |
| 10 | +**My Solution:** |
| 11 | + |
| 12 | +```sql |
| 13 | +SELECT |
| 14 | + name, DAY(whn), confirmed, deaths, recovered |
| 15 | +FROM covid |
| 16 | +WHERE name = 'Spain' |
| 17 | + AND MONTH(whn) = 3 |
| 18 | + AND YEAR(whn) = 2020 |
| 19 | +ORDER BY whn; |
| 20 | +``` |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +## Problem 2 |
| 25 | +Show confirmed cases for the day before, using `LAG`. |
| 26 | + |
| 27 | +**My Solution:** |
| 28 | + |
| 29 | +```sql |
| 30 | +SELECT |
| 31 | + name, DAY(whn), confirmed, |
| 32 | + LAG(confirmed, 1) OVER (PARTITION BY name ORDER BY whn) |
| 33 | +FROM covid |
| 34 | +WHERE name = 'Italy' |
| 35 | + AND MONTH(whn) = 3 |
| 36 | + AND YEAR(whn) = 2020 |
| 37 | +ORDER BY whn; |
| 38 | +``` |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## Problem 3 |
| 43 | +Show the number of new cases for each day in Italy, March 2020. |
| 44 | + |
| 45 | +**My Solution:** |
| 46 | + |
| 47 | +```sql |
| 48 | +SELECT |
| 49 | + name, DAY(whn), |
| 50 | + ( |
| 51 | + confirmed - LAG(confirmed, 1) OVER (PARTITION BY name ORDER BY whn) |
| 52 | + ) AS new_cases |
| 53 | +FROM covid |
| 54 | +WHERE name = 'Italy' |
| 55 | + AND MONTH(whn) = 3 |
| 56 | + AND YEAR(whn) = 2020 |
| 57 | +ORDER BY whn; |
| 58 | +``` |
| 59 | + |
| 60 | +**My Notes:** |
| 61 | +`LAG()` allows comparing to the previous row's confirmed cases. |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +## Problem 4 |
| 66 | +Show the number of new cases in Italy for each week in 2020 (Monday only). |
| 67 | + |
| 68 | +**My Solution:** |
| 69 | + |
| 70 | +```sql |
| 71 | +SELECT |
| 72 | + name, |
| 73 | + DATE_FORMAT(whn,'%Y-%m-%d') AS date, |
| 74 | + ( |
| 75 | + confirmed - LAG(confirmed, 1) OVER (PARTITION BY name ORDER BY whn) |
| 76 | + ) AS new_cases |
| 77 | +FROM covid |
| 78 | +WHERE name = 'Italy' |
| 79 | + AND WEEKDAY(whn) = 0 |
| 80 | + AND YEAR(whn) = 2020 |
| 81 | +ORDER BY whn; |
| 82 | +``` |
| 83 | + |
| 84 | +**My Notes:** |
| 85 | +Filter with `WEEKDAY(whn) = 0` to get Monday's data. |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +## Problem 5 |
| 90 | +Show the number of new cases in Italy for each week using `JOIN`. |
| 91 | + |
| 92 | +**My Solution:** |
| 93 | + |
| 94 | +```sql |
| 95 | +SELECT |
| 96 | + tw.name, |
| 97 | + DATE_FORMAT(tw.whn,'%Y-%m-%d') AS date, |
| 98 | + (tw.confirmed - lw.confirmed) AS new_cases_each_week |
| 99 | +FROM covid tw |
| 100 | + LEFT JOIN covid lw |
| 101 | + ON DATE_ADD(lw.whn, INTERVAL 1 WEEK) = tw.whn |
| 102 | + AND tw.name = lw.name |
| 103 | +WHERE tw.name = 'Italy' |
| 104 | + AND WEEKDAY(tw.whn) = 0 |
| 105 | +ORDER BY tw.whn; |
| 106 | +``` |
| 107 | + |
| 108 | +**My Notes:** |
| 109 | +Using `JOIN` with `DATE_ADD()` to compare weekly data. |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +## Problem 6 |
| 114 | +Add a column to show the ranking for deaths due to COVID on 2020-04-20. |
| 115 | + |
| 116 | +**My Solution:** |
| 117 | + |
| 118 | +```sql |
| 119 | +SELECT |
| 120 | + name, |
| 121 | + confirmed, |
| 122 | + RANK() OVER (ORDER BY confirmed DESC) rc, |
| 123 | + deaths, |
| 124 | + RANK() OVER (ORDER BY deaths DESC) deaths_rank |
| 125 | +FROM covid |
| 126 | +WHERE whn = '2020-04-20' |
| 127 | +ORDER BY confirmed DESC; |
| 128 | +``` |
| 129 | + |
| 130 | +--- |
| 131 | + |
| 132 | +## Problem 7 |
| 133 | +Show the infection rate ranking (cases per 100,000) for countries with population over 10 million. |
| 134 | + |
| 135 | +**My Solution:** |
| 136 | + |
| 137 | +```sql |
| 138 | +SELECT |
| 139 | + world.name, |
| 140 | + ROUND(100000 * confirmed / population, 2) rate, |
| 141 | + RANK() OVER (ORDER BY rate) rank |
| 142 | +FROM covid |
| 143 | +JOIN world ON covid.name = world.name |
| 144 | +WHERE whn = '2020-04-20' |
| 145 | + AND population > 10000000 |
| 146 | +ORDER BY population DESC; |
| 147 | +``` |
| 148 | + |
| 149 | +**My Notes:** |
| 150 | +Calculate infection rates using population data from `world` table. |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +## Problem 8 |
| 155 | +For each country with at least 1000 new cases in a single day, show the date of the peak number of new cases. |
| 156 | + |
| 157 | +**My Solution:** |
| 158 | +_(No solution given in original SQL. Placeholder for future completion.)_ |
| 159 | + |
| 160 | +--- |
| 161 | + |
0 commit comments