AnalystPath

Garages With No Free Spaces

SQLEasyJunior level~15 min

Problem

Table: `garages`

```text
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| garage_id | int |
| garage_name | varchar |
| district | varchar |
| zone_code | varchar |
| opened_year | int |
| total_spaces | int |
+----------------+---------+
garage_id is the primary key for this table.
total_spaces is how many cars the garage can hold.
```

Table: `parking_sessions`

```text
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| session_id | int |
| garage_id | int |
| plate | varchar |
| entry_time | date |
| exit_time | date |
+-------------+---------+
session_id is the primary key for this table.
exit_time is NULL while the car is still parked.
```

A car is currently parked when its `exit_time` is NULL. Find every garage that is completely full right now — that is, the number of currently parked cars equals `total_spaces`.

Return `garage_id`, `garage_name`, `district`, `zone_code`, `opened_year`, and `parked_now`, ordered by `parked_now` descending, then `garage_name` ascending.

Tables

Example rows — the live problem includes the full dataset.

garages
garage_idgarage_namedistrictzone_codeopened_yeartotal_spaces
1Harbor DeckWaterfrontWF20103
2Market TowerOld TownOT20053
3Station LotMidtownMT20181
parking_sessions
session_idgarage_idplateentry_timeexit_time
11AAA1112024-01-15
21BBB2222024-01-20
32CCC3332024-01-102024-01-25

Expected output

Your answer should return 2 rows with the columns garage_id, garage_name, district, zone_code, opened_year, parked_now.

Starter code (SQL)

SELECT *
FROM garages;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions