← SQL workplace practice

Intervals and scheduling · Original fictional data · Free files and full answers

Keep the maximum number of nonoverlapping bookings

Per resource choose an unweighted maximum-cardinality compatible booking set by earliest finishing time, ties by id. After each choice the next booking must start at or after the prior end. Return resource,kept_id sorted resource,kept_id.

Use this fixed business case to build a checked CSV and a replayable SQL handoff. No account required. Scenario datasets are shared across related exercises; each exercise has its own decision and output contract.

Your output contract

  1. Per resource choose an unweighted maximum-cardinality compatible booking set by earliest finishing time, ties by id. After each choice the next booking must start at or after the prior end. Return resource,kept_id sorted resource,kept_id.
  2. Return exactly the declared columns and order. Money uses integer cents; all time values are elapsed UTC minutes unless a field is explicitly a day index. NULL is unknown and differs from numeric zero.
  3. These are original fictional records and exercise policies. All source tables in this case are included in the downloadable pack.

Return columns exactly as resource, kept_id. Sort order follows the task. Output has 9 rows; an empty CSV field means NULL.

Source data

slots

12 source rows · Download slots CSV

id INTEGERresource TEXTstart INTEGERend INTEGERpriority INTEGER
1R10201
2R110303
3R130402
4R150601
5R112184
6R25151
7R220352
8R230451
9R245503
10R30101
11R315251
12R340602

requests

5 source rows · Download requests CSV

id INTEGERresource TEXTminute INTEGERduration INTEGER
1R158
2R1417
3R2165
4R32610
5R1458
Field dictionary and schema
slots:
  id INTEGER — Stable record or business key; retain its declared type.
  resource TEXT — Literal text label or key; comparisons use SQLite binary text semantics.
  start INTEGER — Exact integer measurement; use the task rule for units and ordering.
  end INTEGER — Exact integer measurement; use the task rule for units and ordering.
  priority INTEGER — Exact integer measurement; use the task rule for units and ordering.

requests:
  id INTEGER — Stable record or business key; retain its declared type.
  resource TEXT — Literal text label or key; comparisons use SQLite binary text semantics.
  minute INTEGER — Exact integer measurement; use the task rule for units and ordering.
  duration INTEGER — Exact integer measurement; use the task rule for units and ordering.

Download complete SQLite setup

A method you can reuse

  1. Per resource choose an unweighted maximum-cardinality compatible booking set by earliest finishing time, ties by id. After each choice the next booking must start at or after the prior end. Return resource,kept_id sorted resource,kept_id.
  2. Inspect the source grain, keys and ties before writing the query. Use the worked SQL and complete expected CSV to review any mismatch.

Watch for this wrong turn

Choosing the earliest starting or highest-priority booking solves a different optimization problem and may retain fewer meetings.

Run and check in SQLite

Your SQL runs locally in the existing SQLite worker. The checker compares every column, row, type, value and row order with this fixed output. It accepts equivalent SELECT or WITH queries. Browser storage keeps your SQL on this device when available.

Complete worked SQL and expected output
WITH RECURSIVE pick(resource,id,end) AS (SELECT s.resource,s.id,s.end FROM slots s WHERE s.id=(SELECT z.id FROM slots z WHERE z.resource=s.resource ORDER BY z.end,z.id LIMIT 1) UNION ALL SELECT p.resource,s.id,s.end FROM pick p JOIN slots s ON s.id=(SELECT z.id FROM slots z WHERE z.resource=p.resource AND z.start>=p.end ORDER BY z.end,z.id LIMIT 1)) SELECT resource,id kept_id FROM pick ORDER BY resource,kept_id;
"resource","kept_id"
"R1","3"
"R1","4"
"R1","5"
"R2","6"
"R2","7"
"R2","9"
"R3","10"
"R3","11"
"R3","12"
Reference SQL · Complete expected CSV · Empty answer CSV

Choose another SQL task · Original data exception cases · Larger workplace projects