{"id":23975,"date":"2023-09-13T02:06:02","date_gmt":"2023-09-13T02:06:02","guid":{"rendered":"https:\/\/machinelearningplus.com\/?p=23975"},"modified":"2023-09-13T17:37:26","modified_gmt":"2023-09-13T17:37:26","slug":"sql-window-function-exercises-and-solutions-set-2","status":"publish","type":"post","link":"https:\/\/machinelearningplus.com\/sql\/sql-window-function-exercises-and-solutions-set-2\/","title":{"rendered":"SQL Window Function Exercises and Solutions &#8211; Set 2"},"content":{"rendered":"<p><strong>SQL window functions exercises is designed to challenge your SQL muscle and help internalize data wrangling using window functions in SQL. The questions are designed like fun puzzles, take your time and try to solve. By the end of this set, you should feel confident in solving the hardest SQL Window function problems.<\/strong><\/p>\n<p>If you haven&#8217;t read the tutorial yet, read <a href=\"https:\/\/machinelearningplus.com\/sql\/sql-window-functions\/\"><strong>SQL Window Functions &#8211; Must Read Guide<\/strong><\/a>.<\/p>\n<p>Try solving hands-on in MySQL using the link provided in each question.<\/p>\n<p><em>Author&#8217;s note: The solutions provided is (mostly) one of the several possible solutions. The goal is not to conform with the provided solution. Try reach the desired output shown.<\/em><\/p>\n<p><a href=\"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2023\/09\/SQL-Window-Function-Exercises-Set-2.jpg\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2023\/09\/SQL-Window-Function-Exercises-Set-2-1024x682.jpg\" alt=\"\" width=\"1024\" height=\"682\" class=\"aligncenter size-large wp-image-23976\" srcset=\"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2023\/09\/SQL-Window-Function-Exercises-Set-2-1024x682.jpg 1024w, https:\/\/machinelearningplus.com\/wp-content\/uploads\/2023\/09\/SQL-Window-Function-Exercises-Set-2-300x200.jpg 300w, https:\/\/machinelearningplus.com\/wp-content\/uploads\/2023\/09\/SQL-Window-Function-Exercises-Set-2-768x512.jpg 768w, https:\/\/machinelearningplus.com\/wp-content\/uploads\/2023\/09\/SQL-Window-Function-Exercises-Set-2.jpg 1238w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<h2>Q1. Calculate the running total of sales.<\/h2>\n<p>Difficulty Level: Intermediate<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>From the <code>sales<\/code> table, calculate the running total of <code>amount<\/code>.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>sale_id<\/th>\n<th>sale_date<\/th>\n<th>amount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2022-01-01<\/td>\n<td>100<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2022-01-05<\/td>\n<td>150<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2022-02-15<\/td>\n<td>200<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>2022-02-20<\/td>\n<td>250<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2022-03-10<\/td>\n<td>300<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>sale_date<\/th>\n<th>amount<\/th>\n<th>running_total<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>2022-01-01<\/td>\n<td>100<\/td>\n<td>100<\/td>\n<\/tr>\n<tr>\n<td>2022-01-05<\/td>\n<td>150<\/td>\n<td>250<\/td>\n<\/tr>\n<tr>\n<td>2022-02-15<\/td>\n<td>200<\/td>\n<td>450<\/td>\n<\/tr>\n<tr>\n<td>2022-02-20<\/td>\n<td>250<\/td>\n<td>700<\/td>\n<\/tr>\n<tr>\n<td>2022-03-10<\/td>\n<td>300<\/td>\n<td>1000<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/xdbTd8VZi6GYaFFzNkDUHz\/1\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/752971b25fcc4f15d53765c53d3ff3a1\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nSELECT sale_date, \r\namount,\r\nsum(amount) over (order by sale_date) as running_total\r\nFROM sales;\r\n<\/pre>\n<p>## Q2. Get the difference between the current sale and the previous sale.<\/p>\n<p>Difficulty Level: Intermediate<\/p>\n<p>__Task:__<\/p>\n<p>From the `sales` table, calculate the difference between the current sale and the previous sale.<\/p>\n<p>__Input:__<\/p>\n<p>| sale_id | sale_date  | amount |<br \/>\n| &#8212;&#8212;- | &#8212;&#8212;&#8212;- | &#8212;&#8212; |<br \/>\n| 1       | 2022-01-01 | 100    |<br \/>\n| 2       | 2022-01-05 | 150    |<br \/>\n| 3       | 2022-02-15 | 200    |<br \/>\n| 4       | 2022-02-20 | 250    |<br \/>\n| 5       | 2022-03-10 | 300    |<\/p>\n<p>__Desired Output:__<\/p>\n<p>| sale_date  | amount | difference |<br \/>\n| &#8212;&#8212;&#8212;- | &#8212;&#8212; | &#8212;&#8212;&#8212;- |<br \/>\n| 2022-01-01 | 100    | 100        |<br \/>\n| 2022-01-05 | 150    | 50         |<br \/>\n| 2022-02-15 | 200    | 50         |<br \/>\n| 2022-02-20 | 250    | 50         |<br \/>\n| 2022-03-10 | 300    | 50         |<\/p>\n<p>Solve Hands-On: [HERE](https:\/\/www.db-fiddle.com\/f\/xdbTd8VZi6GYaFFzNkDUHz\/1), Table Schema and data: [Gist](https:\/\/gist.github.com\/machinelearningplus\/752971b25fcc4f15d53765c53d3ff3a1)<\/p>\n<p><details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/div><\/details><\/p>\n<pre>\r\nSELECT sale_date, \r\n\t\tamount, \r\n        amount - LAG(amount, 1, 0) OVER (ORDER BY sale_date) AS difference \r\nFROM Sales;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q3. Find the highest salary in each department<\/h2>\n<p>Difficulty Level: Intermediate<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>From the <code>employees<\/code> table, calculate the maximum salary in each department.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>dept_id<\/th>\n<th>emp_id<\/th>\n<th>salary<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>10<\/td>\n<td>1<\/td>\n<td>50000<\/td>\n<\/tr>\n<tr>\n<td>10<\/td>\n<td>2<\/td>\n<td>55000<\/td>\n<\/tr>\n<tr>\n<td>20<\/td>\n<td>3<\/td>\n<td>60000<\/td>\n<\/tr>\n<tr>\n<td>20<\/td>\n<td>4<\/td>\n<td>65000<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>dept_id<\/th>\n<th>emp_id<\/th>\n<th>salary<\/th>\n<th>max_salary<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>10<\/td>\n<td>1<\/td>\n<td>50000<\/td>\n<td>55000<\/td>\n<\/tr>\n<tr>\n<td>10<\/td>\n<td>2<\/td>\n<td>55000<\/td>\n<td>55000<\/td>\n<\/tr>\n<tr>\n<td>20<\/td>\n<td>3<\/td>\n<td>60000<\/td>\n<td>65000<\/td>\n<\/tr>\n<tr>\n<td>20<\/td>\n<td>4<\/td>\n<td>65000<\/td>\n<td>65000<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/xxvHRfyHgwhZmqH2cBNApT\/4\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/c0abd01421a13564d9274ce7dff0005a\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nSELECT dept_id, \r\n        emp_id, \r\n        salary,\r\n        MAX(salary) OVER (PARTITION BY dept_id) AS max_salary\r\nFROM Employees;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q4. Find the difference between the salary of an employee and the average salary of their department<\/h2>\n<p>Difficulty Level: Intermediate<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>From the <code>employees<\/code> table, find the difference between the salary of an employee and the average salary of their department.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>dept_id<\/th>\n<th>emp_id<\/th>\n<th>salary<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>10<\/td>\n<td>1<\/td>\n<td>50000<\/td>\n<\/tr>\n<tr>\n<td>10<\/td>\n<td>2<\/td>\n<td>55000<\/td>\n<\/tr>\n<tr>\n<td>20<\/td>\n<td>3<\/td>\n<td>60000<\/td>\n<\/tr>\n<tr>\n<td>20<\/td>\n<td>4<\/td>\n<td>65000<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>emp_id<\/th>\n<th>dept_id<\/th>\n<th>salary<\/th>\n<th>avg_dept_salary<\/th>\n<th>diff_from_avg<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>10<\/td>\n<td>50000<\/td>\n<td>52500<\/td>\n<td>-2500<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>10<\/td>\n<td>55000<\/td>\n<td>52500<\/td>\n<td>2500<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>20<\/td>\n<td>60000<\/td>\n<td>62500<\/td>\n<td>-2500<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>20<\/td>\n<td>65000<\/td>\n<td>62500<\/td>\n<td>2500<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/xxvHRfyHgwhZmqH2cBNApT\/4\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/c0abd01421a13564d9274ce7dff0005a\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nSELECT emp_id, \r\n    dept_id, \r\n    salary,\r\n    AVG(salary) OVER (PARTITION BY dept_id) AS avg_dept_salary,\r\n    salary - AVG(salary) OVER (PARTITION BY dept_id) AS diff_from_avg\r\nFROM Employees;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q5. Calculate the 2-day moving average for the stock prices.<\/h2>\n<p>Difficulty Level: Intermediate<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>From the <code>stockprices<\/code> table, find the 2-day moving average.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>date<\/th>\n<th>price<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>2022-01-01<\/td>\n<td>100.5<\/td>\n<\/tr>\n<tr>\n<td>2022-01-02<\/td>\n<td>101.75<\/td>\n<\/tr>\n<tr>\n<td>2022-01-03<\/td>\n<td>102<\/td>\n<\/tr>\n<tr>\n<td>2022-01-04<\/td>\n<td>103<\/td>\n<\/tr>\n<tr>\n<td>2022-01-05<\/td>\n<td>103.5<\/td>\n<\/tr>\n<tr>\n<td>2022-01-06<\/td>\n<td>107<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>date<\/th>\n<th>price<\/th>\n<th>moving_avg<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>2022-01-01<\/td>\n<td>100.5<\/td>\n<td>100.5<\/td>\n<\/tr>\n<tr>\n<td>2022-01-02<\/td>\n<td>101.75<\/td>\n<td>101.125<\/td>\n<\/tr>\n<tr>\n<td>2022-01-03<\/td>\n<td>102<\/td>\n<td>101.875<\/td>\n<\/tr>\n<tr>\n<td>2022-01-04<\/td>\n<td>103<\/td>\n<td>102.5<\/td>\n<\/tr>\n<tr>\n<td>2022-01-05<\/td>\n<td>103.5<\/td>\n<td>103.25<\/td>\n<\/tr>\n<tr>\n<td>2022-01-06<\/td>\n<td>107<\/td>\n<td>105.25<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/pPL54Wxw7sUBm5hRTefeZx\/1\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/22cc123427fef569739a8517afce43c0\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nSELECT date, price, \r\n       AVG(price) OVER (ORDER BY date ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS moving_avg\r\nFROM StockPrices;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q6. Calculate the difference in days between joining dates<\/h2>\n<p>Difficulty Level: Intermediate<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>Calculate the difference in days between each employee&#8217;s joining date and the previous employee&#8217;s joining date.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>emp_id<\/th>\n<th>join_date<\/th>\n<th>salary<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2022-01-01<\/td>\n<td>1000<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2022-01-10<\/td>\n<td>1100<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2022-01-15<\/td>\n<td>1200<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>emp_id<\/th>\n<th>join_date<\/th>\n<th>day_diff<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2022-01-01<\/td>\n<td>NULL<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2022-01-10<\/td>\n<td>9<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2022-01-15<\/td>\n<td>5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/pPL54Wxw7sUBm5hRTefeZx\/2\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/895c04e49e832bc582aa23ef4ee26644\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nwith sub as (SELECT emp_id, \r\n\t\tjoin_date,\r\n        lag(join_date, 1) over (ORDER BY join_date) as prev_date\r\nFROM employees)\r\nselect *, \r\nDATEDIFF(Date(join_date), date(prev_date)) AS day_diff\r\nfrom sub;\r\n<\/pre>\n<p><\/div><\/details>\n<hr \/>\n<p><em>Questions numbers Q7 onwards uses the same table as below. To avoid repetition, the input is printed only for Q7, please use the same for the full question set.<\/em><\/p>\n<h2>Q7. Find the cumulative sales amount for each product.<\/h2>\n<p>Difficulty Level: Intermediate<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>From <code>Sales<\/code> table, find the cumulative sales amount for each product.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>12<\/td>\n<td>120.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>CumulativeSales<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>100<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>250<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>370<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>50<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>120<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>180<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>20<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>50<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/2\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nSELECT \r\n    ProductID,\r\n    SaleDate,\r\n    sum(SalesAmount) over(partition by ProductID order by SaleDate) AS CumulativeSales\r\nFROM sales;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q8. Compute the average sales for each product over all days.<\/h2>\n<p>Difficulty Level: Intermediate<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>From <code>Sales<\/code> table, find the cumulative sales amount for each product.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>12<\/td>\n<td>120.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>CumulativeSales<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>100<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>250<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>370<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>50<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>120<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>180<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>20<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>50<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/2\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nSELECT \r\n    ProductID,\r\n    SaleDate,\r\n    sum(SalesAmount) over(partition by ProductID order by SaleDate) AS CumulativeSales\r\nFROM sales;\r\n```oyees)\r\nselect *, \r\nDATEDIFF(Date(join_date), date(prev_date)) AS day_diff\r\nfrom sub;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q9. Compute the average sales for each product over all days<\/h2>\n<p>Difficulty Level: Intermediate<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>From <code>Sales<\/code> table, compute the average sales for each product over all days.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>12<\/td>\n<td>120.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ProductID<\/th>\n<th>AverageSales<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>123.33<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>123.33<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>123.33<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>60<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>60<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>60<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>25<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>25<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/2\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nSELECT \r\n    ProductID,\r\n    AVG(SalesAmount) OVER(PARTITION BY ProductID) AS AverageSales\r\nFROM Sales;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q10. Calculate the difference between the current day&#8217;s sales and the previous day&#8217;s sales<\/h2>\n<p>Difficulty Level: Intermediate<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>From <code>Sales<\/code> table, Calculate the difference between the current day&#8217;s sales and the previous day&#8217;s sales for each product.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>12<\/td>\n<td>120.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>DifferenceFromPrevious<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>50<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>-30<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>20<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>-10<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>10<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/2\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nSELECT \r\n    ProductID,\r\n    SaleDate,\r\n    SalesAmount - LAG(SalesAmount) OVER(PARTITION BY ProductID ORDER BY SaleDate) AS DifferenceFromPrevious\r\nFROM Sales;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q11. Get the next sale date for each product sale.<\/h2>\n<p>Difficulty Level: Intermediate<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>From <code>Sales<\/code> table, Get the next sale date for each product sale.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>12<\/td>\n<td>120.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>NextSaleDate<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>2023-01-02<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>2023-01-03<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>2023-01-02<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>2023-01-03<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>2023-01-03<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/2\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nSELECT \r\n    ProductID,\r\n    SaleDate,\r\n    LEAD(SaleDate) OVER(PARTITION BY ProductID ORDER BY SaleDate) AS NextSaleDate\r\nFROM Sales;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q12. Find the total sales of the previous day for each product<\/h2>\n<p>Difficulty Level: Intermediate<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>From <code>Sales<\/code> table, Find the total sales of the previous day for each product.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>12<\/td>\n<td>120.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>PreviousDaySales<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>100<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>150<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>50<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>70<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>20<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/2\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\">\n<pre>\r\nSELECT \r\n    ProductID,\r\n    SaleDate,\r\n    LAG(SalesAmount) OVER(PARTITION BY ProductID ORDER BY SaleDate) AS PreviousDaySales\r\nFROM Sales;\r\n<pre>\r\n<\/div><\/details>\n\n<h2>Q13. Calculate the average sales amount of the previous two days<\/h2>\n\nDifficulty Level: Hard\n\n<strong>Task:<\/strong>\n\nFrom <code>Sales<\/code> table, for each sale, calculate the average sales amount of the previous two days (including the current day) for each product.\n\n<strong>Input:<\/strong>\n\n<table>\n<thead>\n<tr>\n  <th>SaleID<\/th>\n  <th>ProductID<\/th>\n  <th>SaleDate<\/th>\n  <th>QuantitySold<\/th>\n  <th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n  <td>1<\/td>\n  <td>1<\/td>\n  <td>2023-01-01<\/td>\n  <td>10<\/td>\n  <td>100.00<\/td>\n<\/tr>\n<tr>\n  <td>2<\/td>\n  <td>2<\/td>\n  <td>2023-01-01<\/td>\n  <td>5<\/td>\n  <td>50.00<\/td>\n<\/tr>\n<tr>\n  <td>3<\/td>\n  <td>3<\/td>\n  <td>2023-01-01<\/td>\n  <td>15<\/td>\n  <td>20.00<\/td>\n<\/tr>\n<tr>\n  <td>4<\/td>\n  <td>1<\/td>\n  <td>2023-01-02<\/td>\n  <td>15<\/td>\n  <td>150.00<\/td>\n<\/tr>\n<tr>\n  <td>5<\/td>\n  <td>2<\/td>\n  <td>2023-01-02<\/td>\n  <td>7<\/td>\n  <td>70.00<\/td>\n<\/tr>\n<tr>\n  <td>6<\/td>\n  <td>1<\/td>\n  <td>2023-01-03<\/td>\n  <td>12<\/td>\n  <td>120.00<\/td>\n<\/tr>\n<tr>\n  <td>7<\/td>\n  <td>2<\/td>\n  <td>2023-01-03<\/td>\n  <td>6<\/td>\n  <td>60.00<\/td>\n<\/tr>\n<tr>\n  <td>8<\/td>\n  <td>3<\/td>\n  <td>2023-01-03<\/td>\n  <td>16<\/td>\n  <td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n<strong>Desired Output:<\/strong>\n\n<table>\n<thead>\n<tr>\n  <th>ProductID<\/th>\n  <th>SaleDate<\/th>\n  <th>AvgOfLastTwoDays<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n  <td>1<\/td>\n  <td>2023-01-01<\/td>\n  <td>100<\/td>\n<\/tr>\n<tr>\n  <td>1<\/td>\n  <td>2023-01-02<\/td>\n  <td>125<\/td>\n<\/tr>\n<tr>\n  <td>1<\/td>\n  <td>2023-01-03<\/td>\n  <td>135<\/td>\n<\/tr>\n<tr>\n  <td>2<\/td>\n  <td>2023-01-01<\/td>\n  <td>50<\/td>\n<\/tr>\n<tr>\n  <td>2<\/td>\n  <td>2023-01-02<\/td>\n  <td>60<\/td>\n<\/tr>\n<tr>\n  <td>2<\/td>\n  <td>2023-01-03<\/td>\n  <td>65<\/td>\n<\/tr>\n<tr>\n  <td>3<\/td>\n  <td>2023-01-01<\/td>\n  <td>20<\/td>\n<\/tr>\n<tr>\n  <td>3<\/td>\n  <td>2023-01-03<\/td>\n  <td>25<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\nSolve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/2\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a>\n\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\">\r\n<pre>\r\nSELECT \r\n    ProductID,\r\n    SaleDate,\r\n    AVG(SalesAmount) OVER(PARTITION BY ProductID ORDER BY SaleDate ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS AvgOfLastTwoDays\r\nFROM Sales;\r\n<\/pre>\n<p><\/div><\/details><\/p>\n<h2>Q14. Find the date of maximum sale<\/h2>\n<p>Difficulty Level: Hard<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>From <code>Sales<\/code> table, for each sale, get the sale date where maximum sales were made in the previous 2 days for each product.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>12<\/td>\n<td>120.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>MaxSaleDateLastTwoDays<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>2023-01-02<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>2023-01-02<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>2023-01-02<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>2023-01-02<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>2023-01-02<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>2023-01-02<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>2023-01-03<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>2023-01-03<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/2\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nSELECT \r\n    ProductID,\r\n    SaleDate,\r\n    FIRST_VALUE(SaleDate) OVER(PARTITION BY ProductID ORDER BY SalesAmount DESC ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS MaxSaleDateLastTwoDays\r\nFROM Sales;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q15. Calculate the percentage contribution of each product's sale<\/h2>\n<p>Difficulty Level: Hard<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>From <code>Sales<\/code> table, Calculate the percentage contribution of each product's sale to the total sales of that day.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>12<\/td>\n<td>120.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>MaxSaleDateLastTwoDays<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>2023-01-02<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>2023-01-02<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>2023-01-02<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>2023-01-02<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>2023-01-02<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>2023-01-02<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>2023-01-03<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>2023-01-03<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/2\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nSELECT \r\n    ProductID,\r\n    SaleDate,\r\n    SalesAmount,\r\n    round((SalesAmount \/ SUM(SalesAmount) OVER(PARTITION BY SaleDate)) * 100, 2) AS PercentageContribution\r\nFROM sales;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q16. Calculate the third highest sales amount<\/h2>\n<p>Difficulty Level: Hard<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>For each product, get the third highest sales amount and its corresponding sale date.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>12<\/td>\n<td>120.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>50.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/2\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nSELECT \r\n    ProductID,\r\n    SaleDate,\r\n    SalesAmount\r\nFROM (\r\n    SELECT \r\n        ProductID,\r\n        SaleDate,\r\n        SalesAmount,\r\n        DENSE_RANK() OVER(PARTITION BY ProductID ORDER BY SalesAmount DESC) AS rnk\r\n    FROM sales\r\n) AS T\r\nWHERE rnk = 3;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q17. Calculate the moving variance of the last 3 sales amounts for each product.<\/h2>\n<p>Difficulty Level: Hard<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>From <code>sales<\/code> table, calculate the moving variance of the last 3 sales amounts for each product.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>12<\/td>\n<td>120.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>MovingVariance<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>625<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>422.22222222222223<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>100<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>66.66666666666667<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>25<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/3\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nSELECT \r\n    ProductID,\r\n    SaleDate,\r\n    VARIANCE(SalesAmount) OVER(PARTITION BY ProductID ORDER BY SaleDate ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS MovingVariance\r\nFROM sales;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q18. Find the product that had the least sales amount difference compared to the previous day.<\/h2>\n<p>Difficulty Level: Hard<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>From <code>sales<\/code> table, For each sale date, find the product that had the least sales amount difference compared to the previous day.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>160.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleDate<\/th>\n<th>ProductID<\/th>\n<th>Diff<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>2023-01-01<\/td>\n<td>1<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>2023-01-02<\/td>\n<td>2<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>2023-01-03<\/td>\n<td>2<\/td>\n<td>10.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/4\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nWITH Differences AS (\r\n    SELECT \r\n        ProductID,\r\n        SaleDate,\r\n        ABS(SalesAmount - LAG(SalesAmount) OVER(PARTITION BY ProductID ORDER BY SaleDate)) AS Diff\r\n    FROM sales\r\n)\r\n\r\nSELECT \r\n    SaleDate,\r\n    ProductID,\r\n    Diff\r\nFROM (\r\n    SELECT \r\n        SaleDate,\r\n        ProductID,\r\n        Diff,\r\n        ROW_NUMBER() OVER(PARTITION BY SaleDate ORDER BY Diff ASC) AS rnk\r\n    FROM Differences\r\n) AS T\r\nWHERE rnk = 1;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q19. Determine the average change in sales amounts<\/h2>\n<p>Difficulty Level: Hard<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>From <code>sales<\/code> table, for each product, determine the average change in sales amounts corresponding to the previous sale day.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>160.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>PRODUCTID<\/th>\n<th>AVGCHANGE<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>30.000000<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>5.000000<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>10.000000<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/4\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nwith tbl as (SELECT \r\n    PRODUCTID,\r\n    SALEDATE,\r\n    (SalesAmount - LAG(SalesAmount, 1) OVER(PARTITION BY PRODUCTID ORDER BY SALEDATE)) AS diff,\r\n    COUNT(*) over (PARTITION BY PRODUCTID) - 1 as num_changes\r\nFROM sales),\r\n\r\ntbl2 as (\r\nSELECT PRODUCTID,\r\n        max(SALEDATE) as SALEDATE,\r\n        sum(diff) as SUMDIFF,\r\n        max(num_changes) as TOTALCHANGES\r\n        from tbl\r\ngroup by ProductID)\r\nSELECT PRODUCTID,\r\n       SUMDIFF\/TOTALCHANGES as AVGCHANGE\r\nFROM tbl2;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q20. Find the median sales amount for each product.<\/h2>\n<p>Difficulty Level: Hard<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>From <code>sales<\/code> table, for each product, determine the average change in sales amounts corresponding to the previous sale day.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>160.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ProductID<\/th>\n<th>Median<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>150.000000<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>60.000000<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>25.000000<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/4\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nWITH RankedSales AS (\r\n    SELECT \r\n        ProductID,\r\n        SalesAmount,\r\n        ROW_NUMBER() OVER(PARTITION BY ProductID ORDER BY SalesAmount) AS rn,\r\n        COUNT(*) OVER(PARTITION BY ProductID) AS cnt\r\n    FROM sales\r\n)\r\n\r\nSELECT \r\n    ProductID,\r\n    AVG(SalesAmount) AS Median\r\nFROM RankedSales\r\nWHERE rn IN (FLOOR((cnt+1)\/2), CEIL((cnt+1)\/2))\r\nGROUP BY ProductID;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q20. Find the difference from the average for each entry<\/h2>\n<p>Difficulty Level: Hard<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>For each sale, calculate the difference from the monthly average of the product's sales amount.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>160.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>SalesAmount<\/th>\n<th>DiffFromMonthlyAvg<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>100.00<\/td>\n<td>-36.666667<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>150.00<\/td>\n<td>13.333333<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>160.00<\/td>\n<td>23.333333<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>50.00<\/td>\n<td>-10.000000<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>70.00<\/td>\n<td>10.000000<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>60.00<\/td>\n<td>0.000000<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>20.00<\/td>\n<td>-5.000000<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>30.00<\/td>\n<td>5.000000<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/4\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nSELECT \r\n    ProductID,\r\n    SaleDate,\r\n    SalesAmount,\r\n    SalesAmount - AVG(SalesAmount) OVER(PARTITION BY ProductID, EXTRACT(MONTH FROM SaleDate)) AS DiffFromMonthlyAvg\r\nFROM sales;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q21. Rank products by the variability<\/h2>\n<p>Difficulty Level: Hard<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>For each sale, calculate the difference from the monthly average of the product's sales amount.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>160.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ProductID<\/th>\n<th>STDV<\/th>\n<th>VariabilityRank<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>32.1455<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>10<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>7.0711<\/td>\n<td>3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/4\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nSELECT \r\n    ProductID,\r\n    ROUND(STDDEV_SAMP(SalesAmount),4) as STDV,\r\n    RANK() OVER(ORDER BY STDDEV_SAMP(SalesAmount) DESC) AS VariabilityRank\r\nFROM sales\r\nGROUP BY ProductID;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q22. Calculate a 3-day centered moving average<\/h2>\n<p>Difficulty Level: Hard<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>Calculate a 3-day centered moving average for each product's sales amount.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>160.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>SalesAmount<\/th>\n<th>CenteredMovingAverage<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>100.00<\/td>\n<td>125.000000<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>150.00<\/td>\n<td>136.666667<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>160.00<\/td>\n<td>155.000000<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>50.00<\/td>\n<td>60.000000<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>70.00<\/td>\n<td>60.000000<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>60.00<\/td>\n<td>65.000000<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>20.00<\/td>\n<td>25.000000<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>30.00<\/td>\n<td>25.000000<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/4\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nSELECT \r\n    ProductID,\r\n    SaleDate,\r\n    SalesAmount,\r\n    AVG(SalesAmount) OVER(PARTITION BY ProductID ORDER BY SaleDate ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS CenteredMovingAverage\r\nFROM sales;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q23. Determine if the sales amount of each product was above or below average<\/h2>\n<p>Difficulty Level: Hard<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>For each sale date, determine if the sales amount of each product was above or below its previous 3 days average.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>160.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>SalesAmount<\/th>\n<th>ComparisonToLast3Days<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>100.00<\/td>\n<td>Below<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>50.00<\/td>\n<td>Below<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>20.00<\/td>\n<td>Below<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>150.00<\/td>\n<td>Above<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>70.00<\/td>\n<td>Above<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>160.00<\/td>\n<td>Above<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>60.00<\/td>\n<td>Below<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>30.00<\/td>\n<td>Above<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/4\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nWITH threeDayAverage AS (\r\n    SELECT \r\n        ProductID,\r\n        SaleDate,\r\n        AVG(SalesAmount) OVER(PARTITION BY ProductID ORDER BY SaleDate ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING) AS avg_last_3_days\r\n    FROM sales\r\n)\r\n\r\nSELECT \r\n    S.ProductID,\r\n    S.SaleDate,\r\n    S.SalesAmount,\r\n    CASE \r\n        WHEN S.SalesAmount > A.avg_last_3_days THEN 'Above'\r\n        ELSE 'Below'\r\n    END AS ComparisonToLast3Days\r\nFROM sales S\r\nJOIN threeDayAverage A ON S.ProductID = A.ProductID AND S.SaleDate = A.SaleDate;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Q24. Calculate the cumulative sales growth rate<\/h2>\n<p>Difficulty Level: Hard<\/p>\n<p><strong>Task:<\/strong><\/p>\n<p>Calculate the cumulative sales growth rate for each product. (Sales growth rate from one day to the next is (TodaysSale\u2212YesterdaysSale)\/(YesterdaysSale).<\/p>\n<p><strong>Input:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>SaleID<\/th>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>QuantitySold<\/th>\n<th>SalesAmount<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>100.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>5<\/td>\n<td>50.00<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>15<\/td>\n<td>20.00<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>15<\/td>\n<td>150.00<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>7<\/td>\n<td>70.00<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>160.00<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>6<\/td>\n<td>60.00<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>16<\/td>\n<td>30.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Desired Output:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ProductID<\/th>\n<th>SaleDate<\/th>\n<th>SalesAmount<\/th>\n<th>cumulative_growth_rate<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>100.00<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-02<\/td>\n<td>150.00<\/td>\n<td>0.500000<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2023-01-03<\/td>\n<td>160.00<\/td>\n<td>0.566667<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-01<\/td>\n<td>50.00<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>70.00<\/td>\n<td>0.400000<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-03<\/td>\n<td>60.00<\/td>\n<td>0.257143<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-01<\/td>\n<td>20.00<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2023-01-03<\/td>\n<td>30.00<\/td>\n<td>0.500000<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Solve Hands-On: <a href=\"https:\/\/www.db-fiddle.com\/f\/fj8znDyT4cnEsDwTheJ53S\/4\">HERE<\/a>, Table Schema and data: <a href=\"https:\/\/gist.github.com\/machinelearningplus\/02f0dd5526d3d33fa00d89e878a2a9fb\">Gist<\/a><\/p>\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\n<pre>\r\nWITH GrowthRates AS (\r\n    SELECT \r\n        ProductID,\r\n        SaleDate,\r\n        SalesAmount,\r\n        (SalesAmount - LAG(SalesAmount) OVER(PARTITION BY ProductID ORDER BY SaleDate)) \/ LAG(SalesAmount) OVER(PARTITION BY ProductID ORDER BY SaleDate) AS growth_rate\r\n    FROM sales\r\n)\r\n\r\nSELECT \r\n    ProductID,\r\n    SaleDate,\r\n    SalesAmount,\r\n    SUM(growth_rate) OVER(PARTITION BY ProductID ORDER BY SaleDate) AS cumulative_growth_rate\r\nFROM GrowthRates;\r\n<\/pre>\n<p><\/div><\/details>\n<h2>Next Steps<\/h2>\n<ol>\n<li>Try the <a href=\"https:\/\/machinelearningplus.com\/sql\/sql-window-functions-exercises\/\">SQL Window Functions Set 1<\/a> if you haven't already done.<\/li>\n<li>To become real solid at SQL, the SQL courses (basic, intermediate and advanced) are included as part of the <a href=\"https:\/\/edu.machinelearningplus.com\/s\/pages\/ds-career-path\">Machine Learning Plus - Complete Data Science University Access<\/a>. <a href=\"https:\/\/machinelearningplus.com\/pricing\">Subscribe<\/a>.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>SQL window functions exercises is designed to challenge your SQL muscle and help internalize data wrangling using window functions in SQL. The questions are designed like fun puzzles, take your time and try to solve. By the end of this set, you should feel confident in solving the hardest SQL Window function problems. If you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":23999,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[2091],"tags":[2106],"class_list":["post-23975","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql","tag-window-functions"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Window Function Exercises and Solutions - Set 2 | machinelearningplus<\/title>\n<meta name=\"description\" content=\"SQL window function exercises is designed to challenge your SQL muscle and help internalize data wrangling using window functions in SQL.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/localhost:8080\/sql\/sql-window-function-exercises-and-solutions-set-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Window Function Exercises and Solutions - Set 2 | machinelearningplus\" \/>\n<meta property=\"og:description\" content=\"SQL window function exercises is designed to challenge your SQL muscle and help internalize data wrangling using window functions in SQL.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/localhost:8080\/sql\/sql-window-function-exercises-and-solutions-set-2\/\" \/>\n<meta property=\"og:site_name\" content=\"machinelearningplus\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/rtipaday\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-13T02:06:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-13T17:37:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/localhost:8080\/wp-content\/uploads\/2023\/09\/SQL-Window-Function-Exercises-and-Solutions-2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1080\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Selva Prabhakaran\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/R_Programming\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Selva Prabhakaran\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"22 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/sql\\\/sql-window-function-exercises-and-solutions-set-2\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/sql\\\/sql-window-function-exercises-and-solutions-set-2\\\/\"},\"author\":{\"name\":\"Selva Prabhakaran\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/person\\\/510885c0515804366fa644c38258391e\"},\"headline\":\"SQL Window Function Exercises and Solutions &#8211; Set 2\",\"datePublished\":\"2023-09-13T02:06:02+00:00\",\"dateModified\":\"2023-09-13T17:37:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/sql\\\/sql-window-function-exercises-and-solutions-set-2\\\/\"},\"wordCount\":2143,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/sql\\\/sql-window-function-exercises-and-solutions-set-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/SQL-Window-Function-Exercises-and-Solutions-2.png\",\"keywords\":[\"Window Functions\"],\"articleSection\":[\"SQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/localhost:8080\\\/sql\\\/sql-window-function-exercises-and-solutions-set-2\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/sql\\\/sql-window-function-exercises-and-solutions-set-2\\\/\",\"url\":\"https:\\\/\\\/localhost:8080\\\/sql\\\/sql-window-function-exercises-and-solutions-set-2\\\/\",\"name\":\"SQL Window Function Exercises and Solutions - Set 2 | machinelearningplus\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/sql\\\/sql-window-function-exercises-and-solutions-set-2\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/sql\\\/sql-window-function-exercises-and-solutions-set-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/SQL-Window-Function-Exercises-and-Solutions-2.png\",\"datePublished\":\"2023-09-13T02:06:02+00:00\",\"dateModified\":\"2023-09-13T17:37:26+00:00\",\"description\":\"SQL window function exercises is designed to challenge your SQL muscle and help internalize data wrangling using window functions in SQL.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/localhost:8080\\\/sql\\\/sql-window-function-exercises-and-solutions-set-2\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/sql\\\/sql-window-function-exercises-and-solutions-set-2\\\/#primaryimage\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/SQL-Window-Function-Exercises-and-Solutions-2.png\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/SQL-Window-Function-Exercises-and-Solutions-2.png\",\"width\":1080,\"height\":1080,\"caption\":\"SQL Window Function Exercises and Solutions 2\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#website\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/\",\"name\":\"machinelearningplus\",\"description\":\"Learn Data Science (AI \\\/ ML) Online\",\"publisher\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/machinelearningplus.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\",\"name\":\"machinelearningplus\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/MachineLearningplus-logo.svg\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/MachineLearningplus-logo.svg\",\"width\":348,\"height\":36,\"caption\":\"machinelearningplus\"},\"image\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/person\\\/510885c0515804366fa644c38258391e\",\"name\":\"Selva Prabhakaran\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/a994280177da541405c016f593e86ea7.jpg?ver=1783622267\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/a994280177da541405c016f593e86ea7.jpg?ver=1783622267\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/a994280177da541405c016f593e86ea7.jpg?ver=1783622267\",\"caption\":\"Selva Prabhakaran\"},\"description\":\"Selva is an experienced Data Scientist and leader, specializing in executing AI projects for large companies. Selva started machinelearningplus to make Data Science \\\/ ML \\\/ AI accessible to everyone. The website enjoys 4 Million+ readership. His courses, lessons, and videos are loved by hundreds of thousands of students and practitioners.\",\"sameAs\":[\"https:\\\/\\\/localhost:8080\\\/\",\"https:\\\/\\\/www.facebook.com\\\/rtipaday\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/R_Programming\"],\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/author\\\/selva86\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Window Function Exercises and Solutions - Set 2 | machinelearningplus","description":"SQL window function exercises is designed to challenge your SQL muscle and help internalize data wrangling using window functions in SQL.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/localhost:8080\/sql\/sql-window-function-exercises-and-solutions-set-2\/","og_locale":"en_US","og_type":"article","og_title":"SQL Window Function Exercises and Solutions - Set 2 | machinelearningplus","og_description":"SQL window function exercises is designed to challenge your SQL muscle and help internalize data wrangling using window functions in SQL.","og_url":"https:\/\/localhost:8080\/sql\/sql-window-function-exercises-and-solutions-set-2\/","og_site_name":"machinelearningplus","article_author":"https:\/\/www.facebook.com\/rtipaday\/","article_published_time":"2023-09-13T02:06:02+00:00","article_modified_time":"2023-09-13T17:37:26+00:00","og_image":[{"width":1080,"height":1080,"url":"https:\/\/localhost:8080\/wp-content\/uploads\/2023\/09\/SQL-Window-Function-Exercises-and-Solutions-2.png","type":"image\/png"}],"author":"Selva Prabhakaran","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/R_Programming","twitter_misc":{"Written by":"Selva Prabhakaran","Est. reading time":"22 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/localhost:8080\/sql\/sql-window-function-exercises-and-solutions-set-2\/#article","isPartOf":{"@id":"https:\/\/localhost:8080\/sql\/sql-window-function-exercises-and-solutions-set-2\/"},"author":{"name":"Selva Prabhakaran","@id":"https:\/\/machinelearningplus.com\/#\/schema\/person\/510885c0515804366fa644c38258391e"},"headline":"SQL Window Function Exercises and Solutions &#8211; Set 2","datePublished":"2023-09-13T02:06:02+00:00","dateModified":"2023-09-13T17:37:26+00:00","mainEntityOfPage":{"@id":"https:\/\/localhost:8080\/sql\/sql-window-function-exercises-and-solutions-set-2\/"},"wordCount":2143,"commentCount":0,"publisher":{"@id":"https:\/\/machinelearningplus.com\/#organization"},"image":{"@id":"https:\/\/localhost:8080\/sql\/sql-window-function-exercises-and-solutions-set-2\/#primaryimage"},"thumbnailUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2023\/09\/SQL-Window-Function-Exercises-and-Solutions-2.png","keywords":["Window Functions"],"articleSection":["SQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/localhost:8080\/sql\/sql-window-function-exercises-and-solutions-set-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/localhost:8080\/sql\/sql-window-function-exercises-and-solutions-set-2\/","url":"https:\/\/localhost:8080\/sql\/sql-window-function-exercises-and-solutions-set-2\/","name":"SQL Window Function Exercises and Solutions - Set 2 | machinelearningplus","isPartOf":{"@id":"https:\/\/machinelearningplus.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/localhost:8080\/sql\/sql-window-function-exercises-and-solutions-set-2\/#primaryimage"},"image":{"@id":"https:\/\/localhost:8080\/sql\/sql-window-function-exercises-and-solutions-set-2\/#primaryimage"},"thumbnailUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2023\/09\/SQL-Window-Function-Exercises-and-Solutions-2.png","datePublished":"2023-09-13T02:06:02+00:00","dateModified":"2023-09-13T17:37:26+00:00","description":"SQL window function exercises is designed to challenge your SQL muscle and help internalize data wrangling using window functions in SQL.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/localhost:8080\/sql\/sql-window-function-exercises-and-solutions-set-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/localhost:8080\/sql\/sql-window-function-exercises-and-solutions-set-2\/#primaryimage","url":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2023\/09\/SQL-Window-Function-Exercises-and-Solutions-2.png","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2023\/09\/SQL-Window-Function-Exercises-and-Solutions-2.png","width":1080,"height":1080,"caption":"SQL Window Function Exercises and Solutions 2"},{"@type":"WebSite","@id":"https:\/\/machinelearningplus.com\/#website","url":"https:\/\/machinelearningplus.com\/","name":"machinelearningplus","description":"Learn Data Science (AI \/ ML) Online","publisher":{"@id":"https:\/\/machinelearningplus.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/machinelearningplus.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/machinelearningplus.com\/#organization","name":"machinelearningplus","url":"https:\/\/machinelearningplus.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/machinelearningplus.com\/#\/schema\/logo\/image\/","url":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/05\/MachineLearningplus-logo.svg","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/05\/MachineLearningplus-logo.svg","width":348,"height":36,"caption":"machinelearningplus"},"image":{"@id":"https:\/\/machinelearningplus.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/machinelearningplus.com\/#\/schema\/person\/510885c0515804366fa644c38258391e","name":"Selva Prabhakaran","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/a994280177da541405c016f593e86ea7.jpg?ver=1783622267","url":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/a994280177da541405c016f593e86ea7.jpg?ver=1783622267","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/a994280177da541405c016f593e86ea7.jpg?ver=1783622267","caption":"Selva Prabhakaran"},"description":"Selva is an experienced Data Scientist and leader, specializing in executing AI projects for large companies. Selva started machinelearningplus to make Data Science \/ ML \/ AI accessible to everyone. The website enjoys 4 Million+ readership. His courses, lessons, and videos are loved by hundreds of thousands of students and practitioners.","sameAs":["https:\/\/localhost:8080\/","https:\/\/www.facebook.com\/rtipaday\/","https:\/\/x.com\/https:\/\/twitter.com\/R_Programming"],"url":"https:\/\/machinelearningplus.com\/author\/selva86\/"}]}},"_links":{"self":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts\/23975","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/comments?post=23975"}],"version-history":[{"count":0,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts\/23975\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/media\/23999"}],"wp:attachment":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/media?parent=23975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/categories?post=23975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/tags?post=23975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}