Let me be honest with you!
Any another working solution will be better then presented in question - for one simple reason - it is wrong! it does not return missing numbers at all! it rather shows number after next gap. that's all (hope you will appreciate that I opened your eyes on this)
Now, about better solution(s) - there are tons of options for you to pursue.
Note: below options for BigQuery ONLY!
Option 1
BigQuery Standard SQL - see How to Enable Standard SQL
WITH YourTable AS (
SELECT 1 AS x UNION ALL
SELECT 2 AS x UNION ALL
SELECT 3 AS x UNION ALL
SELECT 6 AS x UNION ALL
SELECT 8 AS x UNION ALL
SELECT 10 AS x UNION ALL
SELECT 11 AS x
),
nums AS (
SELECT num
FROM UNNEST(GENERATE_ARRAY((SELECT MIN(x) FROM YourTable), (SELECT MAX(x) FROM YourTable))) AS num
)
SELECT num FROM nums
LEFT JOIN YourTable ON num = x
WHERE x IS NULL
ORDER BY num
Option 2
BigQuery Legacy SQL you can try below (here you need to set start/min and end/max values inside select expression for nums table
SELECT num FROM (
SELECT num FROM (
SELECT ROW_NUMBER() OVER() AS num, *
FROM (FLATTEN((SELECT SPLIT(RPAD('', 11, '.'),'') AS h FROM (SELECT NULL)), h))
) WHERE num BETWEEN 1 AND 11
) AS nums
LEFT JOIN (
SELECT x FROM
(SELECT 1 AS x),
(SELECT 2 AS x),
(SELECT 3 AS x),
(SELECT 6 AS x),
(SELECT 8 AS x),
(SELECT 10 AS x),
(SELECT 11 AS x)
) AS YourTable
ON num = x
WHERE x IS NULL
Option 3
BigQuery Legacy SQL - if you want not to have dependency on min and max and have needs in setting those values - you can use below solution - it only requires to set high enough max to accommodate your expected growth (I put 1000 for example)
SELECT num FROM (
SELECT num FROM (
SELECT ROW_NUMBER() OVER() AS num, *
FROM (FLATTEN((SELECT SPLIT(RPAD('', 1000, '.'),'') AS h FROM (SELECT NULL)), h))
) WHERE num BETWEEN 1 AND 1000
) AS nums
LEFT JOIN YourTable
ON num = x
WHERE x IS NULL
AND num BETWEEN (SELECT MIN(x) FROM YourTable) AND (SELECT MAX(x) FROM YourTable)
Option 4 (for some reason - my favorite so far)
BigQuery Standard SQL - without explicit joins
WITH YourTable AS (
SELECT 1 AS x UNION ALL
SELECT 2 AS x UNION ALL
SELECT 3 AS x UNION ALL
SELECT 6 AS x UNION ALL
SELECT 8 AS x UNION ALL
SELECT 10 AS x UNION ALL
SELECT 11 AS x
)
SELECT num
FROM (SELECT x, LEAD(x) OVER(ORDER BY x) AS next_x FROM YourTable),
UNNEST(GENERATE_ARRAY(x + 1,next_x - 1)) AS num
WHERE next_x - x > 1
ORDER BY x