##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1767.html

# 1767. Find the Subtasks That Did Not Execute

Hard

## Description

Table: Tasks

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
+----------------+---------+
task_id is the primary key for this table.
It is guaranteed that 2 <= subtasks_count <= 20.


Table: Executed

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
+---------------+---------+


Write an SQL query to report the IDs of the missing subtasks for each task_id.

Return the result table in any order.

The query result format is in the following example:

Tasks table:
+---------+----------------+
+---------+----------------+
| 1       | 3              |
| 2       | 2              |
| 3       | 4              |
+---------+----------------+

Executed table:
+---------+------------+
+---------+------------+
| 1       | 2          |
| 3       | 1          |
| 3       | 2          |
| 3       | 3          |
| 3       | 4          |
+---------+------------+

Result table:
+---------+------------+
+---------+------------+
| 1       | 1          |
| 1       | 3          |
| 2       | 1          |
| 2       | 2          |
+---------+------------+
Task 1 was divided into 3 subtasks (1, 2, 3). Only subtask 2 was executed successfully, so we include (1, 1) and (1, 3) in the answer.
Task 2 was divided into 2 subtasks (1, 2). No subtask was executed successfully, so we include (2, 1) and (2, 2) in the answer.
Task 3 was divided into 4 subtasks (1, 2, 3, 4). All of the subtasks were executed successfully.


## Solution

Generate table t recursively to make sure that each task has a corresponding subtask. Then generate table cte by joining table t and table Tasks, with a column row_number added so that each task has a corresponding task_id. Finally, join cte table and Executed table amd select the entries with subtask_id is null.

# Write your MySQL query statement below
with recursive t(n) as (
select 1 n
union all
select n + 1 from t where n < (select max(subtasks_count) from Tasks)
),
cte as (