PostgreSQL · Lesson 108 of 120
Project: Core Queries
Seats left and top guests.
The box office asks two questions every day. How many seats are left for upcoming shows? And who are our best guests?
- Seats left?
- Best guests?
- Every day
Seats left
We join events, venues and bookings. Capacity minus the seats booked gives seats left. A left join keeps events with no bookings yet. Run it. The next five shows from March, with seats left.
query.sqlsql
SELECT e.title, e.starts_at::date AS day, v.name AS venue,
v.capacity - coalesce(sum(b.seats), 0) AS seats_left
FROM events e
JOIN venues v USING (venue_id)
LEFT JOIN bookings b USING (event_id)
WHERE e.starts_at >= '2026-03-01'
GROUP BY e.event_id, v.venue_id
ORDER BY e.starts_at
LIMIT 5;Output
title | day | venue | seats_left ------------------+------------+-----------------+------------ Indie Film Night | 2026-03-01 | Lighthouse Loft | 25 Jazz Night | 2026-03-03 | Harbor Hall | 255 Comedy Hour | 2026-03-05 | Pier Theatre | 82 Folk Session | 2026-03-07 | Lighthouse Loft | 20 Poetry Slam | 2026-03-09 | Harbor Hall | 252 (5 rows)
Also uses _setup.sql
CREATE TABLE venues (
venue_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL UNIQUE,
city text NOT NULL,
capacity integer NOT NULL CHECK (capacity > 0));
CREATE TABLE events (
event_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
venue_id integer NOT NULL REFERENCES venues,
title text NOT NULL,
starts_at timestamptz NOT NULL,
price numeric(6,2) NOT NULL CHECK (price >= 0));
CREATE TABLE bookings (
booking_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
event_id integer NOT NULL REFERENCES events,
email text NOT NULL,
seats integer NOT NULL CHECK (seats BETWEEN 1 AND 10),
booked_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (event_id, email));
INSERT INTO venues (name, city, capacity) VALUES
('Harbor Hall', 'Halifax', 300), ('Pier Theatre', 'Halifax', 120),
('Lighthouse Loft', 'Lunenburg', 60);
INSERT INTO events (venue_id, title, starts_at, price)
SELECT 1 + g % 3,
(ARRAY['Jazz Night', 'Comedy Hour', 'Folk Session', 'Poetry Slam',
'Sea Shanty Evening', 'Indie Film Night'])[1 + g % 6],
timestamptz '2026-01-02 19:30' + g * interval '2 days',
(ARRAY[15, 20, 25, 30])[1 + g % 4]
FROM generate_series(1, 60) g;
SELECT setseed(0.7);
INSERT INTO bookings (event_id, email, seats, booked_at)
SELECT e, 'guest' || c || '@mail.com', 1 + (random() * 2)::int,
timestamptz '2026-01-01' + random() * interval '100 days'
FROM generate_series(1, 60) e, generate_series(1, 40) c
WHERE random() < 0.5;Also uses _empty
1Best guests
Group bookings by email, count the shows and sum the spend. Run it. The top three guests by spending.
query.sqlsql
SELECT b.email, count(*) AS shows, sum(b.seats * e.price) AS spent
FROM bookings b JOIN events e USING (event_id)
GROUP BY b.email
ORDER BY spent DESC
LIMIT 3;Output
email | shows | spent ------------------+-------+--------- guest37@mail.com | 36 | 1690.00 guest5@mail.com | 39 | 1675.00 guest12@mail.com | 33 | 1670.00 (3 rows)
Also uses _setup.sql
CREATE TABLE venues (
venue_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL UNIQUE,
city text NOT NULL,
capacity integer NOT NULL CHECK (capacity > 0));
CREATE TABLE events (
event_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
venue_id integer NOT NULL REFERENCES venues,
title text NOT NULL,
starts_at timestamptz NOT NULL,
price numeric(6,2) NOT NULL CHECK (price >= 0));
CREATE TABLE bookings (
booking_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
event_id integer NOT NULL REFERENCES events,
email text NOT NULL,
seats integer NOT NULL CHECK (seats BETWEEN 1 AND 10),
booked_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (event_id, email));
INSERT INTO venues (name, city, capacity) VALUES
('Harbor Hall', 'Halifax', 300), ('Pier Theatre', 'Halifax', 120),
('Lighthouse Loft', 'Lunenburg', 60);
INSERT INTO events (venue_id, title, starts_at, price)
SELECT 1 + g % 3,
(ARRAY['Jazz Night', 'Comedy Hour', 'Folk Session', 'Poetry Slam',
'Sea Shanty Evening', 'Indie Film Night'])[1 + g % 6],
timestamptz '2026-01-02 19:30' + g * interval '2 days',
(ARRAY[15, 20, 25, 30])[1 + g % 4]
FROM generate_series(1, 60) g;
SELECT setseed(0.7);
INSERT INTO bookings (event_id, email, seats, booked_at)
SELECT e, 'guest' || c || '@mail.com', 1 + (random() * 2)::int,
timestamptz '2026-01-01' + random() * interval '100 days'
FROM generate_series(1, 60) e, generate_series(1, 40) c
WHERE random() < 0.5;Also uses _empty
1Summary
Joins and grouping answer the box office's daily questions. Next, monthly reports.
Key takeaways- Seats left
- LEFT JOIN
- Top guests
Next up: Project: Reports — money by venue and month