Table: Cinema
+-------------+------+ | Column Name | Type | +-------------+------+ | seat_id | int | | free | bool | +-------------+------+ In SQL, seat_id is an auto-increment primary key column for this table. Each row of this table indicates whether the ith seat is free or not. 1 means free while 0 means occupied.
Find all the consecutive available seats in the cinema.
Return the result table ordered by seat_id
in ascending order.
The test cases are generated so that more than two seats are consecutively available.
The result format is in the following example.
Example 1:
Input: Cinema table: +---------+------+ | seat_id | free | +---------+------+ | 1 | 1 | | 2 | 0 | | 3 | 1 | | 4 | 1 | | 5 | 1 | +---------+------+ Output: +---------+ | seat_id | +---------+ | 3 | | 4 | | 5 | +---------+
# Write your MySQL query statement below
SELECT DISTINCT a.seat_id
FROM
Cinema AS a
JOIN Cinema AS b ON abs(a.seat_id - b.seat_id) = 1 AND a.free AND b.free
ORDER BY 1;
# Write your MySQL query statement below
WITH
T AS (
SELECT
seat_id,
(free + (lag(free) OVER (ORDER BY seat_id))) AS a,
(free + (lead(free) OVER (ORDER BY seat_id))) AS b
FROM Cinema
)
SELECT seat_id
FROM T
WHERE a = 2 OR b = 2;