+---------------+---------+
| Column Name | Type |
+---------------+---------+
| customer_id | int |
| customer_name | varchar |
+---------------+---------+
customer_id is the column with unique values for this table.
Each row of this table contains the name and the id customer.
Write a solution to find the missing customer IDs. The missing IDs are ones that are not in the Customers table but are in the range between 1 and the
maximumcustomer_id present in the table.
Notice that the maximum customer_id will not exceed 100.
Return the result table ordered by ids in ascending order.
Input:
Customers table:+-------------+---------------+| customer_id | customer_name |+-------------+---------------+|1| Alice ||4| Bob ||5| Charlie |+-------------+---------------+Output:
+-----+| ids |+-----+|2||3|+-----+Explanation:
The maximum customer_id present in the table is5, so in the range [1,5], IDs 2 and 3 are missing from the table.
SELECT n AS ids
FROM (
SELECT a.N + b.N *10+1AS n
FROM (SELECT0AS N UNIONALLSELECT1UNIONALLSELECT2UNIONALLSELECT3UNIONALLSELECT4UNIONALLSELECT5UNIONALLSELECT6UNIONALLSELECT7UNIONALLSELECT8UNIONALLSELECT9) a,
(SELECT0AS N UNIONALLSELECT1UNIONALLSELECT2UNIONALLSELECT3UNIONALLSELECT4UNIONALLSELECT5UNIONALLSELECT6UNIONALLSELECT7UNIONALLSELECT8UNIONALLSELECT9) b
) nums
WHERE n <= (SELECTMAX(customer_id) FROM Customers)
AND n NOTIN (SELECT customer_id FROM Customers)
ORDERBY ids;
1
2
3
4
SELECT n AS ids
FROM generate_series(1, (SELECTMAX(customer_id) FROM Customers)) n
WHERE n NOTIN (SELECT customer_id FROM Customers)
ORDERBY ids;