← SQL workplace practice

Versioned facts and temporal truth · Original fictional data · Free files and full answers

Find overlapping validity slices excluding exact interval corrections

At cutoff15, return version_a,version_b,overlap_days for nondeleted same-entity versions a<b with positive overlap, excluding pairs with identical validity endpoints because those are corrections. Sort both IDs.

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. At cutoff15, return version_a,version_b,overlap_days for nondeleted same-entity versions a<b with positive overlap, excluding pairs with identical validity endpoints because those are corrections. Sort both IDs.
  2. Return exactly the declared columns and order. Money uses integer cents; time and date units follow the task and field dictionary. NULL is unknown and differs from numeric zero.
  3. These are original fictional records and exercise policies. Scenario data may be shared with related tasks, whose decisions and output contracts differ.

Return columns exactly as version_a, version_b, overlap_days. Sort order follows the task. Output has 1 rows; an empty CSV field means NULL.

Source data

versions

9 source rows · Download versions CSV

id INTEGERentity TEXTvalid_from INTEGERvalid_to INTEGERrecorded INTEGERvalue INTEGERdeleted INTEGER
1A01001000
2A102081200
3A010121100
4B080500
5B10205700
6C0200800
7C5159900
8D0200400
9D02010NULL1

facts

5 source rows · Download facts CSV

id INTEGERentity TEXTday INTEGERamount INTEGER
1A52
2A123
3B91
4C104
5D42
Field dictionary and schema
versions:
  id INTEGER — Stable source record identifier; not a row position.
  entity TEXT — Literal business key or category label. Match exactly and retain NULL where present.
  valid_from INTEGER — Inclusive valid-time start day.
  valid_to INTEGER — Exclusive valid-time end day.
  recorded INTEGER — Transaction-time day index when this version became known.
  value INTEGER — Measured numeric value; NULL means unobserved.
  deleted INTEGER — Tombstone flag:1 deleted,0 active; apply precedence before filtering.

facts:
  id INTEGER — Stable source record identifier; not a row position.
  entity TEXT — Literal business key or category label. Match exactly and retain NULL where present.
  day INTEGER — Integer day index, or ISO YYYY-MM-DD date when stored as text; the task declares cutoff semantics.
  amount INTEGER — Exact integer business measurement. Its specific business rule and eligibility are declared in the task.

Download complete SQLite setup

A method you can reuse

  1. At cutoff15, return version_a,version_b,overlap_days for nondeleted same-entity versions a<b with positive overlap, excluding pairs with identical validity endpoints because those are corrections. Sort both IDs.
  2. For the first output record, version_a = 6; version_b = 7; overlap_days = 10.
  3. Inspect the source grain, keys and ties. Compare each result column with the full worked SQL and expected CSV.

Watch for this wrong turn

Flagging exact-interval correction pairs as independent slice overlaps mixes lineage with validity design errors.

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
SELECT a.id version_a,b.id version_b,MIN(a.valid_to,b.valid_to)-MAX(a.valid_from,b.valid_from) overlap_days FROM versions a JOIN versions b ON a.entity=b.entity AND a.id<b.id AND a.valid_from<b.valid_to AND b.valid_from<a.valid_to WHERE a.recorded<=15 AND b.recorded<=15 AND a.deleted=0 AND b.deleted=0 AND NOT(a.valid_from=b.valid_from AND a.valid_to=b.valid_to) ORDER BY a.id,b.id;
"version_a","version_b","overlap_days"
"6","7","10"
Reference SQL · Complete expected CSV · Empty answer CSV

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